0

From UI (thread1) I want to create a progress UI(thread2). Progress UI creates a task in thread3 and waits for its completion.

The task(thread3) completes and invoke closing of progress UI which must be executed in thread2.

For closing operation I use AsyncOperationManager to capture context of thread2 and then execute POST method from thread4.

But closing always happens from some another thread.

All the code below is from ProgressWindows class.

_currentTask = new Progress<double>(Close); // I call this in progress UI constructor.

// This is invoked in constructor of Progress class which is used inside ProgressWindow.
_asyncOperation = AsyncOperationManager.CreateOperation(null);

public static void Run2(Action action) 
{ 
    Debug.WriteLine(":: Run2 in thread: {0}", Thread.CurrentThread.ManagedThreadId); 

    var th = new Thread(_ => 
    { 
        Debug.WriteLine(":: StartNew in thread: {0}", Thread.CurrentThread.ManagedThreadId); 
        var progress = new ProgressWindow(); 
        progress.Run(action); 
    }); 

    th.SetApartmentState(ApartmentState.STA); 
    th.Start(); 
} 


 public void Run(Action action) 
 { 
    Debug.WriteLine(":: Run in thread: {0}", Thread.CurrentThread.ManagedThreadId); 

    SetupProgressBar(); 
    RunTask(action); 

    ShowDialog(); 
} 

private void RunTask(Action action) 
{ 
    Task.Factory.StartNew(action).ContinueWith(_ => _currentTask.OnCompleted(null)); 
} 

private void Close(object state) 
{ 
    Debug.WriteLine(":: Close in thread: {0}", Thread.CurrentThread.ManagedThreadId); 

    Hide(); 
    Close(); 
} 

The question is:

private void RunTask(Action action) 
{ 
    Task.Factory.StartNew(action).ContinueWith(_ => _currentTask.OnCompleted(null)); 
} 

You see, _currentTask.OnCompleted(null) is invoked from another thread, but _currentTaskof type Progress uses context captured in the UI thread, but OnCompletedis always invoked from another thread other than UI thread. Why? It must be in the same context.

Update 1:

Mixing System.Threading.SynchronizationContext with System.Windows.Form.WindowsFormsSynchronizationContext and System.Windows.Threading.DispatcherSynchronizationContext

8
  • You're calling ContinueWith from a background thread. Why would you assume it would marshal it to the UI thread? Oct 16, 2014 at 12:34
  • Because within the scope of ProgressWindow I create Progress where I create asyncOperation with AsyncOperationManager.CreateOperation. And I call OnCompleted with asyncOperation.Post. Definitely, this is the same UI context... I suppose so. Oct 16, 2014 at 12:38
  • All is fixed with Dispatcher.BeginInvoke, but I wonder why SynchronizationContext is a miss here? Oct 16, 2014 at 12:45
  • Not sure, not familiar with AsyncOperationManager. Oct 16, 2014 at 12:50
  • 1

1 Answer 1

0

Synchronization context in asynchronous WCF per call service

Force WCF to use one thread

AsyncOperationManager works differently depending on context where AsyncOperationManager.CreateOperation was invoked. Denending on context, AsyncOperationManager.CreateOperation may create three contexts:

System.Threading.SynchronizationContext System.Windows.Form.WindowsFormsSynchronizationContext System.Windows.Threading.DispatcherSynchronizationContext

In my case when I capture context before ShowDialog, I capture thread sync context, but if I invoke AsyncOperationManager.CreateOperation within OnLoad event, I capture System.Windows.Form.WindowsFormsSynchronizationContext or DispatcherSynchronizationContext depending on WindowsForms or WPF is used. That was the source of error.

I would cut it down to just using Dispatcher.BeginInvoke. But to leave all the code with Progress, I just placed AsyncOperationManager.CreateOperation and creating new background thread inside OnLoad of ProgressWindow UI. It has resolved the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.