473,725 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading problem after migration from 2003 to 2005

We converted our decently large (13 projects, compiled about 12 mb)
VStudio 2003/.Net1.1 project to VStudio 2005/.Net2. My two colleagues
have no problem starting the new solution, but I get an exception
thrown at me. I only get the exception if I run in debug mode, in
release mode all works fine. The exception thrown somewhere along
startup sequence is:
>[ InvalidOperatio nException ]
Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was
created on.
The following snippet shows where the exception is thrown:

----------------------------------------------------------------------
public class PleaseWait : System.Windows. Forms.Form
{
private static PleaseWait fWindow = null;
public static void ShowWindow(stri ng message)
{
if(fWindow == null)
{
fWindow = new PleaseWait();
fWindow.Owner = System.Windows. Forms.Form.Acti veForm;
}
// long i = 0;
// while (i++ < 40000000) ;

fWindow.Show();

// ...etcetera
}
}
--------------------------------------------------------------------------

The ShowWindow method is called from somewhere else in the application
during startup. If I uncomment the counter and let it wait for a
while, no exception is thrown. No exception is thrown either if I
break the code just before the Show instruction and wait a few secs.

I have found articles on this but the problem is I cannot distill a
resolution for the mystery the above poses to me. Can anyone provide
an explanation of what's happening with the above info?

May 10 '07 #1
7 1379
On May 10, 9:49 am, Jeroen <mercu...@gmail .comwrote:
We converted our decently large (13 projects, compiled about 12 mb)
VStudio 2003/.Net1.1 project to VStudio 2005/.Net2. My two colleagues
have no problem starting the new solution, but I get an exception
thrown at me. I only get the exception if I run in debug mode, in
release mode all works fine. The exception thrown somewhere along
startup sequence is:
[ InvalidOperatio nException ]
Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was
created on.

The following snippet shows where the exception is thrown:

----------------------------------------------------------------------
public class PleaseWait : System.Windows. Forms.Form
{
private static PleaseWait fWindow = null;
public static void ShowWindow(stri ng message)
{
if(fWindow == null)
{
fWindow = new PleaseWait();
fWindow.Owner = System.Windows. Forms.Form.Acti veForm;
}
// long i = 0;
// while (i++ < 40000000) ;

fWindow.Show();

// ...etcetera
}}

--------------------------------------------------------------------------

The ShowWindow method is called from somewhere else in the application
during startup. If I uncomment the counter and let it wait for a
while, no exception is thrown. No exception is thrown either if I
break the code just before the Show instruction and wait a few secs.

I have found articles on this but the problem is I cannot distill a
resolution for the mystery the above poses to me. Can anyone provide
an explanation of what's happening with the above info?
Its only fine in release mode because of luck. Controls have thread
affinity, and accessing ANY property or method on a control on a
thread other than the one it was created with will cause problems.

To resolve this problem, check out the Control.Invoke method and
Control.InvokeR equired. You'll see a pattern that will help you fix
the problems.

The reason your colleagues are not getting the exception is because
there's a setting in VS that will turn off that particular exception.
I recommend you have them turn that option ON, because you WILL have
odd problems if you don't fix the offending code.

I assume the code you posted is the one running in a new thread; I
don't see a way for you to NOT run that on the UI thread, especially
without seeing the other code.

HTH
Andy

May 10 '07 #2
Hi,

First of all I see no threading in that piece of code, from where are you
calling this?
IS this beign created in another thread?

Also worth of mention is that declaring a form static and messing with the
owner is not something I advise to do.

"Jeroen" <me******@gmail .comwrote in message
news:11******** **************@ q75g2000hsh.goo glegroups.com.. .
We converted our decently large (13 projects, compiled about 12 mb)
VStudio 2003/.Net1.1 project to VStudio 2005/.Net2. My two colleagues
have no problem starting the new solution, but I get an exception
thrown at me. I only get the exception if I run in debug mode, in
release mode all works fine. The exception thrown somewhere along
startup sequence is:
>>[ InvalidOperatio nException ]
Cross-thread operation not valid: Control '' accessed from a thread
other than the thread it was
created on.

The following snippet shows where the exception is thrown:

----------------------------------------------------------------------
public class PleaseWait : System.Windows. Forms.Form
{
private static PleaseWait fWindow = null;
public static void ShowWindow(stri ng message)
{
if(fWindow == null)
{
fWindow = new PleaseWait();
fWindow.Owner = System.Windows. Forms.Form.Acti veForm;
}
// long i = 0;
// while (i++ < 40000000) ;

fWindow.Show();

// ...etcetera
}
}
--------------------------------------------------------------------------

