Using threading to provide ProgressBar

G

Guest

Rather than putting a progress bar on all of my forms to show progress during time consuming tasks, I made a form called frmProgress with 2 controls, a Label and a ProgressBar. Suppose I expose 1 property -- prgValue (the Value of the ProgressBar); how do I use threading so that my time consuming task will open and display frmProgress as well as continually update prgValue (and display its progress), and then close frmProgress upon completion? This seems a pretty mundane thing to do. Is there an example somewhere

Thanks.
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?cG1jZ3VpcmU=?= said:
Rather than putting a progress bar on all of my forms to show progress
during time consuming tasks, I made a form called frmProgress with 2
controls, a Label and a ProgressBar. Suppose I expose 1 property --
prgValue (the Value of the ProgressBar); how do I use threading so that
my time consuming task will open and display frmProgress as well as
continually update prgValue (and display its progress), and then close
frmProgress upon completion? This seems a pretty mundane thing to do.
Is there an example somewhere?

In C#:

<http://www.codeproject.com/cs/miscctrl/progressdialog.asp>
 
C

Cor

Hi,

I thought Armin would send his sample himself.

This Armin made a while ago.

\\\By Armin Zingler
In a new project, add a button to the Form. Also add the following code:

Private m_Thread As MyThread

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

m_Thread = New MyThread
AddHandler m_Thread.Progress, AddressOf OnProgress
AddHandler m_Thread.Done, AddressOf OnDone
m_Thread.Start()
End Sub

Public Delegate Sub ProgressDelegate(ByVal Progress As Integer)

Private Sub OnProgress(ByVal Progress As Integer)
If Me.InvokeRequired Then
Me.Invoke(New ProgressDelegate( _
AddressOf OnProgress _
), New Object() {Progress})
Else
Me.Button1.Text = Progress.ToString
End If
End Sub

Private Sub OnDone()
m_Thread = Nothing
End Sub
////
Class MyThread
Public Event Progress(ByVal Progress As Integer)
Public Event Done()

Private m_Thread As Thread

Public Sub Start()
m_Thread = New Thread(AddressOf ThreadStart)
m_Thread.Start()
End Sub

Private Sub ThreadStart()
Dim i As Integer
For i = 1 To 100
Thread.Sleep(100)
RaiseEvent Progress(i)
Next
RaiseEvent Done()
End Sub

End Class
///


I hope this helps a little bit?

Cor
Rather than putting a progress bar on all of my forms to show progress
during time consuming tasks, I made a form called frmProgress with 2
controls, a Label and a ProgressBar. Suppose I expose 1 property --
prgValue (the Value of the ProgressBar); how do I use threading so that my
time consuming task will open and display frmProgress as well as continually
update prgValue (and display its progress), and then close frmProgress upon
completion? This seems a pretty mundane thing to do. Is there an example
somewhere?
 
A

Armin Zingler

pmcguire said:

Either it's too late for me now (1:40 am) or you put the work in the wrong
thread. If you wanna do something in the background you should put it in
"Sub ThreadStart". A basic rule is that a control must only be accessed in
the same thread that created the control. The code looks as if your main
target is to fill a Treeview. This is usually not done in the background
(but it can be), so IMO it does not make much sense to put it in a separate
thread. If you did, you would have to use the control's Invoke or
BeginInvoke method within the other thread. These two methods call a
procedure in the same thread that created the control (see docs). Within the
called procedure, you can acces the control without a problem. In your case
I would fill the treeview and update the progressbar in the same thread. It
would slow down filling the Treeview considerably if the largest part was
marshalling the work to the main thread, so you'd better keep it there right
from the start.

A "better" situation, for example, would be processing a volume on the disc
in a separate thread and filling a listview with the result each time an
item is found.
 

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