473,722 Members | 2,430 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading a ShowDialog? - progress form.

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 format when
this runs and slowely update the progress bar as each sub runs.

I cant just call the form with fStatus.ShowDia log() cause then all code
stops till this form is closed. So What I am trying is to somehow open this
form in a different thread - in ShowDialog format, so I can continue to run
my code, and as my code runs, update the progress bar on the new status form
created.
At the end, close the status form, and let the user continue on.
As the status form is open, the whole app is 'locked' because of showdialog,
until it is complete.

I hope this makes sence.

Below I have code that I think is in the right direction, but I am at a wall
as I have never done any thread processing.

Thanks for your help,

Miro
'==== some command button _click event on a current form. 'which is a child
form of a mdi form.
'Somewhere here change the icon to be hourglass

Dim fStatus As New frmStatus()

'im pulling code out of my butt for this next line here cause ive
never done this before
Dim Thread1 As New System.Threadin g.Thread(Addres sOf
fStatus.ShowDia log)

Thread1.Start()

'pbStatus is a status bar / progress bar
fStatus.pbStatu s.Value += 10 'Increment Push Button
System.Threadin g.Thread.Sleep( 2000) 'Will Call some sub instead later

fStatus.pbStatu s.Value += 10
System.Threadin g.Thread.Sleep( 2000) 'Will Call some sub instead later
fStatus.pbStatu s.Value += 10
System.Threadin g.Thread.Sleep( 2000) 'Will Call some sub instead later

'Somewhere here change the icon to be pointer again.

'close the form
fStatus.Close()
'im assuming i have to close the thread too or something?
fStatus.Dispose ()
'user should have access to the whole app again.

'==== end of some command button _click event
Oct 13 '06 #1
5 10904
Miro,
Are you using VS 2005 (.NET 2.0) or VS 2002/2003 (.NET 1.x)?

..NET 2.0's new BackgroundWorke r component makes this really easy!

For example create a new dialog from, put a BackgroundWorke r object on it,
along with a ProgressBar.

Using the following code for the form.

Option Strict On
Option Explicit On

Imports System.Componen tModel
Imports System.Threadin g

Public Class LongProcessDial og

Protected Overrides Sub OnLoad(ByVal e As System.EventArg s)
MyBase.OnLoad(e )
BackgroundWorke r1.RunWorkerAsy nc()
End Sub

Private Sub BackgroundWorke r1_DoWork(ByVal sender As System.Object,
ByVal e As DoWorkEventArgs ) Handles BackgroundWorke r1.DoWork
For percentProgress As Integer = 1 To 100
Thread.Sleep(Ti meSpan.FromSeco nds(1.5))
BackgroundWorke r1.ReportProgre ss(percentProgr ess)
Next
End Sub

Private Sub BackgroundWorke r1_ProgressChan ged(ByVal sender As Object,
ByVal e As ProgressChanged EventArgs) Handles
BackgroundWorke r1.ProgressChan ged
Me.ProgressBar1 .Value = e.ProgressPerce ntage
End Sub

Private Sub BackgroundWorke r1_RunWorkerCom pleted(ByVal sender As Object,
ByVal e As RunWorkerComple tedEventArgs) Handles
BackgroundWorke r1.RunWorkerCom pleted
Close()
End Sub

End Class

The BackgroundWorke r1_DoWork event is where all the real work (long running
process) is being done. Within it you call BackgroundWorke r1.ReportProgre ss
to have the dialog update its controls.

The BackgroundWorke r itself handles calling Control.Invoke where needed.

BackgroundWorke r also supports passing a parameter to the background thread,
returning results (including exceptions) as well as cancelling the
background thread!

My Xml Export Sample, uses the BackgroundWorke r within an Outlook form to
perform a long running process.

http://www.tsbradley.net/Samples/VST...rt.Sample.aspx
--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Miro" <mi******@golde n.netwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
>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 format when
this runs and slowely update the progress bar as each sub runs.

I cant just call the form with fStatus.ShowDia log() cause then all code
stops till this form is closed. So What I am trying is to somehow open
this form in a different thread - in ShowDialog format, so I can continue
to run my code, and as my code runs, update the progress bar on the new
status form created.
At the end, close the status form, and let the user continue on.
As the status form is open, the whole app is 'locked' because of
showdialog, until it is complete.

