1

Possible Duplicate:
Why am I getting this error:“Cross-thread operation not valid: Control lbFolders accessed from a thread other than the thread it was created on.”?
Invoke or BeginInvoke cannot be called on a control until the window handle has been created

I have problem with threads.

I am fetching data from COM port every 600ms and when I'm in debug mode I am getting error like after few seconds but when I am in non-debug running mode I am getting error after minute or two but error persist.

The error that I am getting is occurring on some of the functions that are called on each thread.

Here is the picture of the error:

ERROR

Other part of the code:

private void LoadData()
        {
            while (td.IsAlive)
            {
                eng.GetSpeedKmh();
                eng.GetEngineRpm();
                eng.GetCalculatedEngineLoadValue();
                eng.GetFuelLevelInput();
                eng.GetEngineTemp();
                eng.MAF();

                System.Threading.Thread.Sleep(600);
            }
        }

What can I do to fix this?

Community
  • 1
  • 1
user123_456
  • 5,635
  • 26
  • 84
  • 140

2 Answers2

2

try below code

public void eng_OnGetSpeedDone(OBDIIEngineEventArgs args)
        {
            if (this.InvokeRequired)
            {
                Action action = () => eng_OnGetSpeedDone(args);
                Invoke(action);
                return;
            }
            if (!args.OBDResultNoData)
                brzina_ele.Text = arg.OBDValue.ToString();
            else
                brzina_ele.Text = "0";

        }
Neeraj Kaushik
  • 354
  • 1
  • 5
  • 20
  • can you explain me this code please – user123_456 Aug 28 '12 at 15:11
  • Because Winforms works on STA so cross thread operation should be marshal to main UI thread to paint on UI.... InvokeRequired is property of UI controls helps to check if call is cross thread. To marshal cross thread call you can use Invoke or BeginInvoke method, which will marshal call to UI thread...Invoke method expect delegate as parameter and delegate should be same signature as your method... I used Action delegate with lambda expressions to create instance of delegate..and then called Invoke method to call same method on UI thread. – Neeraj Kaushik Aug 29 '12 at 17:41
  • If you want operation asynchronously then you can use BeginInvoke instead of Invoke. – Neeraj Kaushik Aug 29 '12 at 17:46
  • and which will work better for me? BeginInvoke or Invoke? btw. now I don't have that error so that helped. – user123_456 Aug 29 '12 at 19:22
-1

Invoke the editing of the .Text property:

this.Invoke((MethodInvoker) delegate { brzina_ele.Text = "Its new value"; });

You could also do the calls from a Windows.Forms.Timer by using it's Tick event. This is more thread-safe, and has everything you need.

Diamondo25
  • 769
  • 1
  • 8
  • 21
  • -1: Your answer is a very limited copy of previous answer of NeerajKaushik., who presented a reusable "bouncemeback" approach. You have tried to add an another option, but what you propose is not very usable - look at the problem: he uses COM port and the code he presented is a async-like callback "On - GetSpeed - Done", so probably he has a bgthread with blocking read with timeout. "OBD" also suggests that. Suggesting to turn the async into a UI-bound callback (thus, polling that would block the UI) doesn't seem very reasonable for me.. – quetzalcoatl Aug 28 '12 at 12:38