473,320 Members | 2,000 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,320 software developers and data experts.

"Cross-thread operation not valid" even when .InvokeRequired is false

I'm getting a "Cross-thread operation not valid" Exception and I can't
figure out why.

I've got a BackgroundWorker that creates a UserControl with a whole lot
of other user controls inside of it, full of data I'm getting from the
database. When my background worker completes I take e.Result and cast
it back into a UserControl. I create a TabPage, add the returned
UserControl to the Controls of the new TabPage and *BAM* there's my
exception.

Just before I add the UserControl to the new TabPage's Controls I've
looked at the InvokeRequired properties for the UserControl, the new
TabPage and even my main form and they're all false, leading me to
believe that I should't expect to see such a cross-thread operation.

What gives?

Jan 19 '06 #1
1 9453
Mesan <93*****@gmail.com> wrote:
I'm getting a "Cross-thread operation not valid" Exception and I can't
figure out why.

I've got a BackgroundWorker that creates a UserControl with a whole lot
of other user controls inside of it, full of data I'm getting from the
database. When my background worker completes I take e.Result and cast
it back into a UserControl. I create a TabPage, add the returned
UserControl to the Controls of the new TabPage and *BAM* there's my
exception.

Just before I add the UserControl to the new TabPage's Controls I've
looked at the InvokeRequired properties for the UserControl, the new
TabPage and even my main form and they're all false, leading me to
believe that I should't expect to see such a cross-thread operation.

What gives?


Well, you should never have a control in a Window which "belongs" to a
different thread. The control itself "belongs" to the worker thread -
and although it sounds like InvokeRequired called on the UserControl
should really return true, even if you used Invoke you wouldn't solve
the problem as you'd then be in the wrong thread from the form's point
of view.

My *guess* is that the reason InvokeRequired isn't spotting the problem
is that the worker thread isn't running a message pump - but that's
only a guess.

Just create all the controls on your main UI thread, doing the heavy
non-UI-related work on another thread, and you should be fine.

--
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
Jan 19 '06 #2

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

Similar topics

2
by: matt | last post by:
Hi all- I'm trying to port an ajax spell-checker (http://www.broken-notebook.com/spell_checker/index.php) to use with the moin moin wiki and have been somewhat successful. (By successful I...
0
by: Walter Quirtmair | last post by:
Hello, I have a C# WinForms Application that contains various "old" ActiveX-Controls. Due to some unknown reasons recently the "red cross" - problems appears quite often. Without any know...
4
by: J Fisk | last post by:
Hi, I've been banging my head on the wall over this for about two days now so any thoughts are much appreciated. I have a static .svg file with embedded onclick="open()"'s all over. The svg...
289
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
4
by: Mau Kae Horng | last post by:
Hello, I have a C# Windows Forms application for machine. Due to some unknown reasons, the application face problems with unexpected exceptions happening, resulting in two red lines forming a...
11
by: taoberly | last post by:
A few months ago I posted a question about using a file on my hard drive to perform cross-frame scripting and pull data from a server on my company's intranet. I eventually got this working using...
26
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
19
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - I have <a href="javascript:somefunction()"what ... ?...
0
by: newtechiebug | last post by:
I know that Transform and Pivot are not recognized commands in SQL and I have looked everywhere on how to covert the below query to run properly in SQL, but I can't seem to find the answer. If...
1
by: Luis Freitas | last post by:
I have a table with the following Fields: Article and 8 fields that are Dates. Under each date is a quantity. I would like to run a query that would have only 3 columns: Article, Date and...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.