How to get the progress indication from a thread

  • Thread starter Thread starter Botak
  • Start date Start date
B

Botak

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
 
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
///
 
Great code! Thanks Cor.
What if user decides to cancel the thread middle of the process?
 
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
 
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
 
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
 
* "Botak said:
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.
 

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

Back
Top