Connecting Tech Pros Worldwide Help | Site Map

How to create a separate thread to process events?

Newbie
 
Join Date: Aug 2008
Location: Philippines
Posts: 4
#1: 3 Weeks Ago
Here is my problem. I have a thread which constantly polls a piece of hardware's serial output. Every time it detects a certain serial sequence, it triggers an event handler wherein I have code which processes said sequence.

The problem is that the processing is quite time intensive. I noticed that when the program is in the event handler, the hardware is not being polled. If possible, I would like the processing and polling to happen concurrently (in separate threads). This is because due to the time spent solely in the event handler, I am missing some hardware events as I have to poll the piece of hardware as often as possible. By spending time exclusively in the processor method, I am effectively reducing the duty cycle of my hardware. I can afford some concurrence since the hardware has some amount of latency. I issue a command and some time will pass until it is ready and it triggers the event which initiates the processing.

I've tried creating a thread to process the serial data, but its not working. What happens is that when the code exits the MyEventHandler method, it seems to dispose of the passed data hence making the new thread useless.

Expand|Select|Wrap|Line Numbers
  1. void MyEventHandler (object s, customeventargs e)
  2. {
  3.  Processor p = new Processor(e);
  4.  Thread t = new Thread ( new Threadstart(p.process));
  5.  t.start();
  6. }
  7.  
  8. class Processor 
  9. {
  10. customeventargs  sd;
  11. public Processor(customeventargs args)
  12.      {
  13.      sd = args;
  14.      }
  15.  
  16. public Process
  17.      {
  18.      //Perform operation on sd
  19.      }
  20. }
  21.  
Can anybody help me or at the very least point me in the right direction? Thanks!
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#2: 3 Weeks Ago

re: How to create a separate thread to process events?


Serial ports throw events.
You don't need to poll it.
You can use the .PinChange or DataReceived events.
Newbie
 
Join Date: Aug 2008
Location: Philippines
Posts: 4
#3: 2 Weeks Ago

re: How to create a separate thread to process events?


The thing is I kinda need to poll the serialport. After every time it returns data, I need to run code in the original polling thread to basically reboot it. Lastly, I kinda need to poll it since the data doesnt come all at once. The serialdata doesnt all arrive at one time - sometimes the hardware will take up to five data dumps before its complete. This made it a pain before when I was simply using serialdata events to trigger processing. The thread which handles the polling performs basic data filtering and assembly. When a complete set of data is retrieved, the polling thread itself then issues a custom event.

What I just want to happen is to be able to create a new processing thread when the polling thread issues an event... Is there any way to do this? Thanks!
Reply

Tags
event handling, events, synchronization, threads