473,386 Members | 1,598 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.

C sharp Thread need help...!!

Hi i am a beginner in C sharp and trying to develop a wince 5.0 device application using C sharp.

i am using a thread in the Form1 Load method which is continously running a method named main_program

the problem is when i try to display any text on the textbox it shows the
exception message as follows.

Control.Invoke must be used to interact with controls created on a seperate
thread.


as shown in the code below..

Please tell me how to resolve this problem...!!!


CODE:


Expand|Select|Wrap|Line Numbers
  1. namespace biometric_device_app
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         public AsyncCallback pfnWorkerCallBack;
  6.         public Socket m_socListener;
  7.         public Socket m_socWorker;
  8.  
  9.  
  10.         public Form1()
  11.         {
  12.             InitializeComponent();
  13.         }
  14.  
  15.  
  16.         Thread MainProgramThread;
  17.         String entry = "";
  18.         int toggle = 0;
  19.  
  20.         private void flashing(object sender, EventArgs e)
  21.         {
  22.  
  23.             if (toggle == 0)
  24.             {
  25.                 lbl_login.Text = "PRESS FINGER TO LOGIN";
  26.                 toggle = 1;
  27.             }
  28.             else
  29.             {
  30.                 lbl_login.Text = "";
  31.                 toggle = 0;
  32.             }
  33.         }
  34.  
  35.  
  36.  
  37.  
  38.         private void main_program()
  39.         {
  40.  
  41.  
  42.             while (true)
  43.             {
  44.                 Thread.Sleep(500);
  45.                 switch (current_operation)
  46.                 {
  47.  
  48.  
  49.                     case 0:         // for finger print recognition
  50.  
  51.                         try
  52.                             {
  53.  
  54.                                 txt_show.Text = "hello";
  55.  
  56.                                 if (!serialPort1.IsOpen)
  57.                                     serialPort1.Open();
  58.                                 serialPort1.ReadTimeout = 10000;
  59.                                 int flag_0 = 0;
  60.                                 int flag_no_match_0 = 1;
  61.                                 byte[] value_search = new byte[28];
  62.                                 byte[] info_0 = { 0x02, 0x00, 0x08, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xc6, 0x03 };
  63.                                 byte[] device_getnumb_0 = { 2, 0, 8, 0, 50, 77, 66, 73, 68 };
  64.                                 byte[] no_match_0 = { 2, 0, 6, 57, 50 };
  65.                             again: serialPort1.Write(info_0, 0, 13);
  66.                                 //Thread.Sleep(200);                      // 100 ms Delay after sending command
  67.                                 serialPort1.Read(value_search, 0, 1);
  68.                                 Thread.Sleep(5);
  69.                                 serialPort1.Read(value_search, 1, 12);
  70.                                 for (int b = 0; b < 9; b++)
  71.                                 {
  72.                                     if (!(value_search[b] == device_getnumb_0[b]))
  73.                                     {
  74.                                         flag_0 = 1;
  75.                                         break;
  76.                                     }
  77.                                     else
  78.                                         flag_0 = 0;
  79.                                 }
  80.  
  81.                                 if (flag_0 == 1)
  82.                                 {
  83.                                     for (int b = 0; b < 5; b++)
  84.                                     {
  85.                                         if (!(value_search[b] == no_match_0[b]))
  86.                                         {
  87.                                             flag_no_match_0 = 1;
  88.                                             break;
  89.                                         }
  90.                                         else
  91.                                             flag_no_match_0 = 0;
  92.                                     }
  93.                                 }
  94.                                 /*
  95.                                 while (flag == 1)
  96.                                 {
  97.                                     Thread.Sleep(200);
  98.                                     goto again;
  99.                                 }
  100.                                 */
  101.  
  102.  
  103.  
  104.  
  105.                                 if (flag_0 == 0)
  106.                                 {
  107.                                     int num = value_search[9] * 256 + value_search[10];
  108.                                     txt_show.Text = "EMPLOYEE NUMBER " + num + " LOGED IN";
  109.  
  110.                                     if (rad_btn_in.Checked)
  111.                                     {
  112.                                         entry = "IN";
  113.                                         rad_btn_in.Checked = true;
  114.                                     }
  115.                                     else if (rad_btn_out.Checked)
  116.                                     {
  117.                                         entry = "OUT";
  118.                                         rad_btn_out.Checked = true;
  119.                                     }
  120.                                     else ;
  121.  
  122.                                     /*
  123.                                     while (!((rad_btn_in.Checked) || (rad_btn_out.Checked)))
  124.                                         Thread.Sleep(100);
  125.                                     */
  126.  
  127.                                     StreamWriter rd = new StreamWriter(@"My Documents\Attendence.att", true);
  128.                                     rd.WriteLine("LOGIN-----------" + "#" + "Employee Number: " + num + "#" + entry + "#" + DateTime.Now.ToString());
  129.                                     rd.Close();
  130.                                 }
  131.  
  132.                                 else if (flag_no_match_0 == 0)
  133.                                     txt_show.Text = "NO MATCH FOUND";
  134.  
  135.  
  136.                                 value_search = null;
  137.                                 serialPort1.Close();
  138.                                 flag_no_match_0 = 1;
  139.                                 timer1.Enabled = true;
  140.                             }
  141.                             catch (Exception ex)
  142.                             {
  143.                                 MessageBox.Show(ex.Message);
  144.                             }
  145.  
  146.                             Thread.Sleep(1000);
  147.  
  148.  
  149.                             break;
  150.  
  151.  
  152.  
  153.  
  154.  
  155.                     case 1:         // for enroll
  156.                            current_operation = 0;
  157.                             break;
  158.  
  159.                     case 2:         // for lan recv
  160.                         current_operation = 0;
  161.                         break;
  162.  
  163.  
  164.                 }
  165.             }
  166.         }
  167.  
  168.  
  169.  
  170.  
  171. private void Form1_Load(object sender, EventArgs e)
  172.             {
  173.  
  174.                 MainProgramThread = new Thread(new ThreadStart(main_program));
  175.                 MainProgramThread.Start();
  176.             }
