Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Creation of object in thread not accessible
Message
From
31/05/2011 13:07:39
 
 
To
31/05/2011 12:29:05
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:
01512365
Views:
39
>>>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 :-{
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform