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: - namespace biometric_device_app
-
{
-
public partial class Form1 : Form
-
{
-
public AsyncCallback pfnWorkerCallBack;
-
public Socket m_socListener;
-
public Socket m_socWorker;
-
-
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
-
Thread MainProgramThread;
-
String entry = "";
-
int toggle = 0;
-
-
private void flashing(object sender, EventArgs e)
-
{
-
-
if (toggle == 0)
-
{
-
lbl_login.Text = "PRESS FINGER TO LOGIN";
-
toggle = 1;
-
}
-
else
-
{
-
lbl_login.Text = "";
-
toggle = 0;
-
}
-
}
-
-
-
-
-
private void main_program()
-
{
-
-
-
while (true)
-
{
-
Thread.Sleep(500);
-
switch (current_operation)
-
{
-
-
-
case 0: // for finger print recognition
-
-
try
-
{
-
-
txt_show.Text = "hello";
-
-
if (!serialPort1.IsOpen)
-
serialPort1.Open();
-
serialPort1.ReadTimeout = 10000;
-
int flag_0 = 0;
-
int flag_no_match_0 = 1;
-
byte[] value_search = new byte[28];
-
byte[] info_0 = { 0x02, 0x00, 0x08, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xc6, 0x03 };
-
byte[] device_getnumb_0 = { 2, 0, 8, 0, 50, 77, 66, 73, 68 };
-
byte[] no_match_0 = { 2, 0, 6, 57, 50 };
-
again: serialPort1.Write(info_0, 0, 13);
-
//Thread.Sleep(200); // 100 ms Delay after sending command
-
serialPort1.Read(value_search, 0, 1);
-
Thread.Sleep(5);
-
serialPort1.Read(value_search, 1, 12);
-
for (int b = 0; b < 9; b++)
-
{
-
if (!(value_search[b] == device_getnumb_0[b]))
-
{
-
flag_0 = 1;
-
break;
-
}
-
else
-
flag_0 = 0;
-
}
-
-
if (flag_0 == 1)
-
{
-
for (int b = 0; b < 5; b++)
-
{
-
if (!(value_search[b] == no_match_0[b]))
-
{
-
flag_no_match_0 = 1;
-
break;
-
}
-
else
-
flag_no_match_0 = 0;
-
}
-
}
-
/*
-
while (flag == 1)
-
{
-
Thread.Sleep(200);
-
goto again;
-
}
-
*/
-
-
-
-
-
if (flag_0 == 0)
-
{
-
int num = value_search[9] * 256 + value_search[10];
-
txt_show.Text = "EMPLOYEE NUMBER " + num + " LOGED IN";
-
-
if (rad_btn_in.Checked)
-
{
-
entry = "IN";
-
rad_btn_in.Checked = true;
-
}
-
else if (rad_btn_out.Checked)
-
{
-
entry = "OUT";
-
rad_btn_out.Checked = true;
-
}
-
else ;
-
-
/*
-
while (!((rad_btn_in.Checked) || (rad_btn_out.Checked)))
-
Thread.Sleep(100);
-
*/
-
-
StreamWriter rd = new StreamWriter(@"My Documents\Attendence.att", true);
-
rd.WriteLine("LOGIN-----------" + "#" + "Employee Number: " + num + "#" + entry + "#" + DateTime.Now.ToString());
-
rd.Close();
-
}
-
-
else if (flag_no_match_0 == 0)
-
txt_show.Text = "NO MATCH FOUND";
-
-
-
value_search = null;
-
serialPort1.Close();
-
flag_no_match_0 = 1;
-
timer1.Enabled = true;
-
}
-
catch (Exception ex)
-
{
-
MessageBox.Show(ex.Message);
-
}
-
-
Thread.Sleep(1000);
-
-
-
break;
-
-
-
-
-
-
case 1: // for enroll
-
current_operation = 0;
-
break;
-
-
case 2: // for lan recv
-
current_operation = 0;
-
break;
-
-
-
}
-
}
-
}
-
-
-
-
-
private void Form1_Load(object sender, EventArgs e)
-
{
-
-
MainProgramThread = new Thread(new ThreadStart(main_program));
-
MainProgramThread.Start();
-
}
4 2807 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.
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. - void DoSomethingMethod()
-
{
-
Invoke((MethodInvoker)delegate
-
{
-
// actually do the "something"
-
});
-
}
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 :)
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: pms |
last post by:
Need suggestions to improve this program.
using System;
namespace CalculateWage
{
class GetEmployeeDetails
{
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
| |