Sep 25 '09 #1
4 2807
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Sep 25 '09 #2
tlhintoq
3,525 Expert 2GB
So often error messages are nearly worthless. How often have we seen messages that say something silly like "The unknown program has stopped for unknown reasons"?

But in this case your error message pretty well spells it out.
Control.Invoke must be used to interact with controls created on a seperate thread.

MSDN on control.invoke

Personally, I'm a fan of using anonymous methods for such things where possible.
Expand|Select|Wrap|Line Numbers
  1. void DoSomethingMethod()
  2. {
  3.     Invoke((MethodInvoker)delegate
  4.     {
  5.         // actually do the "something"
  6.     });
  7. }
Sep 25 '09 #3
GaryTexmo
1,501 Expert 1GB
Hmmm, that error message seems pretty descriptive. You're trying to access members of Form1 from a separate thread. I think what you need to do is set up some form of communication from your thread running main_program, to the thread running Form1.

Can you use some kind of call back or event? I have not done much work with threads, but can you pass a parameter to the thread running main_program that tells it how to get back to Form1?

Don't take these as solutions, I'm just brainstorming out loud :)
Sep 25 '09 #4
Plater
7,872 Expert 4TB
I believe (since you really only get that exception in debugging) it also provides you with a direct link to MSDN about making threadsafe callbacks to the gui thread
Sep 25 '09 #5

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

Similar topics

4
by: Hai Nguyen | last post by:
I'm learning C sharp and do not like vb much. I'm creatiing a wepage using panel to test myself. I tried to use these code below, which is written in VB, and to transform them to c sharp but I got...
2
by: HishHish | last post by:
I have a Java code that I want to convert it to C-sharp in order to put it in my ASP.NET web application. I used the JLCA (Java Language Conversion Assistant) for conversion, but it gave me many...
3
by: milkyway | last post by:
Hello all, I was looking at the thread listed below. Basically, I have an HTML page executing form.Submit() (written with Java script). What do I need to do in order see the values in the...
1
by: Jeevan | last post by:
Hi, I am creating a Window Service in C-Sharp. The Window Service has a reference to an OCX file created in VC++. In OnStart method I have created an instance of the class, of the OCX file and...
2
by: vivekian | last post by:
Hi , I am a C Sharp newbie, and need to study its compiler internals as a part of my small project. Does Microsoft provide compiler details e.g. what kind of symbol table it uses etc ? I...
2
by: shrishjain | last post by:
Hi All, I have a xml-schema and I want to generate C-Sharp classes out of it. Please let me know if there is a tool available for that. Also, I have a model(C-Sharp Classes) and I want to...
4
by: pms | last post by:
Need suggestions to improve this program. using System; namespace CalculateWage { class GetEmployeeDetails {
2
by: LostnCode | last post by:
Hi, Can anyone help, I need to convert his code from vbscript to sharp C# for use with ASP.Net2.0? This is my first time using a forum. I don't know anything about either coding language so...
6
by: svgeorge | last post by:
I need help in C Sharp Web Interface , I have web pages for making several 9 type of payments. The data gets loaded on web page from SQL server 2005 database. Then I have Process payment...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.