By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,219 Members | 2,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,219 IT Pros & Developers. It's quick & easy.

How to get the progress indication from a thread

P: n/a
I am able to simulate and run the "thread" as adviced earlier from my
previous post. Thanks for the tips.
Now I am stucked at how to get the progress status from the thread.

I have a main form that contains a datagrid, a progress bar and a button to
hold the result from long calculation.
looks like

[myDataGrid]
Item Code new cost
1 ABC <result from calculation>
2. DEF <....>
3. ..
..

[myPGB]
< and a Progress bar.......>

When the button is clicked, I will do while no end of file with the datagrid
and calculate new cost for each item and fill in back to the grid one by
one. Also, I will need to show user the progress.

Any advice?

TIA


Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi Botak,

You know this sample from Arming Zingler?

I hope this helps?

Cor

\\\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
///
Nov 20 '05 #2

P: n/a
Great code! Thanks Cor.
What if user decides to cancel the thread middle of the process?



"Cor Ligthert" <no**********@planet.nl> wrote in message
news:e#**************@TK2MSFTNGP10.phx.gbl...
Hi Botak,

You know this sample from Arming Zingler?

I hope this helps?

Cor

\\\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
///

Nov 20 '05 #3

P: n/a
Hi Botak,

Normaly nothing special, when you do me.close on the mainform then
everything should be garbaged by the Garbage Collector.

Therefore it is managed code.

I hope this helps?

Cor
Nov 20 '05 #4

P: n/a
Hi Cor,

I tried "THREAD.ABORT" when cancel button is clicked and it closes main
form!!???

What I try to do is just to stop halfway without closing the main form.

Any idea?

Thanks



"Cor Ligthert" <no**********@planet.nl> wrote in message
news:O4**************@TK2MSFTNGP09.phx.gbl...
Hi Botak,

Normaly nothing special, when you do me.close on the mainform then
everything should be garbaged by the Garbage Collector.

Therefore it is managed code.

I hope this helps?

Cor

Nov 20 '05 #5

P: n/a
Hi Botak,

Thread.abort is your main thread, when you want to abort a seperate thread,
you should use the right thread.

So this will be something as when I take the sample.
m_Thread.abort (do not expect it aborts direct in the taskmanager).

That m_Thread would be probably needed to be declared global in this case.

I hope this helps?

Cor
I tried "THREAD.ABORT" when cancel button is clicked and it closes main
form!!???

What I try to do is just to stop halfway without closing the main form.


Nov 20 '05 #6

P: n/a
* "Botak" <wi****@total.com.my> scripsit:
Great code! Thanks Cor.
What if user decides to cancel the thread middle of the process?


Set a Boolean variable that is checked by the thread and tells the
thread to stop processing.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.