473,748 Members | 9,931 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Backgroundworke r and controls

Hi I have a procudure that takes some time and thus slows down the main
system. I want to put it in a backgroundworke r component to run it
asynchroneously . But in the procedure, I want to update a TreeView control I
have on my main form.

When I try to do that, I get the error "Cross-thread operation not valid:
Control 'TreeView1' accessed from a thread other than the thread it was
created on"

I seem to have read something about that poblem before, but can't remember
if there's a solution to it...

Please advice asap,
Johnny J.
Aug 1 '07 #1
13 2418
"Johnny Jörgensen" <jo**@altcom.se schrieb
Hi I have a procudure that takes some time and thus slows down the
main system. I want to put it in a backgroundworke r component to run
it asynchroneously . But in the procedure, I want to update a
TreeView control I have on my main form.

When I try to do that, I get the error "Cross-thread operation not
valid: Control 'TreeView1' accessed from a thread other than the
thread it was created on"

I seem to have read something about that poblem before, but can't
remember if there's a solution to it...

Please advice asap,
Johnny J.

Call the control's BeginInvoke function ([F1] for details). The invoked
function will run in the thread that created the control, which is allowed
to access it.
Armin

Aug 1 '07 #2
But won't that be the same thread as my main thread???

/Johnny J.

"Armin Zingler" <az*******@free net.deskrev i meddelandet
news:uc******** ******@TK2MSFTN GP06.phx.gbl...
"Johnny Jörgensen" <jo**@altcom.se schrieb
>Hi I have a procudure that takes some time and thus slows down the
main system. I want to put it in a backgroundworke r component to run
it asynchroneously . But in the procedure, I want to update a
TreeView control I have on my main form.

When I try to do that, I get the error "Cross-thread operation not
valid: Control 'TreeView1' accessed from a thread other than the
thread it was created on"

I seem to have read something about that poblem before, but can't
remember if there's a solution to it...

Please advice asap,
Johnny J.


Call the control's BeginInvoke function ([F1] for details). The invoked
function will run in the thread that created the control, which is allowed
to access it.
Armin

Aug 1 '07 #3
But won't that be the same thread as my main thread???

Yes.

The problem is that the background worker's thread is not your main
thread, which is why you need to use BeginInvoke to delegate control
back to the main thread.

Thanks,

Seth Rowe

Aug 1 '07 #4
Johnny Jörgensen wrote:
Hi I have a procudure that takes some time and thus slows down the main
system. I want to put it in a backgroundworke r component to run it
asynchroneously . But in the procedure, I want to update a TreeView control I
have on my main form.

When I try to do that, I get the error "Cross-thread operation not valid:
Control 'TreeView1' accessed from a thread other than the thread it was
created on"
But the whole point of the Background worker is that it raises events
(ProgressChange d, RunWorkerComple ted, etc) as the background job proceeds.

Most importantly, these events are raised /on the UI thread/, so you can
update Controls any way you like.

HTH,
Phill W.
Aug 1 '07 #5
On 1 ago, 07:01, "Johnny Jörgensen" <j...@altcom.se wrote:
Hi I have a procudure that takes some time and thus slows down the main
system. I want to put it in a backgroundworke r component to run it
asynchroneously . But in the procedure, I want to update a TreeView control I
have on my main form.

When I try to do that, I get the error "Cross-thread operation not valid:
Control 'TreeView1' accessed from a thread other than the thread it was
created on"

I seem to have read something about that poblem before, but can't remember
if there's a solution to it...

Please advice asap,
Johnny J.
Put in the a place before you call the background compontent to start
checkforillegal crosscall = false //im not in front the vs but the
property is something like that

check that the doesnt fire only on debug

Aug 1 '07 #6
Horacio Nuñez Hernández wrote:
Put in the a place before you call the background compontent to start
checkforillegal crosscall = false //im not in front the vs but the
property is something like that
No. Do NOT do that. All that does is turn off the error detection.
The error still exists.

The first reply from Armin was the best: use BeginInvoke() (or Invoke())
to run code that updates the UI. Yes, that will cause the code (just
the code in the invoked method) to run in the main thread. That's the
whole point, and is exactly what needs to be done.

Pete
Aug 1 '07 #7
Johnny Jörgensen wrote:
But won't that be the same thread as my main thread???
Yes, it will. That's the point.

To be clear: only the code that does the actual updating of the control
needs to be called via Invoke() or BeginInvoke(). Armin isn't
suggesting that you call _all_ of the BackgroundWorke r code via
Invoke(). Just the code that needs to manipulate your UI elements, such
as the TreeView control being updated.

Pete
Aug 1 '07 #8
Horacio Nuñez Hernández <hn******@gmail .comwrote:
Put in the a place before you call the background compontent to start
checkforillegal crosscall = false //im not in front the vs but the
property is something like that

