469,134 Members | 1,313 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,134 developers. It's quick & easy.

Using threading to provide ProgressBar

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.
Nov 20 '05 #1
4 4504
* "=?Utf-8?B?cG1jZ3VpcmU=?=" <an*******@discussions.microsoft.com> scripsit:
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>

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2
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?
Thanks.

Nov 20 '05 #3
"Cor" <no*@non.com> schrieb
Hi,

I thought Armin would send his sample himself.

This Armin made a while ago.

Thx :)
--
Armin

Nov 20 '05 #4
"pmcguire" <an*******@discussions.microsoft.com> schrieb
[...]


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.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Arun Kumar | last post: by
2 posts views Thread by Vjay77 | last post: by
reply views Thread by Ban Leong | last post: by
8 posts views Thread by WhiteWizard | last post: by
1 post views Thread by =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.