Stopwatch and Checking for Events
Stopwatch and Checking for Events
Stopwatch and Checking for Events
Stopwatch and Checking for Events
Stopwatch and Checking for Events
Stopwatch and Checking for Events Stopwatch and Checking for Events Stopwatch and Checking for Events Stopwatch and Checking for Events Stopwatch and Checking for Events Stopwatch and Checking for Events Stopwatch and Checking for Events Stopwatch and Checking for Events
Stopwatch and Checking for Events Stopwatch and Checking for Events
Stopwatch and Checking for Events
Go Back  Xtreme Visual Basic Talk > > > Stopwatch and Checking for Events


Reply
 
Thread Tools Display Modes
  #1  
Old 11-19-2014, 05:18 AM
bobcory's Avatar
bobcory bobcory is offline
Contributor
 
Join Date: Jun 2003
Location: NW UK
Posts: 454
Question Stopwatch and Checking for Events


I have just re-written my time delay function to use the stopwatch, as follows:

Code:
Public Sub TimeDelayLT(intPeriod As Integer)
  ' intPeriod is in milliseconds

  Dim intInterval As Integer = 5
  Dim stpWatch   As New Stopwatch

  ' Start the stopwatch
  stpWatch.Start

  ' Minimum period 
  If intPeriod < 5 then
    intPeriod  = intInterval
  End If
  
  Do While stpWatch.ElapsedMilliseconds < intPeriod
    SleepLUD(intInterval) 
    Application.DoEvents
    If blnLeaveTimeDelayFP = True then
      blnLeaveTimeDelayFP = False 
      Exit Do 
    End If
  Loop

End Sub
Ideally, I need some way of finding out if a user has pressed any button in which case it should leave eg

Code:
  Do While stpWatch.ElapsedMilliseconds < intPeriod
    SleepLUD(intInterval) 
    Application.DoEvents
    If AplicationButtonPressed = True then Exit Do
  Loop
Any ideas?
__________________
MOPEKS - a freeware program that generates programs that use each other to solve problems. Is this the correct route to a genuinely intelligent machine?
.
Reply With Quote
  #2  
Old 11-19-2014, 05:52 AM
PlausiblyDamp's Avatar
PlausiblyDampStopwatch and Checking for Events PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

I suppose you could always have some global variable or similar that the button sets and you are checking inside of this delay function, although I would probably really avoid this if I could.

You might want to turn this into a class so you could create an instance of the class every time you wanted a delay, this way you could make the cancel flag a property of the object.

Out of interest how is this delay being used, there might be a better approach to what you are trying to do, I would tend to try and avoid looping (even with a DoEvents) as it can get very processor intensive.

What version of .Net / VS are you using?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 11-19-2014, 06:27 AM
bobcory's Avatar
bobcory bobcory is offline
Contributor
 
Join Date: Jun 2003
Location: NW UK
Posts: 454
Default

Thanks for the detailed reply

Quote:
Originally Posted by PlausiblyDamp View Post
I suppose you could always have some global variable or similar that the button sets and you are checking inside of this delay function, although I would probably really avoid this if I could.
That is what I do currently but I hoped to drop that!

Quote:
Originally Posted by PlausiblyDamp View Post
You might want to turn this into a class so you could create an instance of the class every time you wanted a delay, this way you could make the cancel flag a property of the object.
Good idea!

Quote:
Originally Posted by PlausiblyDamp View Post
Out of interest how is this delay being used?
A number of ways
1. When I run a script, it pauses to give the user time to see what is happening - typically 50 ms
2. I have found that when I run some things it needs a slight delay to actually execute properly eg "PerformClick()"
3. When user runs multiple copies of the app, it delays a small random time so that the random number generator uses a different set of seeds for each copy (based on time etc)

Quote:
Originally Posted by PlausiblyDamp View Post
I would tend to try and avoid looping (even with a DoEvents) as it can get very processor intensive.
Note that it sleeps for 5 milliseconds - so it checks events 200 times per second (it takes a 250,000th of a second to do this). So it is sleeping about 99.99% of the time. In practice, no delays are longer than one second or so

I use
Code:
Public Declare Sub SleepLUD Lib "kernel32" Alias "Sleep" (intMilliseconds As Integer)
I know vb.net has a sleep function but this works ...

Quote:
Originally Posted by PlausiblyDamp View Post
What version of .Net / VS are you using?
Visual Studio 2013
__________________
MOPEKS - a freeware program that generates programs that use each other to solve problems. Is this the correct route to a genuinely intelligent machine?
.
Reply With Quote
  #4  
Old 11-19-2014, 10:29 AM
PlausiblyDamp's Avatar
PlausiblyDampStopwatch and Checking for Events PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

As you are using a recent version of .Net you could take advantage of the Task Parallel Library and the Task object.

If you create a simple form with two buttons and paste the following code...
Code:
Private cancellationToken As CancellationTokenSource

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    cancellationToken = New CancellationTokenSource()
    Dim interval As Integer = 10000
        Try
            Await Task.Delay(interval, cancellationToken.Token)
            MessageBox.Show("dfdsfsd")
        Catch Exception As TaskCanceledException
            Debug.WriteLine("Cancelled")
        End Try
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    cancellationToken.Cancel()
End Sub
You can see how the Task.Delay will pause execution for a set amount of time, without blocking the UI and can also easily be cancelled.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
Reply

Tags
application.doevents, delay, mopeks, stopwatch, timer


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Stopwatch and Checking for Events
Stopwatch and Checking for Events
Stopwatch and Checking for Events Stopwatch and Checking for Events
Stopwatch and Checking for Events
Stopwatch and Checking for Events
Stopwatch and Checking for Events Stopwatch and Checking for Events Stopwatch and Checking for Events Stopwatch and Checking for Events Stopwatch and Checking for Events Stopwatch and Checking for Events Stopwatch and Checking for Events
Stopwatch and Checking for Events
Stopwatch and Checking for Events
 
Stopwatch and Checking for Events
Stopwatch and Checking for Events
 
-->