// Start the async thread, or perform the operation synchronously. public void Start() { if (Thread.CanStartThreads()) { ThreadPool.QueueCompletionItem (new WaitCallback(Run), null); } else { completedSynchronously = true; Run(null); } }
// Construct a new asynchronous result object and begin invocation. internal AsyncResult(Delegate del, Object[] args, AsyncCallback callback, Object state) { // Initialize the fields within this class. this.del = del; this.args = args; this.callback = callback; this.state = state; this.result = null; this.resultException = null; this.synchronous = false; this.completed = false; this.endInvokeCalled = false; // If we have threads, then queue the delegate to run // on the thread pool's completion worker thread. if (Thread.CanStartThreads()) { ThreadPool.QueueCompletionItem (new WaitCallback(Run), null); return; } // We don't have threads, so call the delegate synchronously. this.synchronous = true; try { this.result = del.DynamicInvoke(args); } catch (Exception e) { this.resultException = e; } this.completed = true; if (callback != null) { callback(this); } }