The ShowWindow method is called from somewhere else in the application
during startup. If I uncomment the counter and let it wait for a
while, no exception is thrown. No exception is thrown either if I
break the code just before the Show instruction and wait a few secs.

I have found articles on this but the problem is I cannot distill a
resolution for the mystery the above poses to me. Can anyone provide
an explanation of what's happening with the above info?

May 10 '07 #3
On May 10, 8:49 am, Jeroen <mercu...@gmail .comwrote:
We converted our decently large (13 projects, compiled about 12 mb)
VStudio 2003/.Net1.1 project to VStudio 2005/.Net2. My two colleagues
have no problem starting the new solution, but I get an exception
thrown at me. I only get the exception if I run in debug mode, in
release mode all works fine. The exception thrown somewhere along
startup sequence is:
[ InvalidOperatio nException ]
Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was
created on.

The following snippet shows where the exception is thrown:

----------------------------------------------------------------------
public class PleaseWait : System.Windows. Forms.Form
{
private static PleaseWait fWindow = null;
public static void ShowWindow(stri ng message)
{
if(fWindow == null)
{
fWindow = new PleaseWait();
fWindow.Owner = System.Windows. Forms.Form.Acti veForm;
}
// long i = 0;
// while (i++ < 40000000) ;

fWindow.Show();

// ...etcetera
}}

--------------------------------------------------------------------------

The ShowWindow method is called from somewhere else in the application
during startup. If I uncomment the counter and let it wait for a
while, no exception is thrown. No exception is thrown either if I
break the code just before the Show instruction and wait a few secs.

I have found articles on this but the problem is I cannot distill a
resolution for the mystery the above poses to me. Can anyone provide
an explanation of what's happening with the above info?
Like Andy said it was working in 1.1 by accident. VS 2005 adds the
managed debugging assistant (MDA) to detect this common problem in
debug builds. Strategically placed calls to Control.Invoke or
Control.BeginIn voke may fix the problem. It's been my experience that
some threading problems require significant architectural changes to
fix so let's hope that's not the case here since you have a large
application.

May 10 '07 #4
Thanks a lot for the response. I'm a little bit further in
understanding this problem. However, it's still not all clear to me;
I'm especially having trouble with the story on "Invoke". The main
problem is that I didn't really get what it was for, until I read the
msdn:
Note: This method is new in the .NET Framework version 2.0.
...
Provides access to properties and methods exposed by an object.
...
Remarks
This method is for access to managed classes from unmanaged
code and should not be called from managed code. For more
information about IDispatch::Invo ke, see the MSDN Library.
My first problem is that some folks here and on blogs explain the
exception I'm getting just as well could have happened on 2003, and
that it's coincidence it only occurs now. Still, "Invoke" wasn't added
until .Net2.0!?

Secondly, I'm only working in managed code, so the msdn remark even
seems to imply I don't need "Invoke".

Any thoughts?

May 14 '07 #5
Wrong Invoke... the correct Invoke here is Control.Invoke, not to be
confused with Delegate.Invoke or IDispatch.Invok e.

This is not new to 2.0, relates to managed code, and although you
*should* have been using it prior to 2.0, the system let you get away
with some [ab]use; from 2.0 it checks a bit more rigorously. This can
be disabled, but I *really* don't recommend it. It is doing this for a
very good reason.

It's purpose is to push a message onto the Form's message pump so that
a unit of work can be done by the UI thread (which has access to the
Controls).

Marc
May 14 '07 #6
On May 14, 7:12 am, Jeroen <mercu...@gmail .comwrote:
Thanks a lot for the response. I'm a little bit further in
understanding this problem. However, it's still not all clear to me;
I'm especially having trouble with the story on "Invoke". The main
problem is that I didn't really get what it was for, until I read the
msdn:
Note: This method is new in the .NET Framework version 2.0.
...
Provides access to properties and methods exposed by an object.
...
Remarks
This method is for access to managed classes from unmanaged
code and should not be called from managed code. For more
information about IDispatch::Invo ke, see the MSDN Library.
I was talking about Control.Invoke which implements
ISynchronizeInv oke.Invoke and has been available since 1.0.
My first problem is that some folks here and on blogs explain the
exception I'm getting just as well could have happened on 2003, and
that it's coincidence it only occurs now. Still, "Invoke" wasn't added
until .Net2.0!?
No, the exact exception you're getting could not be thrown in 1.1
because it is generated from an MDA which is available starting with
2.0. Like Marc said you can disable the MDA, but I too do not
recommend that.

