Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with Cross-thread operation not valid

Posted on 2009-04-10
5
Medium Priority
?
616 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 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 86

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

610 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