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
  1. Home
  2. General Programming
  3. C#
  4. Attaching custom methods

Attaching custom methods

Scheduled Pinned Locked Moved C#
questioncsharpdesignhelp
4 Posts 3 Posters 0 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.
  • E Offline
    E Offline
    Enishi
    wrote on last edited by
    #1

    Hi, im new to C# and have a problem when i must assign methods to objects i create in execution mode. while in design mode u can create a timer and then double click on it and there u go, but how can i attach a method to a timer i have created in execution time? using System.Timers; Timer t = new timer(30000); t.enabled=true; //but how i tell the timer what method or event to raise when its interval elapses? Ty for ur time.

    M S 2 Replies Last reply
    0
    • E Enishi

      Hi, im new to C# and have a problem when i must assign methods to objects i create in execution mode. while in design mode u can create a timer and then double click on it and there u go, but how can i attach a method to a timer i have created in execution time? using System.Timers; Timer t = new timer(30000); t.enabled=true; //but how i tell the timer what method or event to raise when its interval elapses? Ty for ur time.

      M Offline
      M Offline
      MoustafaS
      wrote on last edited by
      #2

      Timer t = new Timer(new TimerCallBack(Your Method)); and then handle all the rest

      1 Reply Last reply
      0
      • E Enishi

        Hi, im new to C# and have a problem when i must assign methods to objects i create in execution mode. while in design mode u can create a timer and then double click on it and there u go, but how can i attach a method to a timer i have created in execution time? using System.Timers; Timer t = new timer(30000); t.enabled=true; //but how i tell the timer what method or event to raise when its interval elapses? Ty for ur time.

        S Offline
        S Offline
        Sebastian Schneider
        wrote on last edited by
        #3

        Declare your Timer [code] System.Timers.Timer myTimer = new System.Timers.Timer(1000); // 1000 is the Interval used by the Timer in ms, i.e. the time between two "Timer.Elapsed" Events [/code] Then add an EventHandler [code] myTimer.Elapsed +=new System.Timers.ElapsedEventHandler(myTimer_Elapsed); // Note: In VS.NET 2003, as soon as you type the +=, VS will ask you to press // TAB. If you do, it will code-complete everything else, even the Delegate's // body if you press TAB again. [/code] Now, on each Elapsed-Event, the Delegate will be executed [code] //If you are not using VS.NET, this is what the delegate should look like: private void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { } [/code]

        E 1 Reply Last reply
        0
        • S Sebastian Schneider

          Declare your Timer [code] System.Timers.Timer myTimer = new System.Timers.Timer(1000); // 1000 is the Interval used by the Timer in ms, i.e. the time between two "Timer.Elapsed" Events [/code] Then add an EventHandler [code] myTimer.Elapsed +=new System.Timers.ElapsedEventHandler(myTimer_Elapsed); // Note: In VS.NET 2003, as soon as you type the +=, VS will ask you to press // TAB. If you do, it will code-complete everything else, even the Delegate's // body if you press TAB again. [/code] Now, on each Elapsed-Event, the Delegate will be executed [code] //If you are not using VS.NET, this is what the delegate should look like: private void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { } [/code]

          E Offline
          E Offline
          Enishi
          wrote on last edited by
          #4

          TY a lot for your help and examples, it works perfect now.

          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