manton
manton

Reputation: 561

Task.Factory and communication between threads

I'm having one thread that generates GUI elements in wpf. A canvas is there to draw objects (rectangles and so on ...)

This wpf thread calls another thread let's name it the calculation thread. This thread calculates the size and position and so on of the elements to be shown in the canvas.

I want to have these two parts (GUI and calculatio) run in different threads. The "calculation thread" is based on a library without references to wpf functionality.

Now I want to show intermediate data of the calculation thread displayed by the wpf thread. I'm doing it that way:

The calc-thread fires an event (DataReady) that is implemented by the wpf-thread:

void MyRegStringObject_DataReady()
{
  if (DebugMode)
    MyDrawingBoard.DrawRegElements();
}

The problem now is, that an error is thrown: "calling thread cannot access this object because a different thread owns it"

There are some answered question here in stackoverflow referring to this error but none of them can help in my case.

The function DrawRegElements() wants to clear the canvas object (among other things):

curCanvas.Children.Clear();

At this position in the code the error is thrown. It seems that the function MyRegStringObject_DataReady triggered by an event from the calc-thread is owened by the calc-thread also. But it is defined in class that the wpf-thread is based on.

How can I solve this problem? Does anyone have any idea? By the way: The calc-thread is called this way:

CalcElements = Task.Factory.StartNew<bool>(MyRegStringObject.CalcRegElements);

When the thread is finished I defined:

CalcElements.ContinueWith((FinishCalcRegElements) =>
{
  MyDrawingBoard.DrawRegElements();
}, CancellationToken.None, TaskContinuationOptions.None, 
TaskScheduler.FromCurrentSynchronizationContext());

No problem with that. Everythins runs perfect. The function defined in ContinueWith seems to be owened by the wpf-thread.

Upvotes: 1

Views: 1296

Answers (1)

sll
sll

Reputation: 62544

Error message is straightforward, you are trying access an UI element from a now owning thread. Delegate this work to WPF Dispatcher which is associated with Main UI thread, which would post all messages from worker thread to UI thread:

Application.Current.Dispatcher.BeginInvoke((ThreadStart)delegate
   {            
       MyDrawingBoard.DrawRegElements();   
   });

To be sure that you are using correct Dispatcher, associated with the Main UI thread, you can pass it as a parameter to the worker thread code, so just pass Dispatcher.Current from Main UI thread, otherwise calling Dispatcher.CurrentDispatcher in the worker thread will initialize a new instance associated with a calling worker thread. Or just use Application.Current.Dispatcher which would reference the Main UI thread's Dispatcher.

PS:

But it is defined in class that the wpf-thread is based on

This is wrong assumption, classes itself are not related to any thread. Concrtete class instances are. So you can create UI/non-UI related classes either in UI or worker thread.

Also important point in which thread DataReady event has been fired?

Upvotes: 2

Related Questions