ThreadPool.QueueUserWorkItem causes the application to freeze until completion

This may be due to a lack of understanding of what is happening under the hood, or simply a lack of understanding of the thread as a whole. When a user logs in, I need to run some tasks that call web services to update data on my system. Since services can take a considerable amount of time, I process the entire process. However, although I think that I am performing a series of tasks in a separate thread other than my application, my application waits until the called function is finished before continuing.

WindowsIdentity identity = WindowsIdentity.GetCurrent();
Manager manager = (Manager)Session["Manager"];

ThreadPool.QueueUserWorkItem(new SafeWaitCallback().Call(identity, delegate(object noCallBack)
{
    manager.RunAccountUpdater(identity);
}));

The application freezes until the "RunAccountUpdater" function is complete and a callback occurs. What am I doing wrong / don't understand?

+3
source share
1 answer

Go through the fact that you are actually making a statement there on the expression - it will be obvious.

ThreadPool.QueueUserWorkItem (new SafeWaitCallback (). Call (identity, delegate (noCallBack object) {manager.RunAccountUpdater (identity);}));

It comes from within.

  • You declare a delegate.
  • Then you use SafeWaitCallback (). Call EXECUTE operation.
  • Then you queue up the result of the call ... the thread pool.

Warrior is your problem. You should queue the call to the delegate, not the result of the execution.

0
source

Source: https://habr.com/ru/post/1779510/


All Articles