Odd problem with C#.NET app: runs fine in Visual Studio but getsstuck when running the EXE directly

A

Andi

I have run into a very odd behavior of a Windows Forms .NET 2.0
application and I'm really hoping that someone here has some ideas of
what else I could look at or try out.

So basically it's an MDI application with about 15 different child
forms that can be opened in the app. There is also an automation
option that performs a whole bunch of tasks automatically and the end
of every task, it brings up the corresponding form to display the
results to the user.

When the automation starts, a new BackgroundWorker is created that
starts the task sequence. Every task itself again runs in its own
BackgroundWorker. So none of the automation tasks runs on the GUI
thread. Whenever a task is done, it calls the 'Show' on the form via
the 'BeginInvoke' on the UI to bring up the form and display the
results (i.e. make sure those calls are done on the UI thread).

Now the interesting thing is that all of this works just fine when I
start the app from within Visual Studio. All the 15 automated tasks
complete and after every task the form is displayed.
However if I run the EXE directly, e.g. by double-clicking it in the
Windows Explorer, then the automation starts fine but it then suddenly
hangs on the second or third task where it tries to show the form.
I've basically narrowed it down to see that in some cases, the actual
Form.Show is just stuck. In my form I overrode the 'Show' and print a
line before the base.Show and print a line afterwards to the log. I
get the printed line before but don't get the printed line afterwards.
So somehow it gets stuck in that call and I have no idea why.

It also doesn't always get stuck at the same place. Sometimes it gets
stuck somewhere else, usually when something's running on the UI
thread.

I'm 100% sure that there are no deadlocks or other synchronization
issues. I've also tried to replace the BackgroundWorker with Thread
but got the same results.
Also in the project settings I've tried to target the build for x86
processors as well as 'Any CPU' but that didn't help either.

Is there something that Visual Studio does fundamentally different
when running an app with regard to the UI thread than when running the
exe by itself?

Any ideas and suggestions are appreciated because at this time I'm
just at a loss as to what the problem could be.

Thanks.
Andi
 
I

Ignacio Machin ( .NET/ C# MVP )

I have run into a very odd behavior of a Windows Forms .NET 2.0
application and I'm really hoping that someone here has some ideas of
what else I could look at or try out.

So basically it's an MDI application with about 15 different child
forms that can be opened in the app. There is also an automation
option that performs a whole bunch of tasks automatically and the end
of every task, it brings up the corresponding form to display the
results to the user.

When the automation starts, a new BackgroundWorker is created that
starts the task sequence. Every task itself again runs in its own
BackgroundWorker. So none of the automation tasks runs on the GUI
thread. Whenever a task is done, it calls the 'Show' on the form via
the 'BeginInvoke' on the UI to bring up the form and display the
results (i.e. make sure those calls are done on the UI thread).

Now the interesting thing is that all of this works just fine when I
start the app from within Visual Studio. All the 15 automated tasks
complete and after every task the form is displayed.
However if I run the EXE directly, e.g. by double-clicking it in the
Windows Explorer, then the automation starts fine but it then suddenly
hangs on the second or third task where it tries to show the form.
I've basically narrowed it down to see that in some cases, the actual
Form.Show is just stuck. In my form I overrode the 'Show' and print a
line before the base.Show and print a line afterwards to the log. I
get the printed line before but don't get the printed line afterwards.
So somehow it gets stuck in that call and I have no idea why.

It also doesn't always get stuck at the same place. Sometimes it gets
stuck somewhere else, usually when something's running on the UI
thread.

I'm 100% sure that there are no deadlocks or other synchronization
issues. I've also tried to replace the BackgroundWorker with Thread
but got the same results.
Also in the project settings I've tried to target the build for x86
processors as well as 'Any CPU' but that didn't help either.

Is there something that Visual Studio does fundamentally different
when running an app with regard to the UI thread than when running the
exe by itself?

Any ideas and suggestions are appreciated because at this time I'm
just at a loss as to what the problem could be.

Thanks.
Andi

Handle Application.ThreadException and AppDomain.UnhandledException
maybe you are getting some exception that you are not dealing with.
 
A

Andi

Handle Application.ThreadException and AppDomain.UnhandledException
maybe you are getting some exception that you are not dealing with.

I think you're onto something here. I don't know why I didn't think
myself to add those exception handlers. I guess I thought that such an
exception would bring down the whole app but it clearly doesn't.

Thanks.
 
A

Andi

Handle Application.ThreadException and AppDomain.UnhandledException
maybe you are getting some exception that you are not dealing with.

I think you're onto something here. I don't know why I didn't think
myself to add those exception handlers. I guess I thought that such an
exception would bring down the whole app but it clearly doesn't.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top