[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2467
  • Last Modified:

Cross Thread operation not valid

I have written a program with multiple threads, the two main ones are the UI threads and the Background worker thread running communications to a serialPort using a IO.MemoryStream...
The problem I am running into is that I am getting the a Cross Thread Operation not valid exception. I have researched this problem but all seem to be refering to assing a value to a control on the UI. I am actually doing the opposite. I need to access the SelectedItem function of a combobox Here is a sample of the code.
//cboSweep is an array of ComboBoxes on my Form.
readMessage.sweep = (short) (cboSweep[chnum - 1].SelectedIndex + 1);

Open in new window

0
EAK31640GW
Asked:
EAK31640GW
  • 4
  • 3
  • 2
  • +1
1 Solution
 
eyal_mtCommented:
It doesn't matter if you assign to a control or read from a control - you should not access the UI controls from any thread other than the UI thread.
But you do have the Invoke method that allows you to do just that.
You can have a look here as an example:
http://www.yoda.arachsys.com/csharp/threads/winforms.shtml
although there are many posts on using the Invoke.
In your case since you need a "return value" you would probably want the invoked method to leave the result in some thread safe variable that both threads can access. A simple way of doing that is to create a static property on one of your classes write to it from the Invoked code, and read it from the Background worker thread.

0
 
abelCommented:
I recently came across this and that in earlier .NET this happened quite often and was then due to a bug in .NET, not in your code. If you are certain that the error is wrong and that you are right, you can disable this error. Quote from Microsoft:

You can disable this exception by setting the value of the CheckForIllegalCrossThreadCalls property to false. This causes your control to run the same way as it would run under Visual Studio 2003.
At the other hand, the same page also says that you should really try your best to resolve the error before you are going to ignore it, because it is a large chance it is correct:

This exception occurs reliably during debugging and, under some circumstances, at run time. You are strongly advised to fix this problem when you see it. You might see this exception when you debug applications that you wrote with the .NET Framework prior to .NET Framework version 2.0.
To help you with it we would need a part of your code to make the problem reproducible. These errors are sometimes hard to correct and a reproducible example would be great.

-- Abel --
0
 
abelCommented:
Forgot the reference: http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx

but it looks like eyal_mt is right on it, too ;-)
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
eyal_mtCommented:
BTW, not sure how the whole code looks like - but a simpler option may be to use one of the onchange events to write the interesting data you need to a shared "safe threaded" variable. This way you will always have the current data without having to access the UI controls.
0
 
coagantusCommented:
add the following line into your constructor... hope this will help u...


Control.CheckForIllegalCrossThreadCalls = false;
0
 
abelCommented:
my point exactly ;)
0
 
eyal_mtCommented:
I would really advise against using Control.CheckForIllegalCrossThreadCalls = false;
The only reason it's there is for debugging purposes, this post talks exactly about why you're not supposed to do that:
http://discuss.joelonsoftware.com/default.asp?dotnet.12.460561.4
0
 
EAK31640GWAuthor Commented:
Thank you all for this info. eval_mt your first link was right on! As was the Static property idea. I have started implementing the logic within my code and everything works as I would like it to except I am getting a compile error that my delegate must declare a body because it is not marked as abstract, extern, pr partial.
What am I missing???

Here is the code:

   delegate void GetSelectedIndexDelegate(int index);
    private static int selectedIndex; 
 
	public virtual bool readChannel(int chnum)
	{
        GetSelectedIndex(chnum);
 
        lock (StateLock)
        {
            readMessage.sweep = (short)SelectedIndex;
        }
 
}
 
  private void GetSelectedIndex(int index)
    {
        if (this.cboSweep[index].InvokeRequired)
        {
 
            Invoke(new GetSelectedIndexDelegate(GetSelectedIndex), new object[] {index});
            return;
        }
        lock (StateLock)
        {
            LoggerReadingPanel.SelectedIndex= cboSweep[index].SelectedIndex;
        }
    }
 
    private static int SelectedIndex
    {
        get
        {
            return selectedIndex;
        }
        set
        {
            selectedIndex = value;
        }
 
    }

Open in new window

0
 
eyal_mtCommented:
I'm not seeing anything that would generate that error in this code.
Is this all the code in the class?
There's a return statement missing from readChannel
and your delegate definition is missing an explicit access level (private I assume)- but that wouldn't give this particular error...
0
 
EAK31640GWAuthor Commented:
No The class is a windows form and has many functions having to do with events on the form.
The function readChannel does return a value later on based off a returned value from another call to read bytes from a IO.Message stream and update labels on the form. I only placed code relavent to the delegate and CrossThreading issue.
Yes the delegate is private.
The error comes from the infamous squiggly blue line under GetSelectedIndexDelegate in the delegate declaration statement.
The whole error is:
Error      1      'LoggerReadingPanel.GetSelectedIndexDelegate(int)' must declare a body because it is not marked abstract, extern, or partial      C:\Documents and Settings\swintern\My Documents\projects\VWQL.NET\VWQuattroLoggerUI\LoggerReadingPanel.cs      15      18      VWQuattroLoggerUI

I too am at a loss, as the only thing I can find in reference to this type off error deals with Auto-Implemented properties...
I'm using Visual Studio 2008 running my code on MixedPlatform... Ok hold on I just switched to run on any CPU and the error went away and now compiles!
Weird!@
This program is a Java conversion using the MS java to C# Converter, which aparently set it to Mixed Platform mode. I had never checked that until now...
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now