Tech Off Thread

9 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Events across threads

Back to Forum: Tech Off
  • User profile image
    GurliGebis

    Hey.

    I'm having a problem.

    I have an event, that I subscribe to from a WinForm.
    When I call the event from another thread, it fails, since it cannot access the winforms thread.

    So, how do I invoke an event, so that everyone thats subscribed to it gets it invoked, so it works across threads?

  • User profile image
    Minh

    Let's see some code & your error messages.

  • User profile image
    GurliGebis

    Not really easy to paste some code, since it's really complicated.
    But the Form subscribes to an event, and the event is fired from another thread.
    So the question really is: "How do I fire an event, if some of the classes that has subscribed to it is Forms, so it doesn't throw a cross thread execption?"

  • User profile image
    Minh

    Sounds like you're using .Net 2.0 --- which looks out for you by throwing an exception when you're trying to manipulate a UI element from a different thread other than the UI thread.

    I haven't played much with .Net 2.0, so my solution is going to be 1.0-ish.

    What you can do is -- your form has an Invoke method, so just call that from your event and you should be OK.

  • User profile image
    phreaks

    Pretty sure all he needs is a publicly accessable delegate to pass the events off to the UI.

    If he is using a BGWorker, you can pretty easily pass the events around.


    EDIT: something Like this (not tested)
    //almost forgot.


    //In your threaded class

    public event MyThreadedClass.MyCompletedEventHandler ItsCompleted ;

    public delegate void MyCompletedEventHandler(object sender, RunWorkerCompletedEventArgs e);  //Delegate

    //In UI Class

    MyThreadedClass.ItsCompleted += new MyCompletedEventHandler(frmMain_SomethingCompleted);

    void frmMain_SomethingCompleted(object sender, RunWorkerCompletedEventArgs e)
    {

    }

    //I think thats right Perplexed

  • User profile image
    Ion Todirel

    or the simplest possible way Smiley

    // the method called by other thread, this method is in UIs thread
    private void OnSomething(object sender, System.SomeEventArgs e)
    {
        if (this.InvokeRequired) 
           Invoke(new MethodInvoker(delegate() { OnSomething(sender, e); }));

       // your code here
    }

  • User profile image
    Minh

    phreaks wrote:
    

    //I think thats right Perplexed

    Actually, no.

    This would only work if your event was raised by a BackgroundWorker component.

    Otherwise, you'll have to do something like what Ion suggests.

  • User profile image
    phreaks

    Minh wrote:
    
    phreaks wrote: 

    //I think thats right Perplexed

    Actually, no.

    This would only work if your event was raised by a BackgroundWorker component.

    Otherwise, you'll have to do something like what Ion suggests.


    That's why I said, "If he is using a BGWorker,"...  Expressionless

  • User profile image
    Ion Todirel

    hey, something strange just happened, don't understand why,

    if you run (F5) this code inside Visual Studio, you get an "InvalidOperationException", Cross Thread ...:

    public partial class Form1 : Form
    {
        System.Timers.Timer timer = new System.Timers.Timer(3000);

        public Form1()
       {
           InitializeComponent();

           timer.AutoReset = true;
           timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
           timer.Start();
       }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
       {
           textBox1.Text = "sd";
       }
    }

    but if you run the generated assembly from Debug or Release folder exception does not occur, why? anyone

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.