In versions prior to 2.0 you could see a variety of exceptions or no
exception at all. You might observe that the application runs
correctly 99% of time and crashes only intermittently. Different
hardware configuration could make the crash more or less likely. In
the past I've seen a big red X drawn in the place of a control as a
symptom. Either way the problems will be unpredicable.
>
Secondly, I'm only working in managed code, so the msdn remark even
seems to imply I don't need "Invoke".
One thing I can say for certain is that you absolutely cannot access a
Form or Control from a thread other than the main UI thread.
Control.Invoke is usually the best option if you want a worker thread
to initiate some kind of UI activity.
May 14 '07 #7
Control.Invoke is usually the best option if you want a worker thread
to initiate some kind of UI activity.
Just a minor point; for those unfamilair with cross-threading an UI,
the BackgroundWorke r component can serve as a gentle introduction; the
background code calls ReportProgress, and *separately* your UI handles
the ProgressChanged event - usually updating a ProgressBar; this
handles all the thread-switching for you, similar to how
Windows.Forms.T imer works.

Of course, once you get the hang of it, direct use of Control.Invoke
offers a lot more flexibility ;-p

Marc

May 14 '07 #8

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

Similar topics

2
2316
by: CK | last post by:
I am a "newbie" to python and today I had the need to write a program which generated a lot of tcp connections to a range of addresses (10.34.32.0/22) in order to troubleshoot a problem with a switch. I also wanted to get familiar with threads under python and so I thought I could do both at the same time. I wrote two programs - one using thread and one using threading but the results between the two were unexpected and quite...
3
1213
by: Shivakumar | last post by:
Hi , In ASP.NET 2.0 the post back is rendered in the following function javascript:WebForm_DoPostBackWithOptions() where is in ASP.NET 1.1 it is rendered in the following function __doPostBack(). Now can anyone tell me can i change the default rendering method as old (1.1) one in ASP.NET 2.0? because i had a project in version 1.1 and i have migrated it to 2.0 using visual studio .NET 2005 but its throwing the following script error...
3
7772
by: cnickl | last post by:
VS 2005 / C++ / __gc program (converted from VS 2003) i have this code in my program: int found; String *temp; this->ofdOpen->ShowDialog(); the last line produces the folloing unhandleld exeption:
6
1400
by: Andre | last post by:
Hi, We had an existing application in ASP.NET 1.1. After migration I tried to utilize MasterPages. The test showed that format for most of labels is treated differently e.g. TextBox'es are much toller, Label's do not have fixed length as they used to, etc... After some testing I discovered that problem is in how page format defined. If I use HTML 4.0 all looking good, if page is XHTML 1.1 it is not. Please advise on how to make pages...
1
3872
by: Bonggoy Cruz | last post by:
We have a fairly big size ASP.NET web application that was written VB.NET. We are in the process converting the web project. We used the migration wizard included in VS 2005. I followed step by step guide outlined in here: http://msdn.microsoft.com/asp.net/reference/migration/upgrade/default.aspx?pull=/library/en-us/dnaspp/html/webprojectsvs05.asp. I was able to convert the project without any problem. Compiling and building them is...
6
1898
by: hzgt9b | last post by:
Using VS 2003, .NET: I developed a windows application that performs several actions based on an input file. The application displays a progress bar as each action executes. Based on new requirements, this application needs to be able to shell off other processes and wait while in the mean time displaying a progress bar of the process's. I am using the System.Disgnostics.Process class to "start" and "waitForExit" of these processes... I...
7
339
by: Phill W. | last post by:
Can anyone recommend a good reference on Threading (Framework 1.1)? I'm particularly confused about how to call a method in my "main" thread from another one. - or more, one day ;-) I've tried creating delegates to main thread methods and Invoking them and BeginInvoking them, but I've had some quite /horrendous/ results ... TIA,
5
10906
by: Miro | last post by:
I will try my best to ask this question correctly. I think in the end the code will make more sence of what I am trying to accomplish. I am just not sure of what to search for on the net. I have a form that has a button. ( this form is a child form of a parent form ( main form ). Anway...in this child form I have a button, and if clicked a bunch of code will get executed. I would like to show a Progress Bar / form in modal/ShowDialog...
2
1457
by: =?Utf-8?B?U2FtdWVsIEJlcm5hcmQ=?= | last post by:
Hello, I start with a project of migration of our Web application from 1.1 to 2.0. Currently we have a deployment tool which load the Web site assembly in order to access methodes containing in each WebForm. In asp.net 2.0 each page is built into it own assembly. So I have to change the way we access the pages now. I will appreciate any help. tia Samuel
0
8889
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
8752
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,...
1
9179
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,...
0
9116
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6011
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
4519
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
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.