Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a timer which in Elapsed Event i want to change the WindowState. but i get this error:
The calling thread cannot access this object because a different thread owns it.

what should i do? it's a WPF project

ok here is my code

C#
System.Timers.Timer timer=new System.Timers.Timer();

private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
            this.WindowState = System.Windows.WindowState.Minimized;
            timer.Interval = 5 * 1000;
                timer.Enabled = true;
                timer.AutoReset = true;
                timer.Start();
		}

 void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                
                    timer.Enabled = false;
                    this.WindowState = System.Windows.WindowState.Normal;
                     .....

                
                timer.Enabled = true;
                timer.Start();

            }
            catch (Exception ex)
            {
                Logger.Log("OnTimer Error:" + ex.Message);
            }
        }
Posted
Updated 27-Jul-12 21:46pm
v4
Comments
OriginalGriff 28-Jul-12 3:05am    
How about you post the code fragment which caused the error?
That way, we would stand a vague chance of working out why?
Use the "Improve question" widget to edit your question and provide better information.
mahdi87_gh 28-Jul-12 3:14am    
ok thanks.

1 solution

I think when you extracted the fragment you got a bit enthusiastic - that code won't work.
Do you do anything else with the timer? I suspect you do, because the code to attach the event handler is not there, which implies that there is additional code missing.

I would guess that the problem is that the timer is firing before the form is fully ready, and that is the only reason I can think of for the set to Normal failing.

So check your code - see if the code which attaches the event handler has the timer and the form fully ready before the timer fires.
 
Share this answer
 
Comments
mahdi87_gh 28-Jul-12 3:50am    
here is the attach part. and i started the timer on window_loaded after 5 secs.i'm sure the form will be ready by that time.because it doesn't load anything from database or sth.
public MainWindow()
{
this.InitializeComponent();
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
}
OriginalGriff 28-Jul-12 3:53am    
Move the event attachment to just before the timer.Start method call - it should be disabled by default, but it might not be. If it is firing early, then that would explain the exception.
mahdi87_gh 28-Jul-12 3:56am    
It didn't work
mahdi87_gh 28-Jul-12 3:53am    
and i updated the error code. did u see that?
thanks
OriginalGriff 28-Jul-12 4:08am    
Why didn't you say that before? :laugh:
You can only access UI elements from the UI thread - you need to invoke it. Since this is WPF, you need to use the Dispatcher. Have a look here: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/360540eb-d756-4434-86f9-a3449f05eb55/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900