By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,644 Members | 1,866 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,644 IT Pros & Developers. It's quick & easy.

Cross-thread operation not valid - used timer to do someting, but can't hide the form

P: 1
Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Timers; 
  10.  
  11. namespace WindowsFormsApplication2
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         private static System.Timers.Timer aTimer;
  16.  
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.             aTimer = new System.Timers.Timer();
  21.             aTimer.Elapsed += new ElapsedEventHandler(found_card);
  22.             // Set the Interval to 2 seconds.
  23.             aTimer.Interval = 1000;                     
  24.             aTimer.Enabled = true;          
  25.  
  26.         }
  27.  
  28.         private void found_card(object sender, EventArgs e)
  29.         {
  30.             aTimer.Enabled = false;
  31.  
  32.             // use smart card system to find the card. if found the card, it will open form2 and close form1            
  33.  
  34.             Form form2 = new Form2();
  35.             this.Hide();  // <--- error, please help me to make a solution
  36.             form2.ShowDialog();
  37.             this.Close();                
  38.         }
  39.     }
  40. }
  41.  
Nov 17 '09 #1
Share this Question
Share on Google+
1 Reply


Plater
Expert 5K+
P: 7,872
That exception only gets thrown in debug mode, to alert you of what you are doing.
In the exception message should be a link to MSDN about ways to solve that issue.
My favorite is using a delegate and invoke.

For instance in my form I have:
Expand|Select|Wrap|Line Numbers
  1. private delegate string ReportInfoCallback(string msg);
  2.  
  3. private string ReportInfo(string msg)
  4. {
  5.   if (this.InvokeRequired)
  6.   {
  7.     ReportInfoCallback d = new ReportInfoCallback(ReportInfo);
  8.     this.Invoke(d, msg);
  9.   }
  10.   else
  11.   {
  12.     tbResults.AppendText(msg);
  13.   }
  14.   return msg;
  15. }
  16.  
Now from another thread (like a timer) I can call the ReportInfo("Some text"); function and it will handle the cross threading.

You can change up the function and the delegate to match your needs for hiding.


I see that you are also calling this.Close(), once that happens your timer dissapears too
Nov 18 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.