Does Task.StartNew () work differently in STA mode?

I am completely new to this whole topic, so hopefully someone can enlighten me.

I have a WPF interface from which I launch a DLL with the click of a button. When the button is pressed, it starts the dll asynchronously so that the user can "navigate" the user interface when the DLL does its job:

await Task.Factory.StartNew(new Action(() => strTime = 
                                             StartSync.Start(strPathFile,
                                             licManager.idCmsMediator)));

this worked very well until I had to run this task in STA mode to open windows in the dll. So I changed this line using the method described in this post :

var scheduler = TaskScheduler.FromCurrentSynchronizationContext();
await Task.Factory.StartNew(new Action(() => strTime = 
                                             StartSync.Start(strPathFile,
                                             licManager.idCmsMediator)),
                                             System.Threading.CancellationToken.None, 
                                             TaskCreationOptions.None, scheduler);

but now when I start the dll by clicking the button, I can no longer navigate the user interface! As if it doesn't work asynchronously anymore ?? How can I run the task in STA mode, but can I still navigate the user interface?

Thanks in advance

+4
source share
1 answer

but now when i start the dll by clicking the button, I can not navigate the user interface

Task != Thread. A task may or may not use a thread to operate it. When you use:

var scheduler = TaskScheduler.FromCurrentSynchronizationContext();

You tell the factory task to execute the given delegate in the current captured synchronization context, which is your UI synchronization context. When you do this, it will execute it in the user interface message loop.

, STA, dll

, . , , , :

// Start CPU bound work on a background thread
await Task.Run(() => strTime = StartSync.DoCpuWork(strPathFile, 
                                                   licManager.idCmsMediator)));

// We're done awaiting, back onto the UI thread, Update window.
+3

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


All Articles