Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Creation of object in thread not accessible
Message
From
31/05/2011 15:27:15
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 4.0
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01512341
Message ID:
01512384
Views:
42
>>>>Hi all,
>>>>
>>>>I have been moving some code to run in a background thread to speed up the performance. In my background worker I create the Configuration class which contains this property.
>>>>
>>>>
>>>>/// <summary>
>>>>/// Main Window State Setting
>>>>/// </summary>
>>>>public System.Windows.WindowState MainWindowState
>>>>{
>>>>	get { return OCSDApplication.Current.MainWindow.WindowState; }
>>>>	set { OCSDApplication.Current.MainWindow.WindowState = value; }
>>>>}
>>>>
>>>>
>>>>Later when Application.InitializeComponent is run, this fails because of "InvalidOperationException" - "The calling thread cannot access this object because a different thread owns it".
>>>>
>>>>Is there a way when it is created to get the ownership of the class back on the main thread? This is WPF by the way.
>>>
>>>Dispatcher ? This works and maybe you can adapt:
   public partial class SomeWindow : Window
>>>    {
>>>        public SomeWindow()
>>>        {
>>>            InitializeComponent();
>>>
>>>            BackgroundWorker b = new BackgroundWorker();
>>>            b.DoWork += new DoWorkEventHandler(DoWork);
>>>            b.RunWorkerAsync();
>>>        }
>>>
>>>        public void SetMainWindowState(WindowState ws)
>>>        {
>>>            Application.Current.MainWindow.WindowState = ws;
>>>        }
>>>
>>>        public WindowState GetMainWindowState()
>>>        {
>>>            return Application.Current.MainWindow.WindowState;
>>>        }
>>>
>>>        void DoWork(object sender, DoWorkEventArgs e)
>>>        {
>>>            MainWindowState = WindowState.Maximized;
>>>            WindowState test = MainWindowState;
>>>        }
>>>
>>>        private WindowState mainWindowState;
>>>        public WindowState MainWindowState
>>>        {
>>>            set { Dispatcher.Invoke(DispatcherPriority.Normal, new Action<WindowState>(SetMainWindowState), value); }
>>>            get
>>>            {
>>>                Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
>>>                    { mainWindowState = this.GetMainWindowState(); }));
>>>                return mainWindowState;
>>>            }
>>>        }
>>>    }
>>
>>In this case Dispatcher is Application.Dispatcher?
>
>No, in the example it's the SomeWindow.Dispatcher. I guess you could do the same thing using Application.Dispatcher.
>
>>In my configuration class would it not have to be System.Windows.Threading.Dispatcher? Would this make a difference?
>
>System.Windows.Threading.Dispatcher is the class returned by DispatcherObject.Dispatcher.
>AFAICS, if you use the static Threading.Dispatcher.CurrentDispatcher property it will only give you the dispatcher for the current thread - which won't be much use.....
>
>Sorry, hard for me to try to explain this without it sounding confusing :-{

That's Ok, I don't have a complete grasp on the whole Dispatcher concept, but I understand what you are saying. I do not know quite how to approach this situation as my property that stores the configurarion is in a class but when it it touched I get the issue of it belonging to another thread. It doesn't appear I can use Dispatcher.Invoke because it isn't available to me in the configuration class. I am looking now at how to just rearchitect this somehow. They store the Window State in configuration and it is saved so the next startup the window state is restored. Only difference is that I implemented some background workers to do some things, but when the Application itself is created and the Main Window run, all that is on the main UI thread. I am not sure exactly where yet the setter for this property is being called from as there are no usages found and doing a find on the property name doesn't find it. But it does stop there and the error does come up there.
Thanks
Timothy
Timothy Bryan
Previous
Reply
Map
View

Click here to load this message in the networking platform