Results 1 to 5 of 5

Thread: Notify user when thread has started and when it has finished?

  1. #1

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    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?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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

  3. #3
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    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

  4. #4
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    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!

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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
  •  



Click Here to Expand Forum to Full Width