This may be due to a lack of understanding开发者_运维知识库 of what's going on under the hood or simply a lack of understanding of threading in general. When a user logins I need to run some tasks that call web services to update data in my system. Since the services could take a considerable amount of time I thread the entire process. However, even though I think I'm running the whole series of tasks in a thread separate from my application, my application waits until the called function is finished before it proceeds.
WindowsIdentity identity = WindowsIdentity.GetCurrent();
Manager manager = (Manager)Session["Manager"];
ThreadPool.QueueUserWorkItem(new SafeWaitCallback().Call(identity, delegate(object noCallBack)
{
manager.RunAccountUpdater(identity);
}));
The application hangs until the function "RunAccountUpdater" is finished and the callback occurs. What am I doing wrong/not understanding?
Go through you actually do there statement by statement - it will be obviouss.
ThreadPool.QueueUserWorkItem(new SafeWaitCallback().Call(identity, delegate(object noCallBack) { manager.RunAccountUpdater(identity); }));
This happens from inside out.
- You declare a delegate.
- You then use SafeWaitCallback().Call to EXECUTE the operation.
- Then you queue the result of the Call to.... the thread pool.
Voila - here is your issue. You have to queue the call to the delegte, not the result of the execution.
精彩评论