I hope this makes sence.

Below I have code that I think is in the right direction, but I am at a
wall as I have never done any thread processing.

Thanks for your help,

Miro
'==== some command button _click event on a current form. 'which is a
child form of a mdi form.
'Somewhere here change the icon to be hourglass

Dim fStatus As New frmStatus()

'im pulling code out of my butt for this next line here cause ive
never done this before
Dim Thread1 As New System.Threadin g.Thread(Addres sOf
fStatus.ShowDia log)

Thread1.Start()

'pbStatus is a status bar / progress bar
fStatus.pbStatu s.Value += 10 'Increment Push Button
System.Threadin g.Thread.Sleep( 2000) 'Will Call some sub instead later

fStatus.pbStatu s.Value += 10
System.Threadin g.Thread.Sleep( 2000) 'Will Call some sub instead later
fStatus.pbStatu s.Value += 10
System.Threadin g.Thread.Sleep( 2000) 'Will Call some sub instead later

'Somewhere here change the icon to be pointer again.

'close the form
fStatus.Close()
'im assuming i have to close the thread too or something?
fStatus.Dispose ()
'user should have access to the whole app again.

'==== end of some command button _click event
Oct 15 '06 #2
Im using vb express 2005

This is what i needed to get me started.

I will search the web and what I need for Background worker

From what I understand reading the code, I cannot have the code I require in
the "form", i have to move the sub calls to the "background worker" sub, and
call the origina subs in there.

Thanks much!

Miro

"Jay B. Harlow" <Ja************ @tsbradley.netw rote in message
news:30******** *************** ***********@mic rosoft.com...
Miro,
Are you using VS 2005 (.NET 2.0) or VS 2002/2003 (.NET 1.x)?

.NET 2.0's new BackgroundWorke r component makes this really easy!

For example create a new dialog from, put a BackgroundWorke r object on it,
along with a ProgressBar.

Using the following code for the form.

Option Strict On
Option Explicit On

Imports System.Componen tModel
Imports System.Threadin g

Public Class LongProcessDial og

Protected Overrides Sub OnLoad(ByVal e As System.EventArg s)
MyBase.OnLoad(e )
BackgroundWorke r1.RunWorkerAsy nc()
End Sub

Private Sub BackgroundWorke r1_DoWork(ByVal sender As System.Object,
ByVal e As DoWorkEventArgs ) Handles BackgroundWorke r1.DoWork
For percentProgress As Integer = 1 To 100
Thread.Sleep(Ti meSpan.FromSeco nds(1.5))
BackgroundWorke r1.ReportProgre ss(percentProgr ess)
Next
End Sub

Private Sub BackgroundWorke r1_ProgressChan ged(ByVal sender As Object,
ByVal e As ProgressChanged EventArgs) Handles
BackgroundWorke r1.ProgressChan ged
Me.ProgressBar1 .Value = e.ProgressPerce ntage
End Sub

Private Sub BackgroundWorke r1_RunWorkerCom pleted(ByVal sender As
Object, ByVal e As RunWorkerComple tedEventArgs) Handles
BackgroundWorke r1.RunWorkerCom pleted
Close()
End Sub

End Class

The BackgroundWorke r1_DoWork event is where all the real work (long
running process) is being done. Within it you call
BackgroundWorke r1.ReportProgre ss to have the dialog update its controls.

The BackgroundWorke r itself handles calling Control.Invoke where needed.

BackgroundWorke r also supports passing a parameter to the background
thread, returning results (including exceptions) as well as cancelling the
background thread!

My Xml Export Sample, uses the BackgroundWorke r within an Outlook form to
perform a long running process.

http://www.tsbradley.net/Samples/VST...rt.Sample.aspx
--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Miro" <mi******@golde n.netwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
>>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 format
when this runs and slowely update the progress bar as each sub runs.

I cant just call the form with fStatus.ShowDia log() cause then all code
stops till this form is closed. So What I am trying is to somehow open
this form in a different thread - in ShowDialog format, so I can continue
to run my code, and as my code runs, update the progress bar on the new
status form created.
At the end, close the status form, and let the user continue on.
As the status form is open, the whole app is 'locked' because of
showdialog, until it is complete.

I hope this makes sence.

Below I have code that I think is in the right direction, but I am at a
wall as I have never done any thread processing.

Thanks for your help,

