7 Replies - 354 Views - Last Post: 01 May 2012 - 10:10 AM Rate Topic: -----

Topic Sponsor:

#1 TheCodeNoob  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 29
  • Joined: 21-November 11

Self-Stopping Timer won't stop itself

Posted 06 February 2012 - 07:04 PM

I've been trying to create a timer that waits for another timer to finish with its coding and, when it's finished, executes a series of code before stopping itself.

This isn't the code for the actual program I wanted to implement this self-stopping timer in, but it's simple and has the exact same problem:

Public Class Form1
    Private Synchro As New Timer()

    Private Sub MeLoad() Handles Me.Load
        AddHandler Synchro.Tick, AddressOf Synchronize
    End Sub

    Private Sub Buttonclick() Handles Button1.Click
        Synchro.Enabled = True
    End Sub

    Private Sub Synchronize()
        Static Disable As Boolean
        If Disable = False Then
            MessageBox.Show("If this is the only messagebox, then the code worked.")
            Disable = True
            Synchro.Enabled = False
            Synchro.Stop()
            Exit Sub
        End If
    End Sub

End Class


The idea here is that it's suppose to (using a timer) show the messagebox ONCE and ONLY once per button click. However, what seems to happen (despite the fact that I have 4 ways of trying to tell it to stop) is that the program creates multiple messageboxes until there are a total of about 49 messageboxes on the screen at the same time. I don't know if I'm using the wrong event (unlikely as Tick seems to be the only timer event) or there's a way of stopping the timer that I don't know.

Is This A Good Question/Topic? 0
  • +

Replies To: Self-Stopping Timer won't stop itself

#2 modi123_1  Icon User is offline

  • Suiter #2
  • member icon


Reputation: 3560
  • View blog
  • Posts: 14,989
  • Joined: 12-June 08

Re: Self-Stopping Timer won't stop itself

Posted 06 February 2012 - 07:36 PM

Look at the scope of your variable 'disable'.. it is limited to that method. Perhaps creating it globally would work for ya.
Was This Post Helpful? 0
  • +
  • -

#3 TheCodeNoob  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 29
  • Joined: 21-November 11

Re: Self-Stopping Timer won't stop itself

Posted 06 February 2012 - 07:52 PM

Didn't help what so ever.
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 757
  • View blog
  • Posts: 2,822
  • Joined: 02-July 08

Re: Self-Stopping Timer won't stop itself

Posted 06 February 2012 - 08:05 PM

Why do you need a timer, if all it does is show a message box?
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Self-Stopping Timer won't stop itself

Posted 06 February 2012 - 09:01 PM

Other things I dont see the use of that Disable variable, because on every call of that sub it will be false and so the if condition will be true. Then that also means there is no need of that if condition because it is always true.!!
Also I dont see the reason of the Exit Sub there, because the method is going to end anyway.

Now going to your question, the answer is in the MSDN Documentation, it says:

Quote

Note
The signal to raise the Elapsed event is always queued for execution on a ThreadPool thread. This might result in the Elapsed event being raised after the Enabled property is set to false. The code example for the Stop method shows one way to work around this race condition.


This is for Timer from System.Timers, if you need to use its solution, then you can change the declaration of your timer to(it is a server based timer):
Private Synchro As New System.Timers.Timer()

Also you will need to raise Elapsed event instead of tick. Then you can have the AutoReset property to do your work:

Quote

If Enabled is set to true and AutoReset is set to false, the Timer raises the Elapsed event only once, the first time the interval elapses.

And it will work for the problem

Another hint to your code, you have:
Synchro.Enabled = False
Synchro.Stop()
No need for both because:

Quote

Setting Enabled to true is the same as calling Start, while setting Enabled to false is the same as calling Stop.

This post has been edited by smohd: 06 February 2012 - 09:03 PM

Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Self-Stopping Timer won't stop itself

Posted 06 February 2012 - 09:25 PM

Another simpler solution for your problem, and may be that was the real problem of your code:
You didnt set the interval for the timer and is default to 100ms, this time is very small for the message box to show and you click OK to allow other execution to continue before another event is raised. That caused the problem because when message box is raised, it will wait for your input and stops Form thread(which is the one the form timer used) from running. That means always the
Synchro.Enabled = False
is not reached and the new event starts. To prove this put a break in your Synchronize() and see only after the message box shows, a new event is raised!!!

So to solve it, put Synchro.Enabled = False the first thing before message box to show.
Was This Post Helpful? 1
  • +
  • -

#7 Ionut  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 327
  • View blog
  • Posts: 914
  • Joined: 17-July 10

Re: Self-Stopping Timer won't stop itself

Posted 07 February 2012 - 05:48 AM

I see this a problem that can be easily solved by using events.
Here is a tutorial from MSDN(it is in C#, but I think you can get the idea)
Another link on MSDN
Also, you can check the tutorial section of this site for relevant information.
Was This Post Helpful? 1
  • +
  • -

#8 drewcifer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-May 12

Re: Self-Stopping Timer won't stop itself

Posted 01 May 2012 - 10:10 AM

The messagebox event is preventing the .stop() function from triggering. Try disabling the timer before the messagebox is called
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1