Click to See Complete Forum and Search --> : Form going white upon running


Macgoober
September 22nd, 2009, 03:23 AM
Whenever I run my code, the form becomes un-visible: what I mean by this is...
3 console windows fly by briefly, via the output of commands, and the form becomes black, covered by these windows even though the windows don't exist anymore, and after it begins another function of the program, the form then turns white until the function is completed...

Is there a way to stop this behavior? I'd like to add some sort of progress notifications but I find it useless given it would just be covered up by non-sense.

Any Ideas?

Regards,

- Mac

Alex F
September 22nd, 2009, 06:15 AM
Let's say that you have message handler Button1_Click, and it is executed 5 seconds. All this time form is not responsive and doesn't redraw itself. To keep form responsive, you have two options:
1. Use multithreading. Long operation must be executed in a worker thread, keeping main application thread responsive. See: BackgroundWorker class.
2. DoEvents - simplified solution. Diring long operation, periodically call Application:: DoEvents method. Every DoEvents call allows redrawing, if it is necessary.

Macgoober
September 24th, 2009, 02:57 PM
Hmm. Ok well I wanted to go with the Multi-Threading approach, however it's a CLR Form application... and the compiler complains about using the /MTd switch instead of the usual single threading MDd... this creates a problem with the multithreaded approach.... any way around this?

Regards,

- Mac

darwen
September 24th, 2009, 07:51 PM
You should be using /MDd anyway in debug builds as this is "Multi-threaded Debug DLL". For release builds it should be /MD "Multi-threaded DLL" - but all this is set up by default for you in the project settings if you've used the wizard to create your project.

You need to create a new worker thread in code and get it to execute your long running code.

Have a look at the BackgroundWorker (http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx) class as a starting point.

Darwen.

darwen
September 24th, 2009, 08:09 PM
I've done a sample project for you.

Click the button and see the percentage go up (even though it's calculated in a loop), leaving the form responsive to events.

Darwen.

autostrad
September 24th, 2009, 09:59 PM
I am sorry, I do not know how to post a new thread. can anybody tell me?

Macgoober
September 24th, 2009, 10:17 PM
absolutely amazing.... now if I understood how it all works I'll be set!!!

But yea that example helped alot, I just don't understand how _worker was initialized

I must do research....

Thank you both for the information.

Regards,

- Mac