473,780 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cross Thread Communications

Hi everyone,

I have a Socket derived class that parses the received data into a message,
populates a object with that information and then tries to send an event to
inform the owner of the socket that there is message to be processed. But
this is not working as the socket receiver method seems to be in a different
thread then the form that owns the socket.

The receive callback gets the data from the socket and process the
information, once it constructs a full message from it then it calls a
delegate if the delegate is set.

The method that handles this event needs to change some controls on the form
and when I do that I get the exception:
Cross-thread operation not valid: Control 'syncButton' accessed from a
thread other than the thread it was created on.

I would appreciate any suggestions on solving this.

class LSSocket : Socket
{
public event ProcessMessageH andler ProcessMessageE vent;

public void OnReceiveData(I AsyncResult)
{
....
if (ProcessMessage Event != null)
{
ProcessMessageE vent(this, new ProcessMessageA rgs(Msg));
}
}
}

class ProcessMessageA rgs : System.EventArg s
{
public ProcessMessageA rgs(LSMessage msg)
{
this.msg = msg;
}
public LSMessage msg;
}
delegate void ProcessMessageH andler(object sender, ProcessMessageA rgs
e);

I would have had to use PostThreadMessa ge in native windows development, but
I can't figure out what I need to use in this situation.

AliR.
Aug 5 '08 #1
6 2376
On Aug 5, 12:48 pm, "AliR \(VC++ MVP\)" <A...@online.no spamwrote:
Hi everyone,

I have a Socket derived class that parses the received data into a message,
populates a object with that information and then tries to send an event to
inform the owner of the socket that there is message to be processed. But
this is not working as the socket receiver method seems to be in a different
thread then the form that owns the socket.

The receive callback gets the data from the socket and process the
information, once it constructs a full message from it then it calls a
delegate if the delegate is set.

The method that handles this event needs to change some controls on the form
and when I do that I get the exception:
Cross-thread operation not valid: Control 'syncButton' accessed from a
thread other than the thread it was created on.

I would appreciate any suggestions on solving this.

class LSSocket : Socket
{
public event ProcessMessageH andler ProcessMessageE vent;

public void OnReceiveData(I AsyncResult)
{
....
if (ProcessMessage Event != null)
{
ProcessMessageE vent(this, new ProcessMessageA rgs(Msg));
}
}
}

class ProcessMessageA rgs : System.EventArg s
{
public ProcessMessageA rgs(LSMessage msg)
{
this.msg = msg;
}
public LSMessage msg;
}
delegate void ProcessMessageH andler(object sender, ProcessMessageA rgs
e);

I would have had to use PostThreadMessa ge in native windows development, but
I can't figure out what I need to use in this situation.

AliR.
Use Control.Invoke to execute the event in the UI thread.
Aug 5 '08 #2
"Ignacio Machin ( .NET/ C# MVP )" <ig************ @gmail.comwrote in
message
news:1f******** *************** ***********@m44 g2000hsc.google groups.com...
On Aug 5, 12:48 pm, "AliR \(VC++ MVP\)" <A...@online.no spamwrote:
>Hi everyone,

I have a Socket derived class that parses the received data into a
message,
populates a object with that information and then tries to send an event
to
inform the owner of the socket that there is message to be processed.
But
this is not working as the socket receiver method seems to be in a
different
thread then the form that owns the socket.

The receive callback gets the data from the socket and process the
information, once it constructs a full message from it then it calls a
delegate if the delegate is set.

The method that handles this event needs to change some controls on the
form
and when I do that I get the exception:
Cross-thread operation not valid: Control 'syncButton' accessed from a
thread other than the thread it was created on.

I would appreciate any suggestions on solving this.

class LSSocket : Socket
{
public event ProcessMessageH andler ProcessMessageE vent;

public void OnReceiveData(I AsyncResult)
{
....
if (ProcessMessage Event != null)
{
ProcessMessageE vent(this, new
ProcessMessage Args(Msg));
}
}
}

class ProcessMessageA rgs : System.EventArg s
{
public ProcessMessageA rgs(LSMessage msg)
{
this.msg = msg;
}
public LSMessage msg;
}
delegate void ProcessMessageH andler(object sender, ProcessMessageA rgs
e);

I would have had to use PostThreadMessa ge in native windows development,
but
I can't figure out what I need to use in this situation.

AliR.

Use Control.Invoke to execute the event in the UI thread.
I looked at Control.Invoke, but wasn't really sure where it would apply. A
little more explanation would be great.

Are you saying that I have to use that in event hander method in my form
class to do the UI manipulation? Or am I supposed to use it in place of the
event hander call? Or am I supposed to call the event handler delegate
using the Invoke method?