check that the doesnt fire only on debug
The whole point of the exception is to show that you're doing something
wrong. You shouldn't access a control from any thread other than the
one running its message loop.

Setting the property to false just hides the problem temporarily
instead of solving it.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 1 '07 #9
On Aug 1, 4:47 am, rowe_newsgroups <rowe_em...@yah oo.comwrote:
But won't that be the same thread as my main thread???

Yes.

The problem is that the background worker's thread is not your main
thread, which is why you need to use BeginInvoke to delegate control
back to the main thread.
.... and just to be clear, if you want to call a member of your
control, you *must* do it on your UI ("main") thread. More precisely,
you must call members of a control from the thread on which that
control was created. This is a requirement specific to WinForms
controls.

More information here:
http://msdn2.microsoft.com/en-us/library/3s8xdz5c.aspx

(technically, it's not a requirement, just a really, really good idea,
one that's enforced by a VisualStudio MDA exception. Windows controls
are not thread-safe in that way, and you'll get unpredictable,
intermittent errors if you try to work around it.)

Michael

Aug 1 '07 #10

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

Similar topics

2
9275
by: dm1608 | last post by:
Hi -- I have a C# application that basically has a button that executes a SQL Reader to loop thru a rather large resul set. Thru each interation of the reader object, I check to see if a file exists on the server. If it does, I update a textbox (multiline) with the missing file, update a status bar label, and a progressbar. Since this process is kind of lengthy, every 400 rows, I do a DoEvents() and TextBox.Refersh() to refresh the...
2
8745
by: Sebastian Crewe | last post by:
Greetings, I was much encouraged to see the new BackgroundWorker class in .NET v2. On the face of it, much easier to use than the various delegates and events of yore, though I imagine the same base classes are being invoked. Anyway, I have given it a try with a VB.NET application that generates charts. Since there are some 3,000 charts to do, I wanted to put the chart generation on a separate thread. Works fine inasmuch as the...
5
14134
by: Rob R. Ainscough | last post by:
I'm using a BackgroundWorker to perform a file download from an ftp site. Per good code design practices where I separate my UI code from my core logic code (in this case my Download file method in my FileIO class) I've established Public Event in my core logic classes along with RaiseEvents (that will updated a progress bar on the UI side). This all works great when I'm NOT using Threading (BackgroundWorker), however, as soon as I...
3
3054
by: Hardy Wang | last post by:
Hi all, I am migrating a Windows Form application from .Net 1.1 to 2.0. I try to use BackgroundWorker object to handle a very lengthy process. I have a separated class to handle some very complex logic. In .Net 1.1, I create some events inside this class to notify WinForm the status of process: obj.OneFileStarted += new OneFileStartedEventHandler(obj_OneFileStarted); obj.OneFileFinished += new...
14
6392
by: =?Utf-8?B?SXNobWFlbA==?= | last post by:
Hi, I have a form with a progress bar on it and wanted to use the BackgroundWorker to be able to update the progress. I looked at examples, run some of them, but in debug, when the code gets to the do work method, it stops for a long while and then executes the first line of the method and the doesn't do anything else. What am i doing wrong?
9
18029
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form will almost always lead to the user opening a certain child window that's fairly resource intensive to load. Is it possible to load this form in a backgroundworker and then use the Show method and hide method as necessary? Anyone know of
4
2583
by: Sin Jeong-hun | last post by:
This is what I've always been wondered. Suppose I've created a class named Agent, and the Agent does some lengthy job. Of course I don't want to block the main window, so the Agent does the job in a separate thread. If the job is progressed it fires an event, and the main window handled the event by changing the value of a progress bar. The problem is that this event is fired in another thread so when the handler in the main window tries...
3
2040
by: Rotsey | last post by:
Hi, Anyone able to look at this code with backgroundworker. I am calling a FindDuplicates class and need to pass in the DriveData class that is passed into the form. So it is necessary to pass in to RunWorkerAsync the DriveData object or can I just access it from the form level variable.???
4
4802
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi misters, I have winForm that needs update Panel (add controls to Panel of SplitContainer dynamically). I have a Backgroundworker, and DoWork event I want to add controls but I get this error in Spanish (I don't get translation: Los controles creados en un subproceso no pueden tener controles primarios en un control en un subproceso diferente.)
0
1898
by: PeterSchwennesen | last post by:
Problems starting a Timer Programmatically within a BackgroundWorker. I am trying to start a Timer inside a Backgroundworker. I want to start the BackGroundWorker and then have a timer tick a calculation reppeting inside the BackGroundWorker. The Code below seems to work ok for sending back the informaton "DoWork - Enter" and "DoWork - Exit, but the Timer "Timer_Ping" is newer ticked and the line "Time - Tick" is newer send back. Why is my...
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9374
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9325
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.