Miro
'==== some command button _click event on a current form. 'which is a
child form of a mdi form.
'Somewhere here change the icon to be hourglass

Dim fStatus As New frmStatus()

'im pulling code out of my butt for this next line here cause ive
never done this before
Dim Thread1 As New System.Threadin g.Thread(Addres sOf
fStatus.ShowDi alog)

Thread1.Start()

'pbStatus is a status bar / progress bar
fStatus.pbStatu s.Value += 10 'Increment Push Button
System.Threadi ng.Thread.Sleep (2000) 'Will Call some sub instead later

fStatus.pbStat us.Value += 10
System.Threadi ng.Thread.Sleep (2000) 'Will Call some sub instead later
fStatus.pbStat us.Value += 10
System.Threadi ng.Thread.Sleep (2000) 'Will Call some sub instead later

'Somewhere here change the icon to be pointer again.

'close the form
fStatus.Close( )
'im assuming i have to close the thread too or something?
fStatus.Dispos e()
'user should have access to the whole app again.

'==== end of some command button _click event

Oct 15 '06 #3
Miro,
From what I understand reading the code, I cannot have the code I require
in the "form",
It depends on what you mean by "form", do you mean your "main" form, or do
mean your "worker" form.

I would put all the subs in a "worker" form, this "worker" form would
contain a ProgressBar, BackgroundWorke r component, plus any other controls
that may be needed (such as inputs, cancel buttons.

The "main" form would display the "worker" form:

' someplace one the "main" form
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
EventArgs) Handles Button1.Click
Using dialog As New WorkerDialog
dialog.ShowDial og()
End Using
End Sub

http://msdn2.microsoft.com/en-us/lib...undworker.aspx
--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Miro" <mi******@golde n.netwrote in message
news:O4******** ******@TK2MSFTN GP05.phx.gbl...
Im using vb express 2005

This is what i needed to get me started.

I will search the web and what I need for Background worker

From what I understand reading the code, I cannot have the code I require
in the "form", i have to move the sub calls to the "background worker"
sub, and call the origina subs in there.

Thanks much!

Miro

"Jay B. Harlow" <Ja************ @tsbradley.netw rote in message
news:30******** *************** ***********@mic rosoft.com...
>Miro,
Are you using VS 2005 (.NET 2.0) or VS 2002/2003 (.NET 1.x)?

.NET 2.0's new BackgroundWorke r component makes this really easy!

For example create a new dialog from, put a BackgroundWorke r object on
it, along with a ProgressBar.

Using the following code for the form.

Option Strict On
Option Explicit On

Imports System.Componen tModel
Imports System.Threadin g

Public Class LongProcessDial og

Protected Overrides Sub OnLoad(ByVal e As System.EventArg s)
MyBase.OnLoad(e )
BackgroundWorke r1.RunWorkerAsy nc()
End Sub

Private Sub BackgroundWorke r1_DoWork(ByVal sender As System.Object,
ByVal e As DoWorkEventArgs ) Handles BackgroundWorke r1.DoWork
For percentProgress As Integer = 1 To 100
Thread.Sleep(Ti meSpan.FromSeco nds(1.5))
BackgroundWorke r1.ReportProgre ss(percentProgr ess)
Next
End Sub

Private Sub BackgroundWorke r1_ProgressChan ged(ByVal sender As Object,
ByVal e As ProgressChanged EventArgs) Handles
BackgroundWork er1.ProgressCha nged
Me.ProgressBar1 .Value = e.ProgressPerce ntage
End Sub

Private Sub BackgroundWorke r1_RunWorkerCom pleted(ByVal sender As
Object, ByVal e As RunWorkerComple tedEventArgs) Handles
BackgroundWork er1.RunWorkerCo mpleted
Close()
End Sub

End Class

The BackgroundWorke r1_DoWork event is where all the real work (long
running process) is being done. Within it you call
BackgroundWork er1.ReportProgr ess to have the dialog update its controls.

The BackgroundWorke r itself handles calling Control.Invoke where needed.

BackgroundWork er also supports passing a parameter to the background
thread, returning results (including exceptions) as well as cancelling
the background thread!

My Xml Export Sample, uses the BackgroundWorke r within an Outlook form to
perform a long running process.

