Solved

Help with Cross-thread operation not valid

Posted on 2009-04-10
5
577 Views
Last Modified: 2012-05-06
I have a simple application that is monitoring a folder in VB.NET.
When a file is dropped into the folder I want the buttons on the form to become enabled. When I try to access the buttons on the form to enable them I receive the following error message.

Cross-thread operation not valid: Control 'btnExtractXML' accessed from a thread other than the thread it was created on.






Sub onChanged(ByVal from As Object, ByVal e As FileSystemEventArgs) Handles watcher.Created
 

       btnExtractXML.Enabled = False

       txtPickFile.Enabled = True

       btnPickFile.Enabled = True

       btnRun.Enabled = True
 

    End Sub

Open in new window

0
Comment
Question by:ross13
  • 4
5 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 24114827
How to solve "Cross thread operation not valid"
http://www.codeproject.com/KB/recipes/AccessControlFromThread.aspx

How can I update my user interface from a thread that did not create it?
http://blogs.msdn.com/csharpfaq/archive/2004/03/17/91685.aspx

Updating the UI from a Secondary Thread
http://msdn.microsoft.com/en-us/magazine/cc188732.aspx
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24114830
The BackgroundWorker class makes this kind of thing very simple. It does all the marshalling to the UI thread for you.

http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx


To use winforms control from separate thread you should call Control.Invoke method. In your case this will be TextBox.Invoke

 

The code may look like this:

 

delegate void OutputUpdateDelegate(string data);

public void UpdateTextBox(string data)

{

if ( txtOutput.InvokeRequired )

  txtOutput.Invoke(new OutputUpdateDelegate(OutputUpdateCallback),

  new object[] { data });

else

  OutputUpdateCallback(data); //call directly

}

 

private void OutputUpdateCallback(string data)

{

 txtOutput.Text += data;

}

 

UpdateTextBox method can be called from other threads 

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24114832
You can't access the form's controls through other threads than the one who created it. You can fix it by calling invoke.

Example of using an invoke and delegate can be found here:
How can I update my user interface from a thread that did not create it?
http://blogs.msdn.com/csharpfaq/archive/2004/03/17/91685.aspx

Update UI the easy way using anonymous delegates
http://staceyw.spaces.live.com/blog/cns!F4A38E96E598161E!652.entry
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24114834
Perhaps you should take a look at this article: Updating the UI from a Secondary Thread
http://msdn.microsoft.com/en-us/magazine/cc188732.aspx
http://weblogs.asp.net/justin_rogers/articles/126345.aspx
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24116267
It looks like your FileSystemWatcher is called "watcher":

    Sub onChanged(ByVal from As Object, ByVal e As FileSystemEventArgs) Handles watcher.Created

How and where from are you creating it?  What is the entry point into your application?  Can you show us some code?


0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now