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 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. 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. - void DoSomethingMethod()
-
{
-
Invoke((MethodInvoker)delegate
-
{
-
// actually do the "something"
-
});
-
}
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 :)
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
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 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...
|
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.*;
|
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#.
|
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...
|
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,
| |
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
|
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 would really appreciate it if you could make it as simple as possible.
Code convert from VBScript to Sharp C#:
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |