Cross-thread operation after using EventHandler

127 views Asked by At

I needed to create an event in another class, so I've used an EventHandler and this works fine, however now in my main class form, when I try to use any controls I get:

Cross-thread operation not valid

So after researching I can Invoke:

tbMessage.Invoke((Action)delegate
{
      tbMessage.Size = new System.Drawing.Size(500, 60);
      this.Controls.Add(tbMessage);
});

However if I do this all over the form, there would be loads of instances, where, I'm moving, resizing, changing text for loads of controls. This doesn't seem right, there must be something to do with using the EventHandle that I don't know about. These errors didn't occur before moving some code out into new class and using task and events.

I am unsubscribing from the event after finished with and before

dvm.VoltageSet -= dvmVoltageSet;

Any ideas please?

EDIT:

I am using a background worker to update UI, whilst receiving serial commands in Form1. In another class I am using Task.Run to set voltage + read voltage whilst I'm not at set voltage. Then I'm using EventHandler to send an event to Form1 so it know's the voltage had been set before proceeding to next step.

RE-EDIT:

I can now see that I have two Windows.Forms threads, which is causing my error. This appears to be happening when I call a method in another class, when it causes an event in form1 and I try to use a control, it then starts a new thread.

Not Flagged >   4432    8   Worker Thread   Worker Thread   System.Windows.Forms.dll!System.Windows.Forms.Control.Handle.get    Normal
                    [External Code]  
                    TestSensor.exe!TestSensor.Form1.TestLvdtNull() Line 3113     
                    TestSensor.exe!TestSensor.Form1.dvmVoltageSet(object sender, VoltageEventArgs e) Line 414    
                    TestSensor.exe!TestSensor.DVMLoopRunner.OnNewVoltageSet(VoltageEventArgs e) Line 71  
                    TestSensor.exe!TestSensor.DVMLoopRunner.NewSet.set(bool value) Line 57   
                    TestSensor.exe!TestSensor.DVMLoopRunner.AdjustVoltage() Line 295     
                    TestSensor.exe!TestSensor.DVMLoopRunner.Form1_DVMReadingAvailable(object sender, TestSensor.DVMLoopRunner.DVMReadingAvailableEventArgs e) Line 147   
                    TestSensor.exe!TestSensor.DVMLoopRunner.ReadDVM() Line 192   
                    TestSensor.exe!TestSensor.DVMLoopRunner.ReadDVMWorker() Line 173     
                    TestSensor.exe!TestSensor.DVMLoopRunner.SetVoltage.AnonymousMethod__49_0() Line 95   
                    [External Code]  

Not Flagged     6768    1   Main Thread Main Thread System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop   Normal
                    [External Code]  
                    TestSensor.exe!TestSensor.Program.Main() Line 17     

Not Flagged     11048   0   Worker Thread   <No Name>       Normal
Not Flagged     668 4   Worker Thread   <No Name>   System.dll!System.IO.Ports.SerialStream.EventLoopRunner.WaitForCommEvent    Normal
Not Flagged     5888    5   Worker Thread   <No Name>   System.dll!System.IO.Ports.SerialStream.EventLoopRunner.WaitForCommEvent    Normal
Not Flagged     12968   0   Worker Thread   <No Name>       Normal
Not Flagged     11924   6   Worker Thread   Worker Thread       Normal

Is it possible to tell the compiler to carry on using the existing Forms thread and not start a new one?

0

There are 0 answers