|
-
May 10th, 2019, 02:59 AM
#1
Thread Starter
Addicted Member
Notify user when thread has started and when it has finished?
Learning this VB.NET stuff and I am still not sure how to notify properly the user when exporting to excel has started and when it has finished.
I have this code:
Code:
Dim finished as Boolean = true
private sub exportToExcel()
' exporting code
end sub
Private Sub exportToExcelBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exportToExcelBtn.Click
finished = False
statusBarTB.Text = "Exporting data to Excel file. "
thread = New System.Threading.Thread(AddressOf exportToExcel)
thread.Start()
While Not thread.IsAlive
statusBarTB.Text = "Finished recording data to Excel file! "
End While
End Sub
I tried with this "finished" boolean flag but the window gets stuck until the export has finished, which is not what I want. Why do i use thread in the first place if it freezes the window thread.
How can I properly notify user when thread has started and has finished?
Does VB.NET Threading API have stuff like addEventListener with which I can add functions and execute those on certain events?
-
May 10th, 2019, 03:57 AM
#2
Re: Notify user when thread has started and when it has finished?
Your While loop is a busy-loop, which will keep the window too busy to do anything else.
One thing you could do is have add code at the end of the exportToExcel routine to set the value itself (by invoking), eg:
Code:
private sub exportToExcel()
' exporting code
statusBarTB.Invoke(Sub() statusBarTB.Text = "Finished recording data to Excel file! ")
end sub
Note however that if this might also be run on the UI thread (rather than a new one), you should use InvokeRequired. There is more info about that here:
http://www.vbforums.com/showthread.p...he-UI-question
Alternatively you can add a BackgroundWorker to your form, which is a thread template that automatically has a Completed event that runs on the UI thread (no invoking required).
Last edited by si_the_geek; May 10th, 2019 at 08:23 AM.
Reason: typo
-
May 10th, 2019, 03:58 AM
#3
Addicted Member
Re: Notify user when thread has started and when it has finished?
You might want to look at the Backgroundworker class.
It has a "RunWorkerCompleted" event.
Utterly useless, but always willing to help
As a finishing touch god created the dutch
-
May 10th, 2019, 07:11 AM
#4
Re: Notify user when thread has started and when it has finished?
Here is a very nice example of a background worker with progress displayed.
'http://www.vbforums.com/showthread.php?680130-Correct-way-to-use-the-BackgroundWorker
Please remember next time...elections matter!
-
May 10th, 2019, 08:21 AM
#5
Re: Notify user when thread has started and when it has finished?
Here is your code modified to use Task and Await. Note the UI will remain responsive while the task is running.
Code:
Private Sub exportToExcel()
' exporting code
Threading.Thread.Sleep(1000) 'simulate work
'when complete
finished.Set()
End Sub
Private finished As New Threading.ManualResetEvent(False)
Private ThreadExp As Task
Private Async Sub exportToExcelBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exportToExcelBtn.Click
finished.Reset()
statusBarTB.Text = "Exporting data to Excel file. "
ThreadExp = Task.Run(Sub() exportToExcel())
Await ThreadExp
statusBarTB.Text = "Finished recording data to Excel file! "
End Sub
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|