http://www.tsbradley.net/Samples/VST...rt.Sample.aspx
--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Miro" <mi******@golde n.netwrote in message
news:%2******* *********@TK2MS FTNGP04.phx.gbl ...
>>>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 format
when this runs and slowely update the progress bar as each sub runs.

I cant just call the form with fStatus.ShowDia log() cause then all
code stops till this form is closed. So What I am trying is to somehow
open this form in a different thread - in ShowDialog format, so I can
continue to run my code, and as my code runs, update the progress bar on
the new status form created.
At the end, close the status form, and let the user continue on.
As the status form is open, the whole app is 'locked' because of
showdialog, until it is complete.

I hope this makes sence.

Below I have code that I think is in the right direction, but I am at a
wall as I have never done any thread processing.

Thanks for your help,

Miro
'==== some command button _click event on a current form. 'which is a
child form of a mdi form.
'Somewhere here change the icon to be hourglass

Dim fStatus As New frmStatus()

'im pulling code out of my butt for this next line here cause ive
never done this before
Dim Thread1 As New System.Threadin g.Thread(Addres sOf
fStatus.ShowD ialog)

Thread1.Start()

'pbStatus is a status bar / progress bar
fStatus.pbStatu s.Value += 10 'Increment Push Button
System.Thread ing.Thread.Slee p(2000) 'Will Call some sub instead later

fStatus.pbSta tus.Value += 10
System.Thread ing.Thread.Slee p(2000) 'Will Call some sub instead later
fStatus.pbSta tus.Value += 10
System.Thread ing.Thread.Slee p(2000) 'Will Call some sub instead later

'Somewhere here change the icon to be pointer again.

'close the form
fStatus.Close ()
'im assuming i have to close the thread too or something?
fStatus.Dispo se()
'user should have access to the whole app again.

'==== end of some command button _click event

Oct 15 '06 #4
I was trying to keep the sub calls in the Main form,
and somehow update the "worker form" from anywhere.

I thought it would be a lot easier to have a modal form but be able to keep
running the code from behind.

I was hoping to make a "generic" "working" form and have the same one called
from everywhere.
Example:
Sub A calls B calls C

Sub D calls B and E
where E calls F

Since B can be called from 2 different original subs' and I would like to
update a "differnet" / "or the same" worker form, I wanted the subs,
themselves take care of the worker form, rather than the worker form take
care of the subs'.

Miro

"Jay B. Harlow" <Ja************ @tsbradley.netw rote in message
news:A5******** *************** ***********@mic rosoft.com...
Miro,
>From what I understand reading the code, I cannot have the code I require
in the "form",
It depends on what you mean by "form", do you mean your "main" form, or do
mean your "worker" form.

I would put all the subs in a "worker" form, this "worker" form would
contain a ProgressBar, BackgroundWorke r component, plus any other controls
that may be needed (such as inputs, cancel buttons.

The "main" form would display the "worker" form:

' someplace one the "main" form
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
EventArgs) Handles Button1.Click
Using dialog As New WorkerDialog
dialog.ShowDial og()
End Using
End Sub

http://msdn2.microsoft.com/en-us/lib...undworker.aspx
--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Miro" <mi******@golde n.netwrote in message
news:O4******** ******@TK2MSFTN GP05.phx.gbl...
>Im using vb express 2005

This is what i needed to get me started.

I will search the web and what I need for Background worker

From what I understand reading the code, I cannot have the code I require
in the "form", i have to move the sub calls to the "background worker"
sub, and call the origina subs in there.

Thanks much!

Miro

"Jay B. Harlow" <Ja************ @tsbradley.netw rote in message
news:30******* *************** ************@mi crosoft.com...
>>Miro,
Are you using VS 2005 (.NET 2.0) or VS 2002/2003 (.NET 1.x)?

.NET 2.0's new BackgroundWorke r component makes this really easy!

For example create a new dialog from, put a BackgroundWorke r object on
it, along with a ProgressBar.

Using the following code for the form.

Option Strict On
Option Explicit On

Imports System.Componen tModel
Imports System.Threadin g

Public Class LongProcessDial og

Protected Overrides Sub OnLoad(ByVal e As System.EventArg s)
MyBase.OnLoad(e )
BackgroundWorke r1.RunWorkerAsy nc()
End Sub

