Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. handling events fired by other threads

handling events fired by other threads

Scheduled Pinned Locked Moved C#
helpquestionalgorithmsbusiness
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Martin23
    wrote on last edited by
    #1

    Hi, I have a class running on a secondary thread that listens to an IRC channel (don't worry if you aren't familiar with IRC, all this code is working), and fires an event when it receives a message, passing on this message on. The problem is that I need the main thread to do all the business once a message is received. Attaching events in the normal manner causes the code fired by the event to be run on the secondary thread, which is not good. So basically; how can I attach an event handler that runs on my main thread to events fired on a secondary thread? Hope that all makes sense, I have done lots of searching but cannot find what I'm looking for, this is partly because I'm not really sure what I am looking for, as I am totally stuck. thanks Martin

    S 1 Reply Last reply
    0
    • M Martin23

      Hi, I have a class running on a secondary thread that listens to an IRC channel (don't worry if you aren't familiar with IRC, all this code is working), and fires an event when it receives a message, passing on this message on. The problem is that I need the main thread to do all the business once a message is received. Attaching events in the normal manner causes the code fired by the event to be run on the secondary thread, which is not good. So basically; how can I attach an event handler that runs on my main thread to events fired on a secondary thread? Hope that all makes sense, I have done lots of searching but cannot find what I'm looking for, this is partly because I'm not really sure what I am looking for, as I am totally stuck. thanks Martin

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      AFAIK there's no other way than doing the following inside each event handler.

      private void _Click(object sender, EventArgs e)
      {
      if (this.InvokeRequired)
      {
      this.Invoke(new EventHandler(this._Click), new object[] { sender, e });
      return;
      }

      // Handle the event
      }


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      M 1 Reply Last reply
      0
      • S Stefan Troschuetz

        AFAIK there's no other way than doing the following inside each event handler.

        private void _Click(object sender, EventArgs e)
        {
        if (this.InvokeRequired)
        {
        this.Invoke(new EventHandler(this._Click), new object[] { sender, e });
        return;
        }

        // Handle the event
        }


        "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

        www.troschuetz.de

        M Offline
        M Offline
        Martin23
        wrote on last edited by
        #3

        ahh that's great, works a treat, thanks.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups