multithreading - C# Task ConfigureAwait -


Thought that I was handling the ConfigureAwait , then I tried to do one experiment.

I understand that configure (wrong) will only make a difference when the synchronization context occurs.

ASP, WPF, etc. should be the reference, but the console applications and service applications should not be.

To see how this works, I created a Web API app and included the following method:

  // API / Value / 5 Public Async Task & Lt; String & gt; (Enter ID) {var syncCtx = SynchronizationContext.Current; Int startThreadId = Thread Present. Thread Managed Trades ID; Waiting for work. Delay (timespace.formsends (3)). Configurable (true); Int endThreadId = Thread.Centrantthith Managed Trades ID; Return "Start Thread ID:" + startThreadId.ToString () + ": Finished Thread ID:" + endThreadId.ToString (); }   

I predicted that ConfigureAwait or ConfigureAwait is not set to true , I should see

My first few tests show that with the right set as above.

Later the start of the code began and ended on the separate thread ID ConfigureAwait .

I've added syncCtx to explain myself.

I have read that one of the warnings is that if the work is done, then you will not be guaranteed the same ID. Is this the case here? If so, then why is this the case?

Am I setting up a crappy or faulty trial? If so, what would be a fair trial?

I started this path in a console / service app and I realized that I was not getting the same thread ID. I was adding ConfigureAwait (false) as I have recommended to write the "best practice". Since I want to see how things really work, I tried to test the thread ID. Seeing them, I carried forward through various discoveries through different searches, which happened in the above code.

Configure (true) (or leaving it now, because it is the default value) It does not run on the same thread, it is Synchronization Contains. Tells the current to continue or to continue the rest. How to post or send does not really run the code.

WindowsFormsSynchronizationContext and DispatcherSynchronizationContext (WinForms and WPF contexts) will put continuity on the line of messages to be processed by Message Pump (UI Thread) .

On the other hand, AspNetSynchronizationContext (which is running under you) just sets some state information (such as httpctx. Current Is set to its old value), then it queues up to the next available thread pool thread. There is no "message pump" for ASP.NET, it does all its work just on the thread pool, so there is no point in trying to get the same thread from the thread pool later, this thread is already there May be destroyed by the time of continuity.

After seeing the same ID after and after you just got lucky and the thread was dragged out of the pool before and after continuing the same thread. >

I highly recommend that you read MSDN magazine articles "", explaining this in detail how the synchronization contaxes work and goes in some kind of context. NET About built-in references

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -