473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

I don't get this --> Make Thread-Safe Calls to Windows Forms Controls

ljh
I am trying to run some pretty simple code that monitors a folder for
changes (copied the example at
http://www.codeproject.com/dotnet/folderwatcher.asp).

But, when I try and update a textbox control I keep getting the
"Cross-thread operation not valid: Control 'txt_folderactivity' accessed
from a thread other than the thread it was created on." error.

I found the Microsoft explanation at
http://msdn2.microsoft.com/en-us/lib...80,d=ide).aspx, but I
just don't get the explanation.

Can somebody help me understand why I can't simply set the text property
from a thread and what this Microsoft explanation is trying to tell me?

Thanks!
Jun 16 '06 #1
4 6820
"ljh" <Re***@groups.please> schrieb:
I am trying to run some pretty simple code that monitors a folder for
changes (copied the example at
http://www.codeproject.com/dotnet/folderwatcher.asp).

But, when I try and update a textbox control I keep getting the
"Cross-thread operation not valid: Control 'txt_folderactivity' accessed
from a thread other than the thread it was created on." error.


Set the filesystemwatcher's 'SynchronizingObject' property to the textbox.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Jun 16 '06 #2
ljh
Sweet!

Thanks Herfried!
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uc**************@TK2MSFTNGP05.phx.gbl...
"ljh" <Re***@groups.please> schrieb:
I am trying to run some pretty simple code that monitors a folder for
changes (copied the example at
http://www.codeproject.com/dotnet/folderwatcher.asp).

But, when I try and update a textbox control I keep getting the
"Cross-thread operation not valid: Control 'txt_folderactivity' accessed
from a thread other than the thread it was created on." error.


Set the filesystemwatcher's 'SynchronizingObject' property to the textbox.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Jun 16 '06 #3
Windows forms and controls can only be accessed from the thread they
were created on. That is the main UI thread. The FileSystemWatcher
events come from some other thread. The cross-thread operation error
you see is coming from a Managed Debugging Assistant (MDA) which is
nice enough to warn you of the potential problem in debug builds. MDAs
are not compiled into release builds. They are a new feature in 2.0.

Setting the SynchronizingObject property to a Form or Control in your
application tells the FileSystemWatcher to automatically marshal the
execution of events onto the thread that created that form or control.

ljh wrote:
I am trying to run some pretty simple code that monitors a folder for
changes (copied the example at
http://www.codeproject.com/dotnet/folderwatcher.asp).

But, when I try and update a textbox control I keep getting the
"Cross-thread operation not valid: Control 'txt_folderactivity' accessed
from a thread other than the thread it was created on." error.

I found the Microsoft explanation at
http://msdn2.microsoft.com/en-us/lib...80,d=ide).aspx, but I
just don't get the explanation.

Can somebody help me understand why I can't simply set the text property
from a thread and what this Microsoft explanation is trying to tell me?

Thanks!


Jun 16 '06 #4
ljh
Thanks for the explanation....

Now I am finding out all of the GOTCHAs associated with using the
FileSystemWatcher control (see the
"FileSystemWatcher raises Changed Twice...." thread for all of the gory
details).
"Brian Gideon" <br*********@yahoo.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Windows forms and controls can only be accessed from the thread they
were created on. That is the main UI thread. The FileSystemWatcher
events come from some other thread. The cross-thread operation error
you see is coming from a Managed Debugging Assistant (MDA) which is
nice enough to warn you of the potential problem in debug builds. MDAs
are not compiled into release builds. They are a new feature in 2.0.

Setting the SynchronizingObject property to a Form or Control in your
application tells the FileSystemWatcher to automatically marshal the
execution of events onto the thread that created that form or control.

ljh wrote:
I am trying to run some pretty simple code that monitors a folder for
changes (copied the example at
http://www.codeproject.com/dotnet/folderwatcher.asp).

But, when I try and update a textbox control I keep getting the
"Cross-thread operation not valid: Control 'txt_folderactivity' accessed
from a thread other than the thread it was created on." error.

I found the Microsoft explanation at
http://msdn2.microsoft.com/en-us/lib...80,d=ide).aspx, but
I
just don't get the explanation.

Can somebody help me understand why I can't simply set the text property
from a thread and what this Microsoft explanation is trying to tell me?

Thanks!

Jun 16 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John Blair | last post by:
Hi, I have one form that i want to make similar to another form but add some additional controls. I thought the following code would make my webform4 have all the content of webform3 but nothing...
3
by: Gene Hubert | last post by:
Is there a way to Visually Design Windows Forms Controls? For example, I have a class that inherits TextBox. Can I see a property sheet and a rendition of what it will look like as I develop...
4
by: Douglas Peterson | last post by:
I created the following code: private struct StackItem { public EventHandler theEvent, theHandler; public StackItem(EventHandler theEvent, EventHandler theHandler) { this.theEvent = theEvent;...
1
by: Gustavo | last post by:
Hi, We are placing Windows Controls inside webforms for a project that needs to interact with files from a local computer. What would be the best way to make the following work: When a user...
2
by: mustafa.rabie | last post by:
Dear all, I am new to the .net 3.0 development. I am currently trying to develop a WPF (XAML) application using VS 2005 + Expressions blend. I created the window added buttons and managed all...
2
by: =?Utf-8?B?bGFyc2dyZWdlcnNlbg==?= | last post by:
Hi I have created a couple of Windows Forms controls in C# either by composition or by subclassing existing controls. I'm able to use these controls as ActiveX controls by using "register for...
2
by: RSH | last post by:
I have been experimenting with overriding the Paint event on Win Form controls...more specifically trying to force the text to render as Antialiased text. I have not been successful in any of the...
3
by: Andy B | last post by:
Is there any way to use .net windows forms controls inside asp.net pages and not require that the client have .net access? I'm more the less thinking of mobile devices that don't have access to, or...
2
by: Andy B | last post by:
Is there any way to use .net windows forms controls inside asp.net pages and not require that the client have .net access? I'm more the less thinking of mobile devices that don't have access to, or...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.