473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
1 3895
Plater
7,872 Expert 4TB
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

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

Similar topics

1
by: Timothy V | last post by:
Hi, People often say that the .NET Framework is cross-platform. Does this mean linux and macOS? Thank you in advance, Timothy.
7
by: Web Master | last post by:
Are there any good websites or books that deal with cross platform XML? I'd like to target Internet Explorer/Netscape Navigator 5 or later, as well as Opera, possibly konqueror on Linux as well. ...
3
by: Klint Gore | last post by:
I've hit a situation where I'm getting an unexpected set of results from a cross join that I've narrowed down to a table alias. If I do (a has 4 rows, b has 1 row) select a.field1, b.* from a...
2
by: Peter Bär | last post by:
A Question to the C#/.Net Gods of this forum: are there performance penalties when i compile (C#, FW1.1, ASP.NET, Studio2003) a central baseclass in a different assembly than all the derived...
17
by: Mark Rae | last post by:
Hi, I'm interested in your opinion concerning how far you would consider it necessary to code for cross-browser compatibility these days, especially for public-facing Internet sites... ...
3
by: willemp | last post by:
i have the need for a bit of an complex cross-tab report. the following information needs to be shown in 1 report. projectname department hours quarter 1,2,3 and 4 and offcourse all sort of...
6
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get:...
1
by: Han-Wen Nienhuys | last post by:
Hello, I have a small patch for Python SVN that makes it possible to cross-compile python on Unix to various other Unix targets. I have successfully built a binary for FreeBSD on Linux. The...
12
by: Acrobatic | last post by:
I'm trying to store user passwords in a MySQL database. I learned the hard way that using MySQL "DECODE" and "ENCODE" doesn't seem to work cross-platform, but if I encrypt on the server side with...
1
by: Roumen Petrov | last post by:
Hi list members, It seems to me that this is discussed many times in the past but without progress. As I understand in general there is no objections and preferred cross-compilation has to be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.