Here is the event handler method:
public void ProcessMessage( object sender, ProcessMessageA rgs e)
{
switch (e.msg.m_Comman d)
{
case MSG_SERVERIP: ServerIP = e.msg.m_Address ;
ServerPort = 29308;
syncButton.Enab led = true;
break;
default: Debug.Print("Oh Oh");
break;
}
}

AliR
Aug 5 '08 #3
"AliR (VC++ MVP)" <Al**@online.no spamwrote in message
news:QW******** ***********@nlp i061.nbdc.sbc.c om...
"Ignacio Machin ( .NET/ C# MVP )" <ig************ @gmail.comwrote in
message
news:1f******** *************** ***********@m44 g2000hsc.google groups.com...
>On Aug 5, 12:48 pm, "AliR \(VC++ MVP\)" <A...@online.no spamwrote:
>>Hi everyone,

I have a Socket derived class that parses the received data into a
message,
populates a object with that information and then tries to send an event
to
inform the owner of the socket that there is message to be processed.
But
this is not working as the socket receiver method seems to be in a
different
thread then the form that owns the socket.

The receive callback gets the data from the socket and process the
information , once it constructs a full message from it then it calls a
delegate if the delegate is set.

The method that handles this event needs to change some controls on the
form
and when I do that I get the exception:
Cross-thread operation not valid: Control 'syncButton' accessed from a
thread other than the thread it was created on.

I would appreciate any suggestions on solving this.

class LSSocket : Socket
{
public event ProcessMessageH andler ProcessMessageE vent;

public void OnReceiveData(I AsyncResult)
{
....
if (ProcessMessage Event != null)
{
ProcessMessageE vent(this, new
ProcessMessag eArgs(Msg));
}
}
}

class ProcessMessageA rgs : System.EventArg s
{
public ProcessMessageA rgs(LSMessage msg)
{
this.msg = msg;
}
public LSMessage msg;
}
delegate void ProcessMessageH andler(object sender,
ProcessMessag eArgs
e);

I would have had to use PostThreadMessa ge in native windows development,
but
I can't figure out what I need to use in this situation.

AliR.

Use Control.Invoke to execute the event in the UI thread.

I looked at Control.Invoke, but wasn't really sure where it would apply.
A little more explanation would be great.

Are you saying that I have to use that in event hander method in my form
class to do the UI manipulation? Or am I supposed to use it in place of
the event hander call? Or am I supposed to call the event handler
delegate using the Invoke method?

Here is the event handler method:
public void ProcessMessage( object sender, ProcessMessageA rgs e)
{
switch (e.msg.m_Comman d)
{
case MSG_SERVERIP: ServerIP = e.msg.m_Address ;
ServerPort = 29308;
syncButton.Enab led = true;
break;
default: Debug.Print("Oh Oh");
break;
}
}

AliR
I think I got it. (Correctly or not I don't know.)
Beside the event member, I added a parent form object to the class of type
Control.

delegate void ProcessMessageH andler(LSMessag e Msg);
class LSSocket : Socket
{
public event ProcessMessageH andler ProcessMessageE vent;
public Form ParentForm = null;

public void OnReceiveData(I AsyncResult)
{
//pares data into a LSMessage class (Msg)
....
if (ProcessMessage Event != null && ParentForm != null)
{
// ProcessMessageE vent(this, new
ProcessMessageA rgs(Msg));
ParentForm.Invo ke(ProcessMessa geEvent,Msg);
}
}
.....
}

Hopefully this is the right way of doing it.

AliR.
Aug 5 '08 #4
On Tue, 05 Aug 2008 11:25:58 -0700, AliR (VC++ MVP) <Al**@online.no spam>
wrote:
[...]
public void ProcessMessage( object sender, ProcessMessageA rgs e)
{
switch (e.msg.m_Comman d)
{
case MSG_SERVERIP: ServerIP = e.msg.m_Address ;
ServerPort = 29308;
syncButton.Enab led = true;
break;
default: Debug.Print("Oh Oh");
break;
}
}
Try:

public void ProcessMessage( object sender, ProcessMessageA rgs e)
{
switch (e.msg.m_Comman d)
{
case MSG_SERVERIP: ServerIP = e.msg.m_Address ;
ServerPort = 29308;
syncButton.Invo ke((MethodInvok er)delegate {
syncButton.Enab led = true } );
break;
default: Debug.Print("Oh Oh");
break;
}
}

Not only does that avoid having to track a specific parent control in your
event-raising class, IMHO it's better for the GUI-specific stuff to stay
in the GUI-specific class.

Pete
Aug 5 '08 #5

