Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2007

Tip: Looking for answers? Try searching our database.

Progress bar cannot be shown while a process is running

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
kimiraikkonen - 03 Oct 2007 23:06 GMT
Hi there,
There is a problem about progress bar; although the
"progressbar1.visible = True" is present for my invisible
"progressbar1" control to make visible when button1 is clicked, before
execution of my external process, it appears after the external
process finishes processing. And my main and only form (form1) cannot
be interacted while external process is processing. How can i get rid
of it?

Here is the code:

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

progressbar1.visible = True

Dim psInfo As New System.Diagnostics.ProcessStartInfo("c:\blabla.exe)
psInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
Dim myProcess As Process = System.Diagnostics.Process.Start(psInfo)
myProcess.WaitForExit()
MsgBox(" Completed Successfully", MsgBoxStyle.Information,
"Completed")

End Sub

Thanks and regards...
kimiraikkonen - 03 Oct 2007 23:42 GMT
Also, note: blabla.exe costs %100 CPU during processing. Could it be
the reason?

Anyway i tried with %40-50 cpu-costing external application with
similar negative result. While external process is running, the main
VB form is corrupted and distorted visually, as appearence and you
cannot click or modify main form during external process runs.
Armin Zingler - 04 Oct 2007 02:00 GMT
> Also, note: blabla.exe costs %100 CPU during processing. Could it be
> the reason?

No, "myProcess.WaitForExit()" is the reason. If it waits it, when is it to
display the progressbar? Use "progressbar1.refresh" directly after showing
it.

> Anyway i tried with %40-50 cpu-costing external application with
> similar negative result. While external process is running, the main
> VB form is corrupted and distorted visually, as appearence and you
> cannot click or modify main form during external process runs.

The short solution in this case is: Handle the process' Exited event instead
of waiting for the exit.

Want some background information?....

The OS is message based. It sends messages to the applications, for example
to inform about user input like mouse clicks and keyboard input. The
application must process the messages. We can react to them by handling
Click (and other) events. The OS puts the messages into a queue because the
application might still be busy handling the previous message. It is called
the "message queue". The application processes the messages in a loop, one
after the other. It is called the "message loop".

The OS maintains a list of visually "invalidated" areas on the screen. If
you show a progress bar, the OS adds the control's rectangle to that list
because that part of the screen must be updated (I keep it simple here). In
addition, the OS sends a message (called WM_PAINT; WM=windows message) to
the application, more precisely to the control, in order to tell it that it
must paint itself.

If you specify a Form as the startup object in the project's properties, VB
internally creates a call to "Application.Run". Guess what "Run" does?
Right, it contains the message loop that processes the arrived messages. It
keeps the application responsive by processing the click notifications and
paint requests.

In your case, Sub Button1_Click is the reaction to a message. In it, you
display the progress bar. In short, this makes the OS send a message to the
progress bar in order to paint itself. As explained above, the message is
put in the queue, but as long as the Click message for the Button hasn't
been processed completely, the Paint message for the progress bar can not be
processed. That's how it is with sub procedures: The main procedure (the
message loop) won't continue before the sub procedure (Sub Button_Click) has
exited. Therefore, the progress bar isn't painted before the Click event
procedure exited.

Calling progressbar1.refresh /immediatelly/ paints the progress bar, but in
order to keep the whole Form responsive, you must return from Sub
Button_Click as soon as possible. Now we have a problem: On the one hand, we
want to wait and on the hand we want to keep on processing messages. There
is only one way to do two things at one time: Create another thread.
Multiple threads are meant to be executed synchronously. So, the solution is
to create another thread that does what has to be done simultaneously.

Multi threading explained:
http://msdn2.microsoft.com/en-us/library/3e8s7xdd.aspx
http://msdn2.microsoft.com/en-us/library/eed6swsx(VS.80).aspx

Armin
Cor Ligthert[MVP] - 04 Oct 2007 05:21 GMT
Armin,

Nice explanation and even without the so by me damned words "message pump".

:-)

Cor
Armin Zingler - 04 Oct 2007 12:17 GMT
> Armin,
>
> Nice explanation

Thanks :)

> and even without the so by me damned words "message
> pump".

I'll put it in my synonym list. ;)

Armin
kimiraikkonen - 04 Oct 2007 21:56 GMT
> > Armin,
>
[quoted text clipped - 8 lines]
>
> Armin

Mr. Zingler,
Thank you for your detailed answer.
kimiraikkonen - 05 Oct 2007 10:13 GMT
Also, if i remove waitforexit(), main form can be interacted while the
external process is running without coding mutlithreading. So is the
unique way to set multi-threading to use waitforexit() effect and main
form interaction at the same same time with external process?
Armin Zingler - 05 Oct 2007 10:52 GMT
> Also, if i remove waitforexit(), main form can be interacted while
> the external process is running without coding mutlithreading.

Right

> So is
> the unique way to set multi-threading to use waitforexit() effect
> and main form interaction at the same same time with external
> process?

Sorry, I don't understand. If you want to wait for exit, you either can
handle the process' Exited event or you can call waitforexit in another
thread. For this purpose I'd prefer handling the Exit event.

Armin
kimiraikkonen - 05 Oct 2007 20:47 GMT
> > Also, if i remove waitforexit(), main form can be interacted while
> > the external process is running without coding mutlithreading.
[quoted text clipped - 11 lines]
>
> Armin

Thanks Armin, so could you give a simple example about handle exit
event? Sorry for being newbie and glad to receive help from experts.

Thanks.
Armin Zingler - 05 Oct 2007 21:01 GMT
> Thanks Armin, so could you give a simple example about handle exit
> event? Sorry for being newbie and glad to receive help from experts.

     Dim p As Process
       '...
       'start process
       '...
     AddHandler p.Exited, AddressOf OnProcessExited

  Private Sub OnProcessExited( _
     ByVal sender As Object, ByVal e As System.EventArgs)

     MsgBox("exited")

  End Sub

Armin
kimiraikkonen - 06 Oct 2007 15:31 GMT
> > Thanks Armin, so could you give a simple example about handle exit
> > event? Sorry for being newbie and glad to receive help from experts.
[quoted text clipped - 13 lines]
>
> Armin

Thanks but sorry, "OnProcessExited" cannot be found via intellisense.
I'm using vb.net 2005 express.
Armin Zingler - 06 Oct 2007 15:50 GMT
> On Oct 5, 11:01 pm, "Armin Zingler" <az.nos...@freenet.de> wrote:
> >
[quoted text clipped - 19 lines]
> Thanks but sorry, "OnProcessExited" cannot be found via
> intellisense. I'm using vb.net 2005 express.

?? It's right there below.

Armin
kimiraikkonen - 06 Oct 2007 16:47 GMT
> > > > Thanks Armin, so could you give a simple example about handle
> > > > exit event? Sorry for being newbie and glad to receive help from
[quoted text clipped - 23 lines]
>
> - Show quoted text -

There are a lot of intellisense listed OnXXXXX statements but there's
no "OnProcessExited" beleive. :(
Jack Jackson - 06 Oct 2007 17:25 GMT
>> > > > Thanks Armin, so could you give a simple example about handle
>> > > > exit event? Sorry for being newbie and glad to receive help from
[quoted text clipped - 26 lines]
>There are a lot of intellisense listed OnXXXXX statements but there's
>no "OnProcessExited" beleive. :(

Look at the code Armin posted.  OnProcessExited is defined in that
code.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.