1

I am trying to create dynamically custom userControl in background thread. This is my method where I am creating new thread:

var thread = new Thread(CreateItemInBackgroundThread);
thread.SetApartmentState(ApartmentState.STA);            
thread.Start();
thread.Join();

And this is method CreateItemInBackgroundThread:

var uc = new MyUserControl();
UserControl item = uc;
AllControls.Add(item);
//Here I am trying to add control to a current Tab
foreach (var currentTab in _allTabs)
{
    currentTab.DocumentWindow.Dispatcher.BeginInvoke(new Action(() =>
                                                            {
                                                                if (tab.DocumentWindow.IsSelected)
                                                                {
                                                                    tempTab = tab;
                                                                    tempControl = item;
                                                                    finish = true;
                                                                }

                                                            }));
}

This is my finish property

bool finish
    {
        get { return _finish; }
        set
        {
            _finish = value;
            if (_finish)
            {
                tempTab.AnimatedCanvas.Dispatcher.BeginInvoke(new Action(() => tempTab.AnimatedCanvas.Children.Add(tempControl)));
            }
        } // Here I get error - The calling thread cannot access this object because a different thread owns it
    }

How can I avoid this error and why this error happend?

Sasha
  • 833
  • 1
  • 20
  • 40
  • 1
    http://stackoverflow.com/questions/11923865/how-to-deal-with-cross-thread-access-exceptions – H.B. Jul 02 '13 at 10:18
  • Also why would you try to use the dispatcher of some specific object? Just use `Application.Current.Dispatcher`. – H.B. Jul 02 '13 at 10:19
  • I want to access tempControl, , where my dynamically created element is saved and put it to UI, but in my property I have always this error – Sasha Jul 02 '13 at 14:07

2 Answers2

0

as the error says, you can't access the this object because a different thread owns it, so you can invoke that thread using Invoke(Delegate Method) you can check if invoke required using tempTab.InvokeRequired

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
0

This error is coming because u must be doing the different tasks on the same thread like U cannot make a thread go async and update the UI using the same thread.It will cause conflict.because UI thread is the main thread.

You can use BAckground Worker Thread and subsribe its two eventHandlers to your events which you want to work on.. for eg-

BackgroundWorker Worker=new BackgroundWorker();
worker.DoWork+=Yorevent which will do the timeTaking Task();
Worker.RunWorkerCompleted+=YOurEvent which will Update your UI after the work is done();
worker.RunWorkerAsync();

RunWorkerAsync() will make your thread go async and work on background this way it will not cause any thread Error too..

Vishal
  • 604
  • 1
  • 12
  • 25
  • Good idea, but backgroundWorker is not usefull here, because I need to create a custom usercontrol using ApartmentState.STA – Sasha Jul 02 '13 at 14:10