CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Cross-Threading

  1. #1
    Join Date
    Jan 2009
    Posts
    9

    Cross-Threading

    Im a bit confused with sending information between threads.

    I have a thread thats constantly checking a UDP port for incoming packets. When a packet arrive i need it to send the packet to one of several different classes thats run by other threads that decodes the information.

    Im experiencing an issue where the program stops recieving packets while its decoding which lead me to believe that the UDP-thread also do the decoding.

    Right now im using an event to send the packet:

    public event PacketToProcessDelegate OnPacketToProcess;

    and i use it:

    if (recvBytes != 0)
    {
    // Decode incoming packet
    OnPacketToProcess(dataBuffer);
    }


    So my question is: On what thread is the event handled?

    Thanks!

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Cross-Threading

    It depends on how things are set up.

    Try displaying a trace message for the thread id to see if the event handler matches your current thread id. Use the System.Thread.CurrentThread.ManagedThreadId property.

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    Re: Cross-Threading

    If it's a standard event with standard event handlers, then yes, it's the same thread.

    Code:
    public event EventHandler MyEvent;
    
    public void Method ()
    {
        // This event handler will be executed on the same thread as invocation in 'Whatever'
        MyEvent += delegate { Console.WriteLine ("I'm on the same thread"); };
    }
    
    public void Whatever ()
    {
        // Raise 'MyEvent' on the same thread
        MyEvent (this, EventArgs.Empty);
    
        // Raise 'MyEvent' on a different thread
        Threadpool.QueueUserWorkDelegate (delegate {
            MyEvent (this, EventArgs.Empty);
        });
    }
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured