473,804 Members | 3,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C sharp Thread need help...!!

2 New Member
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 2831
tlhintoq
3,525 Recognized Expert Specialist
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 Recognized Expert Specialist
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 Recognized Expert Top Contributor
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 Recognized Expert Expert
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
2515
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 hard time to understand vb syntax. I don't know if anyone in here can point out which is the equivalent object used in c sharp. Translate these two lines to C sharp: Sub Next_Click(Sender As Object, e As EventArgs) Select Case...
2
3900
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 errors indicating that there are not matches in .NET for those Java classes!! The Java code is: ***************************************************************** import java.util.Properties; import javax.mail.*;
3
3340
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 fields of the form on the server side. Normally (in .jsp), I would get the values on the page and process it in a servlet. There would be tuples that would be processed in the servlet. But I do not see how to do this in C#.
1
2012
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 called a method of the class. Compilation was successful. I created a Windows installer setup(.MSI), for installing the service. The installation was successful. I started my service from the Services Management Console. It started and stopped...
2
2051
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 believe there is an equivalent Linux implementation - Mono available . Would looking it up help since its open source ? Any pointers to links would be a big help. Thanks in advance,
2
1352
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 generate a schema out of it. Please let me know if there is a tool available for that. THnaks, Shrish
4
1269
by: pms | last post by:
Need suggestions to improve this program. using System; namespace CalculateWage { class GetEmployeeDetails {
2
2189
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 would really appreciate it if you could make it as simple as possible. Code convert from VBScript to Sharp C#:
6
1537
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 button(ProcPaymBTM_Click) on the web page(Detail View) on clicking this button all selected (checked) data needs to be stored temperorily and displayed on another page (Review Data) In this page i click confirm payment button to insert selected data to the...
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9152
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.