473,385 Members | 1,569 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,385 software developers and data experts.

Backgroundworker 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 backgroundworker 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 2392
"Johnny Jörgensen" <jo**@altcom.seschrieb
Hi I have a procudure that takes some time and thus slows down the
main system. I want to put it in a backgroundworker 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*******@freenet.deskrev i meddelandet
news:uc**************@TK2MSFTNGP06.phx.gbl...
"Johnny Jörgensen" <jo**@altcom.seschrieb
>Hi I have a procudure that takes some time and thus slows down the
main system. I want to put it in a backgroundworker 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 backgroundworker 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
(ProgressChanged, RunWorkerCompleted, 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.sewrote:
Hi I have a procudure that takes some time and thus slows down the main
system. I want to put it in a backgroundworker 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
checkforillegalcrosscall = 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
checkforillegalcrosscall = 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 BackgroundWorker 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
checkforillegalcrosscall = 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.com>
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...@yahoo.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
Jon,

Snip
>The whole point of the exception is to show that you're doing something
wrong.
I won't tell it in this way, more "There is something going wrong". By
instance when a server goes down then this can be catched during the proces.
Better in my eyes it is a must to catch this, however that has nothing to do
with "That you are doing something wrong""

I know that it is just an expression, however in my eyes should this be used
more for the exception instead of catching program errors, what I have seen
is done to much. The last in fact in the same way as Peter writes this.

Cor

Aug 2 '07 #11
Cor Ligthert[MVP] <no************@planet.nlwrote:
Snip
The whole point of the exception is to show that you're doing something
wrong.

I won't tell it in this way, more "There is something going wrong". By
instance when a server goes down then this can be catched during the proces.
Better in my eyes it is a must to catch this, however that has nothing to do
with "That you are doing something wrong""

I know that it is just an expression, however in my eyes should this be used
more for the exception instead of catching program errors, what I have seen
is done to much. The last in fact in the same way as Peter writes this.
No, this exception is *specifically* aimed at developers to tell them
that they're doing something wrong. It's not there to let you do clean-
up etc - it's pointing out a bug.

That's why by default it only occurs in debug mode. If it were there to
allow clean-up etc in the case of failure, it would happen in release
mode too.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 2 '07 #12
Cor Ligthert[MVP] wrote:
Jon,

Snip
>The whole point of the exception is to show that you're doing something
wrong.

I won't tell it in this way, more "There is something going wrong". By
instance when a server goes down then this can be catched during the
proces. Better in my eyes it is a must to catch this, however that has
nothing to do with "That you are doing something wrong""
Perhaps what you write makes sense for exceptions generally. However,
this particular exception very much is about "you're doing something
wrong". That is, it doesn't catch an actual "something going wrong".
The code may in fact work much of the time, even when this exception occurs.

The MDA exception is in fact specifically saying "you are doing
something wrong".

Pete
Aug 2 '07 #13
Jon and Peter,

My excuse, I was only looking at your replies.

Cor

"Jon Skeet [C# MVP]" <sk***@pobox.comschreef in bericht
news:MP*********************@msnews.microsoft.com. ..
Cor Ligthert[MVP] <no************@planet.nlwrote:
>Snip
>The whole point of the exception is to show that you're doing something
wrong.

I won't tell it in this way, more "There is something going wrong". By
instance when a server goes down then this can be catched during the
proces.
Better in my eyes it is a must to catch this, however that has nothing to
do
with "That you are doing something wrong""

I know that it is just an expression, however in my eyes should this be
used
more for the exception instead of catching program errors, what I have
seen
is done to much. The last in fact in the same way as Peter writes this.

No, this exception is *specifically* aimed at developers to tell them
that they're doing something wrong. It's not there to let you do clean-
up etc - it's pointing out a bug.

That's why by default it only occurs in debug mode. If it were there to
allow clean-up etc in the case of failure, it would happen in release
mode too.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 2 '07 #14

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

Similar topics

2
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...
2
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...
5
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...
3
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...
14
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...
9
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...
4
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...
3
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...
4
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...
0
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.