Private Sub BackgroundWorke r1_DoWork(ByVal sender As System.Object,
ByVal e As DoWorkEventArgs ) Handles BackgroundWorke r1.DoWork
For percentProgress As Integer = 1 To 100
Thread.Sleep(Ti meSpan.FromSeco nds(1.5))
BackgroundWorke r1.ReportProgre ss(percentProgr ess)
Next
End Sub

Private Sub BackgroundWorke r1_ProgressChan ged(ByVal sender As Object,
ByVal e As ProgressChanged EventArgs) Handles
BackgroundWor ker1.ProgressCh anged
Me.ProgressBar1 .Value = e.ProgressPerce ntage
End Sub

Private Sub BackgroundWorke r1_RunWorkerCom pleted(ByVal sender As
Object, ByVal e As RunWorkerComple tedEventArgs) Handles
BackgroundWor ker1.RunWorkerC ompleted
Close()
End Sub

End Class

The BackgroundWorke r1_DoWork event is where all the real work (long
running process) is being done. Within it you call
BackgroundWor ker1.ReportProg ress to have the dialog update its controls.

The BackgroundWorke r itself handles calling Control.Invoke where needed.

BackgroundWor ker also supports passing a parameter to the background
thread, returning results (including exceptions) as well as cancelling
the background thread!

My Xml Export Sample, uses the BackgroundWorke r within an Outlook form
to perform a long running process.

http://www.tsbradley.net/Samples/VST...rt.Sample.aspx
--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Miro" <mi******@golde n.netwrote in message
news:%2****** **********@TK2M SFTNGP04.phx.gb l...
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 format
when this runs and slowely update the progress bar as each sub runs.

I cant just call the form with fStatus.ShowDia log() cause then all
code stops till this form is closed. So What I am trying is to somehow
open this form in a different thread - in ShowDialog format, so I can
continue to run my code, and as my code runs, update the progress bar
on the new status form created.
At the end, close the status form, and let the user continue on.
As the status form is open, the whole app is 'locked' because of
showdialog , until it is complete.

I hope this makes sence.

Below I have code that I think is in the right direction, but I am at a
wall as I have never done any thread processing.

Thanks for your help,

Miro
'==== some command button _click event on a current form. 'which is a
child form of a mdi form.
'Somewhere here change the icon to be hourglass

Dim fStatus As New frmStatus()

'im pulling code out of my butt for this next line here cause
ive never done this before
Dim Thread1 As New System.Threadin g.Thread(Addres sOf
fStatus.Show Dialog)

Thread1.Start()

'pbStatus is a status bar / progress bar
fStatus.pbStatu s.Value += 10 'Increment Push Button
System.Threa ding.Thread.Sle ep(2000) 'Will Call some sub instead later

fStatus.pbSt atus.Value += 10
System.Threa ding.Thread.Sle ep(2000) 'Will Call some sub instead later
fStatus.pbSt atus.Value += 10
System.Threa ding.Thread.Sle ep(2000) 'Will Call some sub instead later

'Somewhere here change the icon to be pointer again.

'close the form
fStatus.Clos e()
'im assuming i have to close the thread too or something?
fStatus.Disp ose()
'user should have access to the whole app again.

'==== end of some command button _click event



Oct 16 '06 #5
I thought it would be a lot easier to have a modal form but be able to
keep running the code from behind.
I agree, on the surface a modal form would appear be a lot easier. However
in practice I am sure you will find the BackgroundWorke r component to be
significantly easier. Considering that all the "heavy lifting" has been done
for you. All you really need to do is insert your code in the DoWork event.
Occasionally you may want to, but don't need to, call the BackgroundWorke r
..ReportProgres s method.

I was hoping to make a "generic" "working" form and have the same one
called from everywhere.
Example:
Sub A calls B calls C

Sub D calls B and E
where E calls F
It sounds like a good use for inheritance.

WorkerForm is a base form that has the common routines (B) along with the
progress bar & background worker. The DoWork event handler would call an
overridable method DoWork.

AForm would inherit from WorkerForm overriding the DoWork method calling Sub
A.

DForm would inherit from WorkerForm overiding the DoWork method calling Sub
D.

Sub A would be in AForm, while Sub D would be in DForm.

>I was trying to keep the sub calls in the Main form,
and somehow update the "worker form" from anywhere.
You should be able to use the instance of the Form from the Forms
collection. The caveat is you need to handle the DoWork event to get the
background started...

