Safe thread calls
-
Hi, I have an event registered to receive serial port data. to my understanding this is done in a separate thread than the one registered in. Ok now in my serial port data receive event i take the data generate a message and send to a windows message queue. method to implement sending is a static method in a separate class library. Problems. When data is received and the static method is called , suddenly a new thread is generated and it keeps on doing that for each data received even though i havent spawned any new thread. i have taken the method from the library pasted it in my current working class scope and the same thing happens. but if i write the sending stuff in the event itself this doesnot happen and everything works just fine. So to my understanding a different thread is accessing a different thread so there could be safety issues or something else. Do i need to make some kind of asynchronous calls or any other solution please. I want the function to remain in class library outside the scope of this class. Can anyone help me with this matter. Thank you!!! in advance.
-
Hi, I have an event registered to receive serial port data. to my understanding this is done in a separate thread than the one registered in. Ok now in my serial port data receive event i take the data generate a message and send to a windows message queue. method to implement sending is a static method in a separate class library. Problems. When data is received and the static method is called , suddenly a new thread is generated and it keeps on doing that for each data received even though i havent spawned any new thread. i have taken the method from the library pasted it in my current working class scope and the same thing happens. but if i write the sending stuff in the event itself this doesnot happen and everything works just fine. So to my understanding a different thread is accessing a different thread so there could be safety issues or something else. Do i need to make some kind of asynchronous calls or any other solution please. I want the function to remain in class library outside the scope of this class. Can anyone help me with this matter. Thank you!!! in advance.
Hi, the DataReceived and ErrorReceived events fire on a ThreadPool thread. AFAIK it is always one and the same thread for any given serial port. You do not need to create yet another thread to handle the data; when you want to access some data structures (maybe a collection), you would need thread synchronization, most likely a simple lock would suffice. And when you would like to access some GUI Controls, you would need a Control.InvokeRequired/Control.Invoke pattern.
static
is irrelevant in this context. if your code keeps creating new threads, that does not make sense. you do not need to add any asynchronous calls. if you want specific help, post specific information and (part of) actual code. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Getting an article published on CodeProject should be easier and faster.
-
Hi, the DataReceived and ErrorReceived events fire on a ThreadPool thread. AFAIK it is always one and the same thread for any given serial port. You do not need to create yet another thread to handle the data; when you want to access some data structures (maybe a collection), you would need thread synchronization, most likely a simple lock would suffice. And when you would like to access some GUI Controls, you would need a Control.InvokeRequired/Control.Invoke pattern.
static
is irrelevant in this context. if your code keeps creating new threads, that does not make sense. you do not need to add any asynchronous calls. if you want specific help, post specific information and (part of) actual code. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Getting an article published on CodeProject should be easier and faster.
I understand your point clearly... this code is a part of a windows service... Please find the code below. This is the serial data received event(part of a running windows service). Where i just called a static event and passing in some event arguments (custom made)
private void serialCnn_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{// Send received data to data processor for parsing and filtering EventReporter.SendData(new SendSerialDataArguments { SerialPortName = ((SerialPort)sender).PortName, Source = Entity.SerialServer, Destination = Entity.DataProcessor, SourceServer = Entity.SerialServer, TransmittedDataType = TransmittedDataType.RawProcessed, Data = new Message(((SerialPort)sender).ReadTo("\\r")) }); /\* msq.Send("pinki", MessageQueueTransactionType.Single); msq.Close(); msq.Dispose(); \*/ } catch (Exception ex) { // Report error to event log this.MiscFunctions.WriteToLog(Entity.SerialServer, ex); } }
The method called is a static method in a class library reference by this windows service. The method is:
public static void SendData(SendSerialDataArguments args) { try { // Add message formatter property args.Data.Formatter = new BinaryMessageFormatter(); // Add a label to message args.Data.Label = ((int)args.Source).ToString() + ":" + ((int)args.Destination).ToString() + ":" + ((int)TransmittedMessageType.Data).ToString() + ":" + ((int)args.SourceServer).ToString() + ":" + ((int)args.TransmittedDataType).ToString() + ":" + args.SerialPortName; // Enable message to persist to disk rather than memory args.Data.Recoverable = true; // Check destination for message, choose queue accordingly switch (args.Destination) { case Entity.Application: { // Send message with custom label and current transaction referenc
-
I understand your point clearly... this code is a part of a windows service... Please find the code below. This is the serial data received event(part of a running windows service). Where i just called a static event and passing in some event arguments (custom made)
private void serialCnn_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{// Send received data to data processor for parsing and filtering EventReporter.SendData(new SendSerialDataArguments { SerialPortName = ((SerialPort)sender).PortName, Source = Entity.SerialServer, Destination = Entity.DataProcessor, SourceServer = Entity.SerialServer, TransmittedDataType = TransmittedDataType.RawProcessed, Data = new Message(((SerialPort)sender).ReadTo("\\r")) }); /\* msq.Send("pinki", MessageQueueTransactionType.Single); msq.Close(); msq.Dispose(); \*/ } catch (Exception ex) { // Report error to event log this.MiscFunctions.WriteToLog(Entity.SerialServer, ex); } }
The method called is a static method in a class library reference by this windows service. The method is:
public static void SendData(SendSerialDataArguments args) { try { // Add message formatter property args.Data.Formatter = new BinaryMessageFormatter(); // Add a label to message args.Data.Label = ((int)args.Source).ToString() + ":" + ((int)args.Destination).ToString() + ":" + ((int)TransmittedMessageType.Data).ToString() + ":" + ((int)args.SourceServer).ToString() + ":" + ((int)args.TransmittedDataType).ToString() + ":" + args.SerialPortName; // Enable message to persist to disk rather than memory args.Data.Recoverable = true; // Check destination for message, choose queue accordingly switch (args.Destination) { case Entity.Application: { // Send message with custom label and current transaction referenc
Hi, 1. Threading I don't see anything about threading at all. Hence I don't think there would be a threading problem. 2. static Your explanation isn't clear at all; there are two simple schemes regarding static: A. don't make anything static (possible exception: a method that does not need any class member data could be made static to enforce that) B. make everything static (that is impossible if you need two or more of something) If you try to have a half-static approach, every method declared static can only access class members that are static themselves. That is all that is to it. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Getting an article published on CodeProject should be easier and faster.
-
Hi, 1. Threading I don't see anything about threading at all. Hence I don't think there would be a threading problem. 2. static Your explanation isn't clear at all; there are two simple schemes regarding static: A. don't make anything static (possible exception: a method that does not need any class member data could be made static to enforce that) B. make everything static (that is impossible if you need two or more of something) If you try to have a half-static approach, every method declared static can only access class members that are static themselves. That is all that is to it. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Getting an article published on CodeProject should be easier and faster.
Again i understand ure explanation clearly... There isn't suppose to be any new thread spawned and doing something static doesn't matter either. But the thing odd is that if i do the same thing(i.e. sending messages to msq in the serial port data received event, this doesn't result in the continuous spawning of new threads. Nothing seems to add up y new threads are created if i just take the same code and place it in a class library rather than in the serialport data received event. i have commented the code in the serial port data received event. which i executed instead of the event reporter event and everything went f9. May be there is some problem with this serial port data received thread accessing methods outside its thread.perhaps. again any further solutions invited.
-
Hi, I have an event registered to receive serial port data. to my understanding this is done in a separate thread than the one registered in. Ok now in my serial port data receive event i take the data generate a message and send to a windows message queue. method to implement sending is a static method in a separate class library. Problems. When data is received and the static method is called , suddenly a new thread is generated and it keeps on doing that for each data received even though i havent spawned any new thread. i have taken the method from the library pasted it in my current working class scope and the same thing happens. but if i write the sending stuff in the event itself this doesnot happen and everything works just fine. So to my understanding a different thread is accessing a different thread so there could be safety issues or something else. Do i need to make some kind of asynchronous calls or any other solution please. I want the function to remain in class library outside the scope of this class. Can anyone help me with this matter. Thank you!!! in advance.
-
Hi, Is there any chance that you are blocking the serial port data received thread? If this were to happen I assume that subsequent events would have to use a different thread. Alan.
That will surely do that... but in the debug mode i can see my control returning to the data received event, if that can be checked this way. + enlighten me like async functions which uses the threadpool limited to 25 threads, saturating on 25 threads if used. If according to u it uses the thread pool and if there is a a cap on it it must also saturate. but i have seen the threads go up to 500 and keeps on increasing. + i think so i should use an async function to do the rest of the work (sending to msgq). if it ties up the thread that would be freed perhaps and it may not require another thread for next data received event. perhaps. Again what do u say... Really thanks on answering all of my questions grateful..
-
Hi, I have an event registered to receive serial port data. to my understanding this is done in a separate thread than the one registered in. Ok now in my serial port data receive event i take the data generate a message and send to a windows message queue. method to implement sending is a static method in a separate class library. Problems. When data is received and the static method is called , suddenly a new thread is generated and it keeps on doing that for each data received even though i havent spawned any new thread. i have taken the method from the library pasted it in my current working class scope and the same thing happens. but if i write the sending stuff in the event itself this doesnot happen and everything works just fine. So to my understanding a different thread is accessing a different thread so there could be safety issues or something else. Do i need to make some kind of asynchronous calls or any other solution please. I want the function to remain in class library outside the scope of this class. Can anyone help me with this matter. Thank you!!! in advance.
Found the solution.... I tried everything even async method calls. nothing got me to the solution. finally i just mistakenly changed the serialport.ReadTo("\r") to ReadExisting() and it solved the thread problem. Dont know till now y That ReadTo problem acted like that . :P