PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 02/03 Raise Event-VBForums
Results 1 to 14 of 14

Thread: Raise Event

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Raise Event

    Hello There,

    i just to ask some question regarding on how to raise an event in vb2003

    this my code:

    Code:
    Public Class Form1
    Public Event TimerStart()
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    RaiseEvent TimerStart
    End Sub End Class
    this is my recieving class
    Code:
    Public Class Form2
    Private WithEvents frm as Form1
    
    Private Sub TimerReceiver()Handles frm.TimerStart
    
    Messagebox.show("Testing")
    End Sub End Class
    please give me suggestion if what im done wrong here..thanks!

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Raise Event

    I'm not really sure, but I think you need to declare your event 'As EventHandler'. I've always done it that way, but I'm not sure if it's absolutely required.

    If you do that, then your method signature (of TimerReceiver) should also accept a sender As Object and a 'e' As EventArgs (in that order).

  3. #3
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Raise Event

    What concerns raising of events:
    vb.net Code:
    1. ' A form code for testing
    2. Public Class Form1
    3.     Public Catcher As New CatcherClass
    4.  
    5.     Public Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    6.         Catcher.RaiseWithEvents()
    7.         Catcher.RaiseWithoutWithEvents()
    8.     End Sub
    9. End Class
    10.  
    11. ' Events example
    12. 'General systax:
    13.  
    14. ' Declaring an event
    15. Public Class SomeClass
    16.     ' Our Event
    17.     Public Event MyEvent() ' You can add parameters here
    18.  
    19.     ' This Sub is for test purposes
    20.     ' It simply fires our event
    21.     Sub SomeWork()
    22.         RaiseEvent MyEvent()
    23.     End Sub
    24. End Class
    25.  
    26. ' Catching an Event
    27. ' There are two ways you can assign an eventhandler to your event
    28. ' 1. Declare the class using WithEvents keyword
    29. ' 2. Use AddHandler statement
    30. ' This example covers both cases
    31.  
    32. Public Class CatcherClass
    33.  
    34.     ' Note tha we declare two instances of the same class
    35.     ' but using different declarations:
    36.  
    37.     'Declaration with 'WithEvents'
    38.     Dim WithEvents mycls1 As New SomeClass
    39.  
    40.     ' Declaration without 'WithEvents'
    41.     Dim mycls2 As New SomeClass
    42.  
    43.     ' Adding a handler for the declaration without Withevents
    44.     ' (preferred way)
    45.     Public Sub New()
    46.         AddHandler mycls2.MyEvent, AddressOf WithoutWitheventsHandler
    47.     End Sub
    48.  
    49.     ' For test purposes - raising an event
    50.     ' Well we need something that will fire the event
    51.     ' In a real life this can be any condition
    52.     Sub RaiseWithEvents()
    53.         mycls1.SomeWork()
    54.     End Sub
    55.  
    56.     Sub RaiseWithoutWithEvents()
    57.         mycls2.SomeWork()
    58.     End Sub
    59.  
    60.     Sub WithEventsHandler() Handles mycls1.MyEvent ' Note the Handles keyword
    61.         MsgBox("Event handler for Withevents declaration.")
    62.     End Sub
    63.  
    64.     Sub WithoutWitheventsHandler() ' Note the absense of the Handles keyword
    65.         MsgBox("Event handler for declaration without Withevents.")
    66.     End Sub
    67. End Class

    What concerns Timer:
    Your code didn't have any timer object. In order for Timer to work you need:
    1. Set the timer interval
    2. Enable it

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Raise Event

    None of that is relevant to this thread I think. He is raising the event when a button is clicked, nothing to do with timers (except the name of the event). In the receiving class he's declaring the form WithEvents, and is using Handles correctly.

    So as long as the form in variable 'frm' is actually being shown, and the button on there being clicked, then it should raise the event (assuming you don't need the 'As EventHandler' as I said in my first post).

  5. #5
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Raise Event

    Quote Originally Posted by NickThissen View Post
    None of that is relevant to this thread I think. He is raising the event when a button is clicked, nothing to do with timers (except the name of the event). In the receiving class he's declaring the form WithEvents, and is using Handles correctly.

    So as long as the form in variable 'frm' is actually being shown, and the button on there being clicked, then it should raise the event (assuming you don't need the 'As EventHandler' as I said in my first post).
    You don't.

    But I still think that he has some issues with timers.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Re: Raise Event

    Good Day Guys,

    First, i would like to thank you all for your quick reply.

    im very sorry for my confussing question..what i really want to do in here is...i want to Start the timer object at Class Form2..from Class Form1..and i think using event i can handle it..

  7. #7
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Raise Event

    Quote Originally Posted by NickThissen
    I'm not really sure, but I think you need to declare your event 'As EventHandler'. I've always done it that way, but I'm not sure if it's absolutely required.
    Not required, but the event stub will argument-less. Probably not good practice... I would think Microsoft recommends that all events should at least have the standard EventHandler signature, dunno.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Re: Raise Event

    Quote Originally Posted by cicatrix View Post
    What concerns raising of events:
    vb.net Code:
    1. ' A form code for testing
    2. Public Class Form1
    3.     Public Catcher As New CatcherClass
    4.  
    5.     Public Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    6.         Catcher.RaiseWithEvents()
    7.         Catcher.RaiseWithoutWithEvents()
    8.     End Sub
    9. End Class
    10.  
    11. ' Events example
    12. 'General systax:
    13.  
    14. ' Declaring an event
    15. Public Class SomeClass
    16.     ' Our Event
    17.     Public Event MyEvent() ' You can add parameters here
    18.  
    19.     ' This Sub is for test purposes
    20.     ' It simply fires our event
    21.     Sub SomeWork()
    22.         RaiseEvent MyEvent()
    23.     End Sub
    24. End Class
    25.  
    26. ' Catching an Event
    27. ' There are two ways you can assign an eventhandler to your event
    28. ' 1. Declare the class using WithEvents keyword
    29. ' 2. Use AddHandler statement
    30. ' This example covers both cases
    31.  
    32. Public Class CatcherClass
    33.  
    34.     ' Note tha we declare two instances of the same class
    35.     ' but using different declarations:
    36.  
    37.     'Declaration with 'WithEvents'
    38.     Dim WithEvents mycls1 As New SomeClass
    39.  
    40.     ' Declaration without 'WithEvents'
    41.     Dim mycls2 As New SomeClass
    42.  
    43.     ' Adding a handler for the declaration without Withevents
    44.     ' (preferred way)
    45.     Public Sub New()
    46.         AddHandler mycls2.MyEvent, AddressOf WithoutWitheventsHandler
    47.     End Sub
    48.  
    49.     ' For test purposes - raising an event
    50.     ' Well we need something that will fire the event
    51.     ' In a real life this can be any condition
    52.     Sub RaiseWithEvents()
    53.         mycls1.SomeWork()
    54.     End Sub
    55.  
    56.     Sub RaiseWithoutWithEvents()
    57.         mycls2.SomeWork()
    58.     End Sub
    59.  
    60.     Sub WithEventsHandler() Handles mycls1.MyEvent ' Note the Handles keyword
    61.         MsgBox("Event handler for Withevents declaration.")
    62.     End Sub
    63.  
    64.     Sub WithoutWitheventsHandler() ' Note the absense of the Handles keyword
    65.         MsgBox("Event handler for declaration without Withevents.")
    66.     End Sub
    67. End Class

    What concerns Timer:
    Your code didn't have any timer object. In order for Timer to work you need:
    1. Set the timer interval
    2. Enable it
    Thanks you very much!..this will be a great help.and additional information to me regarding raising an event..thank you guys.

  9. #9
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Raise Event

    Quote Originally Posted by [gja] View Post
    Good Day Guys,

    First, i would like to thank you all for your quick reply.

    im very sorry for my confussing question..what i really want to do in here is...i want to Start the timer object at Class Form2..from Class Form1..and i think using event i can handle it..
    You don't raise timer events yourself, it's timer's job.

    There are three basic types of timers in .NET.

    Here is a little demonstration of all. Read the comments.
    • System.Windows.Forms.Timer
    • System.Timers.Timer
    • System.Threading.Timer



    vb.net Code:
    1. Public Class Form1
    2.  
    3.    
    4.     Dim WinFormsTimer As Windows.Forms.Timer
    5.     Dim TimersTimer As Timers.Timer
    6.     Dim ThreadingTimer As Threading.Timer
    7.  
    8.     ' The purpose of this delegate will be exlained below
    9.     Delegate Sub ChangeLabelCallback(ByVal [text] As String)
    10.  
    11.     ' We're going to need it for output
    12.     Dim lbl As Label
    13.  
    14.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    15.  
    16.         ' Adding our label
    17.         lbl = New Label
    18.         With lbl
    19.             .Location = New Point(20, 20)
    20.             .AutoSize = False
    21.             .Size = New Size(Me.Width - 40, Me.Height - 40)
    22.             .Font = New Font(Me.Font.FontFamily, 14, FontStyle.Bold, GraphicsUnit.Pixel)
    23.         End With
    24.         Me.Controls.Add(lbl)
    25.  
    26.  
    27.         ' System.Windows.Forms.Timer
    28.         ' *********************************************************
    29.         ' A component timer.
    30.         ' The simplest one. Compiled as a Windows.Forms.component
    31.         ' and can be 'placed' on a form
    32.         ' It's syncronous and starts disabled
    33.         WinFormsTimer = New Windows.Forms.Timer()
    34.         ' To enable it we must set the interval:
    35.         WinFormsTimer.Interval = 1000 ' It will generate an event each second
    36.         ' And we should also provide a handler to its Tick event
    37.         AddHandler WinFormsTimer.Tick, AddressOf WinFormsTimerTickHandler
    38.         ' To start the timer we should invoke its Start method:
    39.         WinFormsTimer.Start()
    40.  
    41.  
    42.  
    43.         ' System.Timers.Timer
    44.         ' *********************************************************
    45.         ' The .NET Framework documentation refers to the System.Timers.Timer class
    46.         ' as a server-based timer that was designed and optimized for use in
    47.         ' multithreaded environments. Instances of this timer class can be safely
    48.         ' accessed from multiple threads. Unlike the System.Windows.Forms.Timer,
    49.         ' the System.Timers.Timer class will, by default, call your timer event handler
    50.         ' on a worker thread obtained from the common language runtime (CLR) thread pool.
    51.         ' This means that the code inside your Elapsed event handler must conform to a
    52.         ' golden rule of Win32 programming: an instance of a control should never be
    53.         ' accessed from any thread other than the thread that was used to instantiate it.
    54.         ' The System.Timers.Timer class provides an easy way to deal with this dilemma — it
    55.         ' exposes a public SynchronizingObject property.
    56.         ' Setting this property to an instance of a Windows Form (or a control on a Windows Form)
    57.         ' will ensure that the code in your Elapsed event handler runs on the same thread on
    58.         ' which the SynchronizingObject was instantiated.
    59.         TimersTimer = New Timers.Timer
    60.         TimersTimer.Interval = 3000 ' We want it to 'fire' each 3 second
    61.         ' We need some event handler to perform periodical tasks (it has the Elapsed event)
    62.         AddHandler TimersTimer.Elapsed, AddressOf TimersTimerElapsedHandler
    63.         ' This class works in a thread-friendly manner
    64.         ' We don't have to check whether we're in the working thread or not
    65.         ' To do so we must tell the timer what object it has to be syncronous to.
    66.         ' We do it this way:
    67.         TimersTimer.SynchronizingObject = Me
    68.         ' And of cours we have to start this timer
    69.         ' (there is also Stop method)
    70.         TimersTimer.Start()
    71.  
    72.         ' System.Threading.Timer
    73.         ' *********************************************************
    74.         ' Threading timer. The most precise timer available
    75.         ' Since it is asyncronous it doesn't depend on the current
    76.         ' execution thread.
    77.         ' To syncronize with the main workflow you can use
    78.         ' a syncronization object which can report the
    79.         ' state of an executing thread to the callback procedure
    80.         ' Callback procedure is a procedure with a signature
    81.         ' identical to Threading.TimerCallBack delegate.
    82.         ' You provide its address to the timer and it will be
    83.         ' invoking it each time a timer event is generated.
    84.         Dim SyncObject As New Object ' Specifies a syncronization object
    85.         Dim Delay As Integer = 5000 ' Specifies a delay before the first callback is invoked
    86.         Dim Interval As Integer = 5000 ' Specifies the time interval in milliseconds between each callback call
    87.         ThreadingTimer = New Threading.Timer(AddressOf ThreadingTimerCallback, SyncObject, Delay, Interval)
    88.  
    89.  
    90.     End Sub
    91.  
    92.  
    93.     ' Handling of Timer events
    94.     ' *****************************************************************
    95.     ' This is our Winforms Timer Tick handler it is called each second.
    96.     ' But it can 'miss' some ticks when our form class is busy with something
    97.  
    98.     Private Sub WinFormsTimerTickHandler(ByVal sender As Object, ByVal e As System.EventArgs)
    99.         ' We'll get this handler invoked each second
    100.         Static X As Integer
    101.  
    102.         ' We're going to change the title of our window each time this timer is fired
    103.         X += 1
    104.         lbl.Text = String.Format("Ticks {0}", X)
    105.  
    106.         Debug.WriteLine("Windows.Forms.Timer Tick")
    107.     End Sub
    108.  
    109.     ' This is a handler for Timers.Timer
    110.     ' It's generally more precise but still not as precise as Threading.Timer
    111.     ' It is invoked every 3 seconds
    112.     Private Sub TimersTimerElapsedHandler(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
    113.         ' Since we provided the syncronization object we don't have to worry about
    114.         ' cross-thread calls and thus we can access our form properties directly:
    115.         lbl.Text = " Winforms Timer stopped with TimersTimer"
    116.  
    117.         ' We'll control winforms timer here.
    118.         ' Every three secodns we're going to turn it off
    119.         ' and after another three seconds - turn it on again.
    120.         WinFormsTimer.Enabled = Not WinFormsTimer.Enabled
    121.         Debug.WriteLine("Timer Elapsed event")
    122.     End Sub
    123.  
    124.     ' This is the ThreadingTimer callback procedure
    125.  
    126.     ' It will be invoked each 5 seconds and receive a syncronization object
    127.     ' we passed in its initializer
    128.     Private Sub ThreadingTimerCallback(ByVal state As Object)
    129.         ' Note that the code here will be executed asyncronously in another thread
    130.         ' Try to uncomment the following line and see what happens in 5 seconds:
    131.         ' lbl.Text = "We don't care about cross-thread calls!"
    132.  
    133.         ' Spoiler: You'd get an exception.
    134.         ' Instead, let's call ChangeLabel method (see below)
    135.         ChangeLabel("We care about cross-thread calls!")
    136.  
    137.         Debug.WriteLine("Threading Timer event")
    138.     End Sub
    139.  
    140.     ' The whole purpose of this sub is to change the label text
    141.     Private Sub ChangeLabel(ByVal Text As String)
    142.         ' To work safe we need to act thread-aware and
    143.         ' check whether we're in the same thread:
    144.         If lbl.InvokeRequired Then
    145.             ' Apparently not, we're in the wrong thread
    146.             ' so we need to marshal this call back to the UI thread
    147.             ' Do you remember that delegate in the beginning?
    148.             ' We need it to invoke this very method once again,
    149.             ' but in the right thread:
    150.             Dim d As New ChangeLabelCallback(AddressOf ChangeLabel)
    151.             Me.Invoke(d, New Object() {[Text]})
    152.         Else
    153.             ' Yes! We're in the right thread and can finally change the label text:
    154.             lbl.Text = Text
    155.         End If
    156.     End Sub
    157.  
    158.  
    159. End Class

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Re: Raise Event

    hello cicatrix,

    Thanks for the examples.very nice sir.i study it right now and i got error with this line.

    Code:
    ThreadingTimer = New Threading.Timer(AddressOf ThreadingTimerCallback, SyncObject, Delay, Interval)
    Error:
    Code:
     Overload resolution failed because no accessible 'New' can be called without a narrowing conversion:
        'Public Sub New(callback As System.Threading.TimerCallback, state As Object, dueTime As Long, period As Long)': Argument matching parameter 'period' narrows from 'System.Object' to 'Long'.
        'Public Sub New(callback As System.Threading.TimerCallback, state As Object, dueTime As Integer, period As Integer)': Argument matching parameter 'period' narrows from 'System.Object' to 'Integer'.
    how can fix this sir.

  11. #11
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Raise Event

    Hm, this is most strange because it's been compiled and successfully executed by me before posting.

    But hey, which version of VS do you use?
    It was done for VS2008 with Option Infer On
    Try to change this line:
    Code:
    Dim Interval = 5000
    to this:
    Code:
    Dim Interval As Integer = 5000
    If this happens to be the case this is probably my fault not to anticipate that someone might have got earlier versions of VS.

    Corrections made in the original post.

    P.S. If it doesn't then ... well I dunno.
    Try to initialize it with this line:

    Code:
    ThreadingTimer = New Threading.Timer(AddressOf ThreadingTimerCallback, SyncObject, 5000, 5000)

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Re: Raise Event

    Hi cicatrix,

    sorry for not indicating the version of my Visual Studio..
    btw,im using VS2003.i already test your updated code..and it works perfectly.

    thank you very much for your effort.

    i already rated this post as very helpful to me.

  13. #13
    Member
    Join Date
    Feb 2009
    Posts
    36

    Re: Raise Event

    Quote Originally Posted by cicatrix View Post
    You don't raise timer events yourself, it's timer's job.

    There are three basic types of timers in .NET.

    Here is a little demonstration of all. Read the comments.
    • System.Windows.Forms.Timer
    • System.Timers.Timer
    • System.Threading.Timer



    vb.net Code:
    1. Public Class Form1
    2.  
    3.    
    4.     Dim WinFormsTimer As Windows.Forms.Timer
    5.     Dim TimersTimer As Timers.Timer
    6.     Dim ThreadingTimer As Threading.Timer
    7.  
    8.     ' The purpose of this delegate will be exlained below
    9.     Delegate Sub ChangeLabelCallback(ByVal [text] As String)
    10.  
    11.     ' We're going to need it for output
    12.     Dim lbl As Label
    13.  
    14.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    15.  
    16.         ' Adding our label
    17.         lbl = New Label
    18.         With lbl
    19.             .Location = New Point(20, 20)
    20.             .AutoSize = False
    21.             .Size = New Size(Me.Width - 40, Me.Height - 40)
    22.             .Font = New Font(Me.Font.FontFamily, 14, FontStyle.Bold, GraphicsUnit.Pixel)
    23.         End With
    24.         Me.Controls.Add(lbl)
    25.  
    26.  
    27.         ' System.Windows.Forms.Timer
    28.         ' *********************************************************
    29.         ' A component timer.
    30.         ' The simplest one. Compiled as a Windows.Forms.component
    31.         ' and can be 'placed' on a form
    32.         ' It's syncronous and starts disabled
    33.         WinFormsTimer = New Windows.Forms.Timer()
    34.         ' To enable it we must set the interval:
    35.         WinFormsTimer.Interval = 1000 ' It will generate an event each second
    36.         ' And we should also provide a handler to its Tick event
    37.         AddHandler WinFormsTimer.Tick, AddressOf WinFormsTimerTickHandler
    38.         ' To start the timer we should invoke its Start method:
    39.         WinFormsTimer.Start()
    40.  
    41.  
    42.  
    43.         ' System.Timers.Timer
    44.         ' *********************************************************
    45.         ' The .NET Framework documentation refers to the System.Timers.Timer class
    46.         ' as a server-based timer that was designed and optimized for use in
    47.         ' multithreaded environments. Instances of this timer class can be safely
    48.         ' accessed from multiple threads. Unlike the System.Windows.Forms.Timer,
    49.         ' the System.Timers.Timer class will, by default, call your timer event handler
    50.         ' on a worker thread obtained from the common language runtime (CLR) thread pool.
    51.         ' This means that the code inside your Elapsed event handler must conform to a
    52.         ' golden rule of Win32 programming: an instance of a control should never be
    53.         ' accessed from any thread other than the thread that was used to instantiate it.
    54.         ' The System.Timers.Timer class provides an easy way to deal with this dilemma it
    55.         ' exposes a public SynchronizingObject property.
    56.         ' Setting this property to an instance of a Windows Form (or a control on a Windows Form)
    57.         ' will ensure that the code in your Elapsed event handler runs on the same thread on
    58.         ' which the SynchronizingObject was instantiated.
    59.         TimersTimer = New Timers.Timer
    60.         TimersTimer.Interval = 3000 ' We want it to 'fire' each 3 second
    61.         ' We need some event handler to perform periodical tasks (it has the Elapsed event)
    62.         AddHandler TimersTimer.Elapsed, AddressOf TimersTimerElapsedHandler
    63.         ' This class works in a thread-friendly manner
    64.         ' We don't have to check whether we're in the working thread or not
    65.         ' To do so we must tell the timer what object it has to be syncronous to.
    66.         ' We do it this way:
    67.         TimersTimer.SynchronizingObject = Me
    68.         ' And of cours we have to start this timer
    69.         ' (there is also Stop method)
    70.         TimersTimer.Start()
    71.  
    72.         ' System.Threading.Timer
    73.         ' *********************************************************
    74.         ' Threading timer. The most precise timer available
    75.         ' Since it is asyncronous it doesn't depend on the current
    76.         ' execution thread.
    77.         ' To syncronize with the main workflow you can use
    78.         ' a syncronization object which can report the
    79.         ' state of an executing thread to the callback procedure
    80.         ' Callback procedure is a procedure with a signature
    81.         ' identical to Threading.TimerCallBack delegate.
    82.         ' You provide its address to the timer and it will be
    83.         ' invoking it each time a timer event is generated.
    84.         Dim SyncObject As New Object ' Specifies a syncronization object
    85.         Dim Delay As Integer = 5000 ' Specifies a delay before the first callback is invoked
    86.         Dim Interval As Integer = 5000 ' Specifies the time interval in milliseconds between each callback call
    87.         ThreadingTimer = New Threading.Timer(AddressOf ThreadingTimerCallback, SyncObject, Delay, Interval)
    88.  
    89.  
    90.     End Sub
    91.  
    92.  
    93.     ' Handling of Timer events
    94.     ' *****************************************************************
    95.     ' This is our Winforms Timer Tick handler it is called each second.
    96.     ' But it can 'miss' some ticks when our form class is busy with something
    97.  
    98.     Private Sub WinFormsTimerTickHandler(ByVal sender As Object, ByVal e As System.EventArgs)
    99.         ' We'll get this handler invoked each second
    100.         Static X As Integer
    101.  
    102.         ' We're going to change the title of our window each time this timer is fired
    103.         X += 1
    104.         lbl.Text = String.Format("Ticks {0}", X)
    105.  
    106.         Debug.WriteLine("Windows.Forms.Timer Tick")
    107.     End Sub
    108.  
    109.     ' This is a handler for Timers.Timer
    110.     ' It's generally more precise but still not as precise as Threading.Timer
    111.     ' It is invoked every 3 seconds
    112.     Private Sub TimersTimerElapsedHandler(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
    113.         ' Since we provided the syncronization object we don't have to worry about
    114.         ' cross-thread calls and thus we can access our form properties directly:
    115.         lbl.Text = " Winforms Timer stopped with TimersTimer"
    116.  
    117.         ' We'll control winforms timer here.
    118.         ' Every three secodns we're going to turn it off
    119.         ' and after another three seconds - turn it on again.
    120.         WinFormsTimer.Enabled = Not WinFormsTimer.Enabled
    121.         Debug.WriteLine("Timer Elapsed event")
    122.     End Sub
    123.  
    124.     ' This is the ThreadingTimer callback procedure
    125.  
    126.     ' It will be invoked each 5 seconds and receive a syncronization object
    127.     ' we passed in its initializer
    128.     Private Sub ThreadingTimerCallback(ByVal state As Object)
    129.         ' Note that the code here will be executed asyncronously in another thread
    130.         ' Try to uncomment the following line and see what happens in 5 seconds:
    131.         ' lbl.Text = "We don't care about cross-thread calls!"
    132.  
    133.         ' Spoiler: You'd get an exception.
    134.         ' Instead, let's call ChangeLabel method (see below)
    135.         ChangeLabel("We care about cross-thread calls!")
    136.  
    137.         Debug.WriteLine("Threading Timer event")
    138.     End Sub
    139.  
    140.     ' The whole purpose of this sub is to change the label text
    141.     Private Sub ChangeLabel(ByVal Text As String)
    142.         ' To work safe we need to act thread-aware and
    143.         ' check whether we're in the same thread:
    144.         If lbl.InvokeRequired Then
    145.             ' Apparently not, we're in the wrong thread
    146.             ' so we need to marshal this call back to the UI thread
    147.             ' Do you remember that delegate in the beginning?
    148.             ' We need it to invoke this very method once again,
    149.             ' but in the right thread:
    150.             Dim d As New ChangeLabelCallback(AddressOf ChangeLabel)
    151.             Me.Invoke(d, New Object() {[Text]})
    152.         Else
    153.             ' Yes! We're in the right thread and can finally change the label text:
    154.             lbl.Text = Text
    155.         End If
    156.     End Sub
    157.  
    158.  
    159. End Class

    dude, how do i stop the threaded timer??? thanks

  14. #14
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Raise Event

    Quote Originally Posted by pidyok View Post
    dude, how do i stop the threaded timer??? thanks
    You don't, check the documentation:
    http://msdn.microsoft.com/en-us/libr...ing.timer.aspx

    When a timer is no longer needed, use the Dispose method to free the resources held by the timer. Note that callbacks can occur after the Dispose method overload has been called, because the timer queues callbacks for execution by thread pool threads. You can use the Dispose(WaitHandle) method overload to wait until all callbacks have completed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width