Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Passing arguments to timer event

Passing arguments to timer event

Scheduled Pinned Locked Moved Visual Basic
question
6 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sohaib_a
    wrote on last edited by
    #1

    Hello all, I am creating a timer in a background worker thread as shown below and want to pass arguements to the event handler 'timerevent'. Private Sub DoJob(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Dim timer1 As New System.Timers.Timer AddHandler timer1.Elapsed, AddressOf timerevent timer1.start() End Sub but in the timer event handler i can only get something called 'signal time' from the elapsedeventargs. Is there a way to pass arguments for (in my case a string) to the timer event handler. Also is the elapsed event similar to the tick event?

    W 1 Reply Last reply
    0
    • S sohaib_a

      Hello all, I am creating a timer in a background worker thread as shown below and want to pass arguements to the event handler 'timerevent'. Private Sub DoJob(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Dim timer1 As New System.Timers.Timer AddHandler timer1.Elapsed, AddressOf timerevent timer1.start() End Sub but in the timer event handler i can only get something called 'signal time' from the elapsedeventargs. Is there a way to pass arguments for (in my case a string) to the timer event handler. Also is the elapsed event similar to the tick event?

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Sohaib_A wrote:

      Is there a way to pass arguments for (in my case a string) to the timer event handler

      You can't change the signature of the Elapsed event. Also where would that string come from? Timer.Elapsed is raised by timer when the interval has elapsed. So you cannot change the code how Elapsed is raised by the timer. You can store strings for example in class variables and use them, but without knowing anything more of your scenario, it's hard to say if that would be a feasible solution.

      Sohaib_A wrote:

      Also is the elapsed event similar to the tick event?

      Yes, Elapsed is for System.Timers.Timer class and Tick is for System.Windows.Forms.Timer class.

      The need to optimize rises from a bad design.My articles[^]

      S 1 Reply Last reply
      0
      • W Wendelius

        Sohaib_A wrote:

        Is there a way to pass arguments for (in my case a string) to the timer event handler

        You can't change the signature of the Elapsed event. Also where would that string come from? Timer.Elapsed is raised by timer when the interval has elapsed. So you cannot change the code how Elapsed is raised by the timer. You can store strings for example in class variables and use them, but without knowing anything more of your scenario, it's hard to say if that would be a feasible solution.

        Sohaib_A wrote:

        Also is the elapsed event similar to the tick event?

        Yes, Elapsed is for System.Timers.Timer class and Tick is for System.Windows.Forms.Timer class.

        The need to optimize rises from a bad design.My articles[^]

        S Offline
        S Offline
        sohaib_a
        wrote on last edited by
        #3

        Ok. Well the strings are stored in a database.Each background worker will take a different string depending on input from user. This is how i am doing it.

        If reading <= m_WorkerList.Count - 1 Then

              str = ds1.Tables(0).Rows(0)("ReaderLocation").ToString
        
            status = api.TcpConnectReader(ds1.Tables(0).Rows(0)("ReaderIP").ToString, \_
                     ds1.Tables(0).Rows(0)("ReaderPort"))
        
                   If status = 0 Then
        
                api.TcpCloseConnect()
                st = str
        
                m\_WorkerList(reading).RunWorkerAsync(str)
        

        reading = reading +1
        End If

        m_Workerlist is a list of BGW that i have initailized earlier.

        W 1 Reply Last reply
        0
        • S sohaib_a

          Ok. Well the strings are stored in a database.Each background worker will take a different string depending on input from user. This is how i am doing it.

          If reading <= m_WorkerList.Count - 1 Then

                str = ds1.Tables(0).Rows(0)("ReaderLocation").ToString
          
              status = api.TcpConnectReader(ds1.Tables(0).Rows(0)("ReaderIP").ToString, \_
                       ds1.Tables(0).Rows(0)("ReaderPort"))
          
                     If status = 0 Then
          
                  api.TcpCloseConnect()
                  st = str
          
                  m\_WorkerList(reading).RunWorkerAsync(str)
          

          reading = reading +1
          End If

          m_Workerlist is a list of BGW that i have initailized earlier.

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          Okay, so you pass a string to the worker when you run it. And the worker uses that string when doing it's stuff. How the timer is related to this? Do you have a timer inside the worker, which then causes some actions? If that's the case, could you simply store the string inside individual workers as a class variable and use that class variable whenever Elapsed event is raised?

          The need to optimize rises from a bad design.My articles[^]

          S 1 Reply Last reply
          0
          • W Wendelius

            Okay, so you pass a string to the worker when you run it. And the worker uses that string when doing it's stuff. How the timer is related to this? Do you have a timer inside the worker, which then causes some actions? If that's the case, could you simply store the string inside individual workers as a class variable and use that class variable whenever Elapsed event is raised?

            The need to optimize rises from a bad design.My articles[^]

            S Offline
            S Offline
            sohaib_a
            wrote on last edited by
            #5

            Yes there is a timer inside the worker which is started in the worker's dojob event as shown in the code i posted earlier. Ok..so is a class variable something like a global variable?And after i pass the string to worker's DOJOB event,i should declare at is a class variable in the DOJOB? Do you have any examples for this? All the workers will be running in parallel,so will there be multiple instances of this string each with it own value relative to the back ground worker it was passed from?

            W 1 Reply Last reply
            0
            • S sohaib_a

              Yes there is a timer inside the worker which is started in the worker's dojob event as shown in the code i posted earlier. Ok..so is a class variable something like a global variable?And after i pass the string to worker's DOJOB event,i should declare at is a class variable in the DOJOB? Do you have any examples for this? All the workers will be running in parallel,so will there be multiple instances of this string each with it own value relative to the back ground worker it was passed from?

              W Offline
              W Offline
              Wendelius
              wrote on last edited by
              #6

              You can inherit the BGW and add the variable into that class. I don't have VB at the moment but the basic idea is simple. I believe that this C# snippet will give you the idea. Instead of : in class definition, you would use inherits also the definition of the variable, constructor and property are a bit different, but not much:

              public class MyWorker : System.ComponentModel.BackgroundWorker {
              private string _customData;
              public MyWorker() : base() { }
              public string CustomData {
              get {
              return this._customData;
              }
              set {
              this._customData = value;
              }
              }
              }

              When using it, you would:

              ...
              MyWorker theWorker = new MyWorker();
              theWorker.CustomData = "ABCD";
              theWorker.RunWorkerAsync();
              ...

              This is one way that you define and set the class variable inside each BGW. Now in the DoWork yuo can get the value using CustomData property getter.

              The need to optimize rises from a bad design.My articles[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups