making vb.net sleep for 30 seconds

Associate
Joined
8 Mar 2007
Posts
2,176
Location
between here and there
all hey,

I need to make vb.net sleep (or wait) for 30 seconds before continuing to run the next line.

I have tried;

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

sleep(5000)

but i got

"unable to find an entry point named 'sleep' in DLL kernel32" and it error's on the sleep (5000) line


any ideas anyone??
cheers
 
hey cheers,

I'm new to this so forgive me......

that runs great, only issue now is that I'm trying to run a public sub (which runs a dos command and out puts to a listbox) Then the sleep line, then another dos command to be displayed to the same place.

I'd like to click "go" and have the first displayed and then the system wait's however long and runs and displays the second one.

any ideas?

mutlithreading prehaps?
 
hey cheers,

I'm new to this so forgive me......

that runs great, only issue now is that I'm trying to run a public sub (which runs a dos command and out puts to a listbox) Then the sleep line, then another dos command to be displayed to the same place.

I'd like to click "go" and have the first displayed and then the system wait's however long and runs and displays the second one.

any ideas?

mutlithreading prehaps?

Again not quite sure what you mean ?

cant you use run the shell command > output to listbox > use the thread sleep method as posted above then run > the second shell command > output to listbox ?
 
sorry guys,

I'd had bottle of wine and it was after a hard days work. Having just re-read it i see what you mean.:rolleyes:

Since it's now 11.15 in the morning let see if i can more sense!!:p

this is my first real vb.net app that I've written. It takes over from a batch file menu that calls other batch files to do certain tasks. (such as copying files to remote Pc's, fixing the missing toolbar in excel, restarting services and Pc's and most recently helping with an install that we're doing at over 300 sites with around 5 Pc's each. - .ini files get made with correct details for site and files get transfered.

At first i had just used shell to call the batch file that did the work. I then decided it would look much nicer if the output from the batch file was brought back in to vb in a status window (listbox)

The problem i had was i that really wanted the lines to appear in the status (listbox) window as they where run in the batch file. This was proving to be a pain, so i decided that i had might as well just write in vb.net the commands that the batch file was running, that way i could get it to display as it ran.

the issue i now have is that my form doesn't display each command as it is run, it still waits for the whole form to finish.

heres the function called dos;

Code:
Imports System.io
Module dos
    Public Sub dos1(ByVal fileName As String, ByVal arguments As String)
        Dim p As New Process()

        With p
            .StartInfo.Arguments = arguments
            .StartInfo.CreateNoWindow = True
            .StartInfo.FileName = fileName
            .StartInfo.RedirectStandardOutput = True
            .StartInfo.UseShellExecute = False
            .Start()

            Dim reader As StreamReader = .StandardOutput

            While (Not reader.EndOfStream)
                Formstatus.statuslist.Items.Add(String.Format("{0}", reader.ReadLine))
                Formstatus.statuslist.HorizontalScrollbar = True
                Formstatus.Show()
            End While

            .WaitForExit()
        End With
        p.Close()
    End Sub

and heres the form where the "testmerun" button is clicked;

Code:
 Public Class Formtestme

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles testmerun.Click
        dos.dos1("cmd", "/c copy c:\" + Test.Text + ".txt c:\temp\nice2.txt")


    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles testmerun.Click
        System.Threading.Thread.Sleep(15 * 1000)
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles testmerun.Click
        dos.dos1a("cmd", "/c copy c:\" + Test.Text + ".txt c:\temp\nice3.txt")
    End Sub
End Class

i reckon its something simple. (just beyond me!!)

i know that i could just use some fso to copy files, but I've started down this route so I'd like to finish it. (also, helps with the learning process to complete each project.)

hope that makes more sense!!

blastman
 
Last edited:
You should have used a multiline textbox rather than a list box.

Also it will hang you application as you are running all your intensive commands on the main thread. You will need to use threading or a background worker if you want you main for accessable while doing intensive work.

EDIT: Making you app sleep for 30 seconds is not really wise.

TrUz
 
Last edited:
hey TrUz, hows things?

tbh the sleep command was A, me learning bit more and B, making sure that it did if fact display each command as it ran and not once all of it finished.

you suggest threading or a background worker if i want to do other things with my app while it's doing the first. tbh, it is really only ever going to be used to run one command (say restarting a service) at a time and then closed, so i reakon a background worker is propbley a but much.

so threading it is. (not that i know anything about it!!)

sounds like a google question to me. Is there anything i should be looking for of would you suggest just reading up on threading first?

cheers
 
BackgroundWorker is pretty much the same as Threading except it is a little more managed. Both will be as good a option as the other. Which ever you find easiest will be best. :)

TrUz
 
I don't think you need another thread as you're not really doing much in your UI thread anyway. So uou could try something like this:

Dim SOut As System.IO.StreamReader = myProcess.StandardOutput

Dim exited As Boolean = False

While Not exited
exited = myProcess.HasExited

While (Not SOut.EndOfStream)
Formstatus.statuslist.Add(SOut.ReadLine())
End While

Application.DoEvents()

Thread.Sleep(1000)
End While
 
I don't think you need another thread as you're not really doing much in your UI thread anyway. So uou could try something like this:

Dim SOut As System.IO.StreamReader = myProcess.StandardOutput

Dim exited As Boolean = False

While Not exited
exited = myProcess.HasExited

While (Not SOut.EndOfStream)
Formstatus.statuslist.Add(SOut.ReadLine())
End While

Application.DoEvents()

Thread.Sleep(1000)
End While
No point DoEvents() out side of the loop. The application is still going to hang while looping. Might be ok if you put DoEvents() in the loop. Threading would be the more professional approach and you get to learn something in the process.

EDIT: Use a multiline textbox instead of a list box for this kinda thing. Use the text box like:
Code:
Me.TextBox1.AppendText(String.Format("{0}" & vBCrLf, SOut.ReadLine()))
TrUz
 
No point DoEvents() out side of the loop. The application is still going to hang while looping. Might be ok if you put DoEvents() in the loop. Threading would be the more professional approach and you get to learn something in the process.
No it won't hang in the inner loop. It simply flushes the stream out to the UI. And you really don't want or need to process window messages every single time an EOL occurs in the output stream.

The most professional solution is always the simplest one in my opinion. Besides, cross-threaded UI access is a minefield for a novice developer.
 
Back
Top Bottom