How to create a separate thread to process events?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • poldoga
    New Member
    • Aug 2008
    • 4

    How to create a separate thread to process events?

    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.

    Code:
    void MyEventHandler (object s, customeventargs e)
    {
     Processor p = new Processor(e);
     Thread t = new Thread ( new Threadstart(p.process));
     t.start();
    }
    
    class Processor 
    {
    customeventargs  sd;
    public Processor(customeventargs args)
         {
         sd = args;
         }
    
    public Process
         {
         //Perform operation on sd
         }
    }
    Can anybody help me or at the very least point me in the right direction? Thanks!
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Serial ports throw events.
    You don't need to poll it.
    You can use the .PinChange or DataReceived events.

    Comment

    • poldoga
      New Member
      • Aug 2008
      • 4

      #3
      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!

      Comment

      Working...