Code Snippets

  

VB.NET Source Code


Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 148,801 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,195 people online right now. Registration is fast and FREE... Join Now!





Multi-threading with background worker

Enables to take advantage of multi-possessors.

Submitted By: m2s87
Actions:
Rating:
Views: 11,443

Language: VB.NET

Last Modified: January 10, 2007
Instructions: Save it in a new class file

Write your code to the BackgroundWorker1_DoWork protsedure.

deklelare it like:
Dim x As New tee
start it like:
x.startBackgroundTask()

Snippet


  1. 'written by Margus Martsepp AKA m2s87
  2. Imports System.Threading
  3. Public Class tee
  4.     Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _
  5.     ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
  6.         ' Add your code here
  7.  
  8.     End Sub
  9.     Private EndedAt As String, StartedAt As String
  10.     Private tegutseb As Boolean = False, Notifieonend As Boolean
  11.     Private WithEvents BackgroundWorker1 As New System.ComponentModel.BackgroundWorker
  12.     Public Sub startBackgroundTask() ' This will start the backgroundworker
  13.         tegutseb = True
  14.         StartedAt = "Started : " & Format(Now, "h:mm:ss") & "." & Now.Millisecond
  15.         BackgroundWorker1.RunWorkerAsync()
  16.     End Sub
  17.     Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _
  18.     ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) _
  19.     Handles BackgroundWorker1.RunWorkerCompleted ' Compleated
  20.         tegutseb = False
  21.         EndedAt = "Ended   : " & Format(Now, "h:mm:ss") & "." & Now.Millisecond
  22.         If Notifieonend = True Then MsgBox(Timestamp, , "Protsess Done")
  23.     End Sub
  24.     Public ReadOnly Property Timestamp() As String
  25.         Get
  26.             Return StartedAt & Chr(13) & EndedAt
  27.         End Get
  28.     End Property
  29.     Public ReadOnly Property IsWorking() As Boolean
  30.         Get
  31.             Return tegutseb
  32.         End Get
  33.     End Property
  34.     Public Property Notifie_on_end() As Boolean
  35.         Get
  36.             Return Notifieonend
  37.         End Get
  38.         Set(ByVal value As Boolean)
  39.             Notifieonend = value
  40.         End Set
  41.     End Property
  42. End Class

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month