Personally I find it cleaner to encapsulate (one of the tenants of Object
Oriented development) the "worker" logic all in the "worker form"...

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Miro" <mi******@golde n.netwrote in message
news:uP******** ******@TK2MSFTN GP04.phx.gbl...
>I was trying to keep the sub calls in the Main form,
and somehow update the "worker form" from anywhere.

I thought it would be a lot easier to have a modal form but be able to
keep running the code from behind.

I was hoping to make a "generic" "working" form and have the same one
called from everywhere.
Example:
Sub A calls B calls C

Sub D calls B and E
where E calls F

Since B can be called from 2 different original subs' and I would like to
update a "differnet" / "or the same" worker form, I wanted the subs,
themselves take care of the worker form, rather than the worker form take
care of the subs'.

Miro

"Jay B. Harlow" <Ja************ @tsbradley.netw rote in message
news:A5******** *************** ***********@mic rosoft.com...
>Miro,
>>From what I understand reading the code, I cannot have the code I
require in the "form",
It depends on what you mean by "form", do you mean your "main" form, or
do mean your "worker" form.

I would put all the subs in a "worker" form, this "worker" form would
contain a ProgressBar, BackgroundWorke r component, plus any other
controls that may be needed (such as inputs, cancel buttons.

The "main" form would display the "worker" form:

' someplace one the "main" form
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
EventArgs) Handles Button1.Click
Using dialog As New WorkerDialog
dialog.ShowDial og()
End Using
End Sub

http://msdn2.microsoft.com/en-us/lib...undworker.aspx
--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Miro" <mi******@golde n.netwrote in message
news:O4******* *******@TK2MSFT NGP05.phx.gbl.. .
>>Im using vb express 2005

This is what i needed to get me started.

I will search the web and what I need for Background worker

From what I understand reading the code, I cannot have the code I
require in the "form", i have to move the sub calls to the "background
worker" sub, and call the origina subs in there.

Thanks much!

Miro

"Jay B. Harlow" <Ja************ @tsbradley.netw rote in message
news:30****** *************** *************@m icrosoft.com...
Miro,
Are you using VS 2005 (.NET 2.0) or VS 2002/2003 (.NET 1.x)?

.NET 2.0's new BackgroundWorke r component makes this really easy!

For example create a new dialog from, put a BackgroundWorke r object on
it, along with a ProgressBar.

Using the following code for the form.

Option Strict On
Option Explicit On

Imports System.Componen tModel
Imports System.Threadin g

Public Class LongProcessDial og

Protected Overrides Sub OnLoad(ByVal e As System.EventArg s)
MyBase.OnLoad(e )
BackgroundWorke r1.RunWorkerAsy nc()
End Sub

Private Sub BackgroundWorke r1_DoWork(ByVal sender As System.Object,
ByVal e As DoWorkEventArgs ) Handles BackgroundWorke r1.DoWork
For percentProgress As Integer = 1 To 100
Thread.Sleep(Ti meSpan.FromSeco nds(1.5))
BackgroundWorke r1.ReportProgre ss(percentProgr ess)
Next
End Sub

Private Sub BackgroundWorke r1_ProgressChan ged(ByVal sender As
Object, ByVal e As ProgressChanged EventArgs) Handles
BackgroundWo rker1.ProgressC hanged
Me.ProgressBar1 .Value = e.ProgressPerce ntage
End Sub

Private Sub BackgroundWorke r1_RunWorkerCom pleted(ByVal sender As
Object, ByVal e As RunWorkerComple tedEventArgs) Handles
BackgroundWo rker1.RunWorker Completed
Close()
End Sub

End Class

The BackgroundWorke r1_DoWork event is where all the real work (long
running process) is being done. Within it you call
BackgroundWo rker1.ReportPro gress to have the dialog update its
controls.

The BackgroundWorke r itself handles calling Control.Invoke where
needed.

BackgroundWo rker also supports passing a parameter to the background
thread, returning results (including exceptions) as well as cancelling
the background thread!

My Xml Export Sample, uses the BackgroundWorke r within an Outlook form
to perform a long running process.