"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Tue, 05 Aug 2008 11:25:58 -0700, AliR (VC++ MVP) <Al**@online.no spam>
wrote:
>[...]
public void ProcessMessage( object sender, ProcessMessageA rgs e)
{
switch (e.msg.m_Comman d)
{
case MSG_SERVERIP: ServerIP = e.msg.m_Address ;
ServerPort = 29308;
syncButton.Enab led = true;
break;
default: Debug.Print("Oh Oh");
break;
}
}

Try:

public void ProcessMessage( object sender, ProcessMessageA rgs e)
{
switch (e.msg.m_Comman d)
{
case MSG_SERVERIP: ServerIP = e.msg.m_Address ;
ServerPort = 29308;
syncButton.Invo ke((MethodInvok er)delegate {
syncButton.Enab led = true } );
break;
default: Debug.Print("Oh Oh");
break;
}
}

Not only does that avoid having to track a specific parent control in your
event-raising class, IMHO it's better for the GUI-specific stuff to stay
in the GUI-specific class.

Pete
Very cool, thank you. I had no idea how to use Invoke! As you can tell
I'm a newbee.

AliR.
Aug 5 '08 #6
On Aug 5, 2:25 pm, "AliR \(VC++ MVP\)" <A...@online.no spamwrote:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mac... @gmail.comwrote in
messagenews:1f* *************** *************** ***@m44g2000hsc .googlegroups.c om...
On Aug 5, 12:48 pm, "AliR \(VC++ MVP\)" <A...@online.no spamwrote:
Hi everyone,
I have a Socket derived class that parses the received data into a
message,
populates a object with that information and then tries to send an event
to
inform the owner of the socket that there is message to be processed.
But
this is not working as the socket receiver method seems to be in a
different
thread then the form that owns the socket.
The receive callback gets the data from the socket and process the
information, once it constructs a full message from it then it calls a
delegate if the delegate is set.
The method that handles this event needs to change some controls on the
form
and when I do that I get the exception:
Cross-thread operation not valid: Control 'syncButton' accessed from a
thread other than the thread it was created on.
I would appreciate any suggestions on solving this.
class LSSocket : Socket
{
public event ProcessMessageH andler ProcessMessageE vent;
public void OnReceiveData(I AsyncResult)
{
....
if (ProcessMessage Event != null)
{
ProcessMessageE vent(this, new
ProcessMessageA rgs(Msg));
}
}
}
class ProcessMessageA rgs : System.EventArg s
{
public ProcessMessageA rgs(LSMessage msg)
{
this.msg = msg;
}
public LSMessage msg;
}
delegate void ProcessMessageH andler(object sender, ProcessMessageA rgs
e);
I would have had to use PostThreadMessa ge in native windows development,
but
I can't figure out what I need to use in this situation.
AliR.
Use Control.Invoke to execute the event in the UI thread.

I looked at Control.Invoke, but wasn't really sure where it would apply. A
little more explanation would be great.

Are you saying that I have to use that in event hander method in my form
class to do the UI manipulation?
Yes, in windows forms the controls should only be accessed from the
UI thread. You can make an event to be executed in the UI thread (or
more exactly in the control's creation thread, that in the current
implementation is the UI) by using Control.Invoke.

Invoke what does is put a message in the message pump that will be
executed as any other message in a win. app.
Aug 5 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
8688
by: HairlipDog58 | last post by:
Hello, There are several 'cross-thread operation not valid' exception postings in the MSDN discussion groups, but none address my exact situation. I have authored a .NET component in Visual C# .NET 2003 that is capable of firing several types of events to notify user of errors, data changes, etc. The component uses a thread to perform background communications and if there is an error or data-change fires an event to notify the user....
3
1710
by: TClancey | last post by:
I'm confused! Probably becuase I know little about threads, and really should. But... I have a serial port control, when it reads data I want to update several things, database fields and tables, also a text box. Everything works fine apart from updating the text box where I get this error. I have other parts of code that update text boxes, list boxes all sorts of controls that don't cause this error, even updating text boxes on...
13
11288
by: LordHog | last post by:
Hello all, I have a little application that needs to poll a device (CAN communications) every 10 to 15 ms otherwise the hardware buffer might overflow when there are message burst on the bus. I would implement an interrupt driven model, but the external device (which is connected via USB) does not support interrupts therefore the device needs to be polled at a specific interval. So my question is how can I implement a "deterministic"...
11
3796
by: Jon Slaughter | last post by:
Is there any way to start a terminated thread without using a pool or creating a new thread object? void counter() { clicks = 0; clock.Start(); while (counterActive) { clicks++;
0
9636
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
9474
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
10306
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9931
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8961
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
7485
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
6727
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
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.