Click here to Skip to main content
16,006,013 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: Wait in Background Worker Pin
Gideon Engelberth20-Aug-08 7:44
Gideon Engelberth20-Aug-08 7:44 
Yes (and yes). There are two ways that come to mind if you need this kind of a wait. If you will not ever need to cancel the wait, you can just use Thread.Sleep, but if you may need to cancel (eg: to shut the thread down immediately), you can use an AutoResetEvent (or one of the other similar System.Threading classes).

As far as the variable wait time, just encapsulate the DoWork event handler in a class with the wait time as a field or property.

Here's a simple console app sample to demonstrate the two ways. Uncomment the lines and comment the Thread.Sleep line to use the AutoResetEvent instead.

Dim waitTime As Integer
Dim handle As New System.Threading.AutoResetEvent(False)

Sub Main()
    Dim rand As New System.Random
    waitTime = rand.Next(10, 20)
    Dim bw As New System.ComponentModel.BackgroundWorker
    AddHandler bw.DoWork, AddressOf WorkerRoutine

    Console.WriteLine("Wait time will be {0} seconds", waitTime)
    bw.RunWorkerAsync()
    'Console.WriteLine("Press any key to interrupt the worker.")
    'Console.ReadKey()
    'handle.Set()
     
    Console.WriteLine("Program complete...")
    Console.ReadKey()
End Sub

Private Sub WorkerRoutine(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
    Console.WriteLine("Beginning work")
    System.Threading.Thread.Sleep(waitTime * 1000)  'gives a non-interruptable sleep
    'handle.WaitOne(waitTime * 1000, False)   'gives an interruptable "sleep" period
    Console.WriteLine("Ending work")
End Sub

QuestionConnect to a password protected Access database!! Pin
John Kh20-Aug-08 7:09
John Kh20-Aug-08 7:09 
AnswerRe: Connect to a password protected Access database!! Pin
Kschuler20-Aug-08 10:25
Kschuler20-Aug-08 10:25 
AnswerRe: Connect to a password protected Access database!! Pin
emimmortal20-Aug-08 19:01
emimmortal20-Aug-08 19:01 
GeneralRe: Connect to a password protected Access database!! Pin
John Kh22-Aug-08 22:56
John Kh22-Aug-08 22:56 
QuestionVB:net mysql subtract value Pin
Pestinha198420-Aug-08 6:06
Pestinha198420-Aug-08 6:06 
AnswerRe: VB:net mysql subtract value Pin
Gideon Engelberth20-Aug-08 7:45
Gideon Engelberth20-Aug-08 7:45 
GeneralRe: VB:net mysql subtract value Pin
Pestinha198420-Aug-08 7:53
Pestinha198420-Aug-08 7:53 
QuestionHow can i count bytes in an array of bytes ? Pin
Kobi_Z20-Aug-08 5:00
Kobi_Z20-Aug-08 5:00 
GeneralRe: How can i count bytes in an array of bytes ? Pin
Kschuler20-Aug-08 10:27
Kschuler20-Aug-08 10:27 
GeneralRe: How can i count bytes in an array of bytes ? Pin
Kobi_Z21-Aug-08 9:24
Kobi_Z21-Aug-08 9:24 
QuestionFunction declaration that returns array Pin
indian14320-Aug-08 3:58
indian14320-Aug-08 3:58 
AnswerRe: Function declaration that returns array Pin
cstrader23220-Aug-08 4:31
cstrader23220-Aug-08 4:31 
AnswerRe: Function declaration that returns array Pin
MikeMarq20-Aug-08 6:47
MikeMarq20-Aug-08 6:47 
GeneralThe 1 votes Pin
MikeMarq20-Aug-08 15:46
MikeMarq20-Aug-08 15:46 
GeneralRe: The 1 votes Pin
cstrader23220-Aug-08 16:17
cstrader23220-Aug-08 16:17 
GeneralRe: The 1 votes Pin
MikeMarq20-Aug-08 16:25
MikeMarq20-Aug-08 16:25 
GeneralRe: The 1 votes Pin
Mycroft Holmes20-Aug-08 19:16
professionalMycroft Holmes20-Aug-08 19:16 
GeneralRe: The 1 votes Pin
MikeMarq21-Aug-08 7:04
MikeMarq21-Aug-08 7:04 
QuestionConverting C# code to Vb.net Pin
dix20-Aug-08 3:46
dix20-Aug-08 3:46 
AnswerRe: Converting C# code to Vb.net Pin
Steven J Jowett20-Aug-08 11:48
Steven J Jowett20-Aug-08 11:48 
AnswerRe: Converting C# code to Vb.net Pin
Dave Doknjas20-Aug-08 14:29
Dave Doknjas20-Aug-08 14:29 
QuestionComputer Science 101 question Pin
cstrader23220-Aug-08 2:57
cstrader23220-Aug-08 2:57 
AnswerRe: Computer Science 101 question Pin
MikeMarq20-Aug-08 6:39
MikeMarq20-Aug-08 6:39 
GeneralRe: Computer Science 101 question Pin
cstrader23220-Aug-08 7:52
cstrader23220-Aug-08 7:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.