|
-
Oct 4th, 2010, 01:52 PM
#1
Thread Starter
Frenzied Member
Pause Not Pausing
I have a function that i send it a int like 3, and the function will pause for 3 seconds then move on..
However... i cant seem to get the pause to work correctly, it just shoots right through the function.
Code:
'Function To Control Pause / Wait Time Selected
Private Sub Pause(ByVal seconds As Integer)
Dim returnTime As Date = Now.AddSeconds(seconds)
Do While Now < returnTime
Application.DoEvents()
Threading.Thread.Sleep(100)
Loop
End Sub
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Try
Pause(3)
MsgBox("OK")
Pause(3)
Catch ex As Exception
Pause(3)
MsgBox("Error")
Pause(3)
End Try
End Sub
-
Oct 4th, 2010, 02:32 PM
#2
Lively Member
Re: Pause Not Pausing
maybe you can simply do
Private Sub Pause(ByVal nSeconds As Integer)
System.Threading.Thread.Sleep(nSeconds * 1000)
Last edited by Guerrero; Oct 4th, 2010 at 02:41 PM.
-
Oct 4th, 2010, 02:34 PM
#3
Thread Starter
Frenzied Member
Re: Pause Not Pausing
I think when you do that, it freezes the app, i dont want it to freeze the app
-
Oct 4th, 2010, 02:50 PM
#4
Lively Member
Re: Pause Not Pausing
 Originally Posted by joefox
I think when you do that, it freezes the app, i dont want it to freeze the app
I use this technique in several sites and don't understand the freeze you comment.
I think there is more freeze when doevents called each 100 milliseconds.
maybe call doevents only one time each second.
don't sure how to test the freezes you comment.
-
Oct 4th, 2010, 02:56 PM
#5
Thread Starter
Frenzied Member
Re: Pause Not Pausing
I just tried it, and it dosent give the desired results..
-
Oct 4th, 2010, 03:00 PM
#6
Lively Member
Re: Pause Not Pausing
 Originally Posted by joefox
I just tried it, and it dosent give the desired results..
what is "freeze"?
some of blinking controls in the form?
(btw I don't lile to using maybe-reserved-words as "seconds" as parameters)
-
Oct 4th, 2010, 03:05 PM
#7
Thread Starter
Frenzied Member
Re: Pause Not Pausing
yes it acts like its trying to process something.
before in my other apps it would make the function wait... then go not freezing the app
-
Oct 4th, 2010, 03:13 PM
#8
Lively Member
Re: Pause Not Pausing
I've made a form with pause(9) and works fine.
without using doevents,
but using the full sentence (;-)
System.Threading.Thread.Sleep(9 * 1000)
-
Oct 4th, 2010, 03:14 PM
#9
Lively Member
Re: Pause Not Pausing
 Originally Posted by joefox
yes it acts like its trying to process something.
before in my other apps it would make the function wait... then go not freezing the app
sure... your doevents....
-
Oct 4th, 2010, 03:20 PM
#10
Re: Pause Not Pausing
Not the way to go (i.e. do NOT use sleep like this to 'pause' your application).
Depending on what you want to do, you can simply enable (start) a timer, after setting the desired interval (in milliseconds). When the timer tick event fires, you can continue processing, and disable the timer.
Really, though, you need to rethink the necessity of a 'pause'. Windows is an event driven system, and as such, you need to respond to events through event driven programming. Timers are used to generate interval-based events for you to respond to.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Oct 4th, 2010, 03:43 PM
#11
Thread Starter
Frenzied Member
Re: Pause Not Pausing
I have the pause working at home, but i have the full version of VS2010, here at work i have Express version of 2010 maybe thats why my "pause" style is not working?
-
Oct 4th, 2010, 05:54 PM
#12
Re: Pause Not Pausing
Threading.Thread.Sleep doesn't freeze the application, just the thread. Of course, that doesn't make any practical difference if you are not using multiple threads. One way to pause something without freezing the app is to use a separate thread. An easy way to do that to use a System.Timers.Timer (not a Windows.Forms.Timer!). Its Elapsed event fires on a separate thread. Here's an example of a Delay sub that uses it:
Code:
Private WithEvents tim As New System.Timers.Timer With {.Enabled = False, .AutoReset = False}
Private delayMessage As String
Private Sub Delay(ByVal message As String, ByVal seconds As Single)
tim.Interval = seconds * 1000
delayMessage = message
tim.Start()
End Sub
Private Sub tim_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles tim.Elapsed
MessageBox.Show(delayMessage)
End Sub
There are some limitations to what you can do in the Elapsed sub, though. A MessageBox works, and so does MsgBox. But you can't change anything directly in the UI such as setting a TextBox text, because you get a threading error. Instead, you could raise an event and handle it.
BB
-
Oct 4th, 2010, 06:34 PM
#13
Re: Pause Not Pausing
What is the pause for? It looks like you are trying to "pause" the UI for 3 seconds, then show a message box, and after the user presses OK, "pause" the UI for 3 more seconds. As has been pointed out this is not something you should do, but without knowing your reasoning it is hard to provide help.
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
|