http://www.tsbradley.net/Samples/VST...rt.Sample.aspx
--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Miro" <mi******@golde n.netwrote in message
news:%2***** ***********@TK2 MSFTNGP04.phx.g bl...
>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...i n 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 format
when this runs and slowely update the progress bar as each sub runs.
>
I cant just call the form with fStatus.ShowDia log() cause then all
code stops till this form is closed. So What I am trying is to
somehow open this form in a different thread - in ShowDialog format,
so I can continue to run my code, and as my code runs, update the
progress bar on the new status form created.
At the end, close the status form, and let the user continue on.
As the status form is open, the whole app is 'locked' because of
showdialo g, until it is complete.
>
I hope this makes sence.
>
Below I have code that I think is in the right direction, but I am at
a wall as I have never done any thread processing.
>
Thanks for your help,
>
Miro
>
>
'==== some command button _click event on a current form. 'which is a
child form of a mdi form.
'Somewhere here change the icon to be hourglass
>
Dim fStatus As New frmStatus()
>
'im pulling code out of my butt for this next line here cause
ive never done this before
Dim Thread1 As New System.Threadin g.Thread(Addres sOf
fStatus.Sho wDialog)
>
Thread1.Start()
>
'pbStatus is a status bar / progress bar
fStatus.pbStatu s.Value += 10 'Increment Push Button
System.Thre ading.Thread.Sl eep(2000) 'Will Call some sub instead later
>
fStatus.pbS tatus.Value += 10
System.Thre ading.Thread.Sl eep(2000) 'Will Call some sub instead later
fStatus.pbS tatus.Value += 10
System.Thre ading.Thread.Sl eep(2000) 'Will Call some sub instead later
>
'Somewher e here change the icon to be pointer again.
>
'close the form
fStatus.Clo se()
'im assuming i have to close the thread too or something?
fStatus.Dis pose()
'user should have access to the whole app again.
>
'==== end of some command button _click event
>

Oct 16 '06 #6

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

Similar topics

5
306
by: Charles A. Lackman | last post by:
Hello, I am making a app that creates a thread to show a clipboard. If the clipboard is double clicked, another window is displayed, the event is handled inside the main app. What I would like to do is have the clipboard use the main thread of the application that called it to handle the new window. Is this possible and if so how would this be accomplished. ie.
4
1599
by: Charles A. Lackman | last post by:
Hello and thank you for your assistance. I have attempted to accomplish what I need using delegates with no success. i.e. //Button Click// Dim PollThread As Threading.Thread PollThread = New Threading.Thread(AddressOf PollThreadAddress) PollThread.Start() End Sub
4
1274
by: Max | last post by:
Playing around with multi-threading programs and ran into this little problem that maybe someone here could explain... Basically I have a class which launches a form object. Once the form is launched (using ShowDialog) I still needed both to be available, so my solution was to start the form in a new thread. This way the program itself can still use the class and the user can interact with the form. So here's how I did this: Public...
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...
14
1635
by: Simon Verona | last post by:
I think I'm doing this wrong : I have a class with a public shared method such as follows: public shared sub myFunction dim frm as new myFrm dim t as new Threading.Thread(Addressof myFrm.myShowDialog t.start end sub
0
1494
by: Flack | last post by:
Hey guys, I'm trying to read a file and add some nodes to a treee view control. I would like the GUI to remain interactive and have a progress bar and text box be updated with info as the file is read. I'm using a threadpool thread and reading the file from there. However, I seem to be running into some problems. Sometimes the GUi seems to be updating/repainting fine but than all of a sudden stops responding. Also, if the open file...
7
2375
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has finished. I am trying to follow this example : http://www.codeproject.com/cs/miscctrl/progressdialog.asp But although the messages still get moved, the progress window never does anything. Here is my code in full, if anybody who knows...
10
3734
by: Mike P | last post by:
I have two loops that I am running, one of which is calling a method which contains the second one. What I want to happen is for when the second loop exits (the inner loop), then the code will return to the first loop (the outer loop) and continue executing, but this doesn't seem to be happening. Here is my code if anybody can help me out : Loop 1 : for (int i = 0; i < oItems.Count; i++) {
5
6931
by: CCLeasing | last post by:
For an application I'm creating I want to create a 'fake' progress bar. By fake I mean a progress bar that looks like it's doing something but actually isn't. I know philosophically this isn't sound. But my little app is a 'fake' app and is designed to look like another - hence this seeming crazy situation of needing to fake a progess bar. PROBLEM.
0
8739
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,...
0
9384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9238
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9157
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
9088
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
8052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.