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. C#
  4. use of system.threading.timer

use of system.threading.timer

Scheduled Pinned Locked Moved C#
javascripthelptutorialquestion
1 Posts 1 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.
  • D Offline
    D Offline
    dhol
    wrote on last edited by
    #1

    Here is a small example which i did, Create a form with button, label, textbox, and progressbar on it (I just use the default names in this example). Create two methods on the form, one that updates the label, and one that updates the progress bar. Code: private void UpdateLabel() { label1.BackColor=Color.Red; } private void UpdateProgress() { progressBar1.PerformStep(); } Create a class that does the thread work, in this case it will start a timer, and invoke methods above for the form each time the timer elapses. Code: private class ThreadRunner { private Form1 _form; private int _delay; private System.Threading.Timer _timer; public ThreadRunner(Form1 mainForm,int delayInSeconds) { _form=mainForm; _delay=delayInSeconds; } public void Go() { _timer=new System.Threading.Timer(new TimerCallback(this.TimerElapsed),null,0,10); while (_timer!=null) Thread.Sleep(0); // Do nothing until timer has stopped } public void TimerElapsed(object o) { _delay--; _form.Invoke(new MethodInvoker(_form.UpdateProgress)); if (_delay<=0) { _form.Invoke(new MethodInvoker(_form.UpdateLabel)); _timer.Dispose(); _timer=null; } } } Add code to start an object of the class defined above: Code: private void button1_Click(object sender, System.EventArgs e) { int timeToRun=Int32.Parse(textBox1.Text); progressBar1.Maximum=timeToRun; progressBar1.Step=1; ThreadRunner tr=new ThreadRunner(this,timeToRun); Thread t=new Thread(new ThreadStart(tr.Go)); t.Start(); } Inthis u can see that for a specify seconds in the textbox, ,label glows red after the progress bar completes. so now wht i need is that to do another thing in the above example,that is ..that giving a time in the text box....say 3:00 pm.... then the label should turn red at 3:00pm.. i mean giving the system timing ,... then we should give a time in the text box, then the button click property should be done at the specified time in the text box. how can i do this in the above method....please can u help me to do this. js -- modified at 3:39 Tuesday 13th September, 2005

    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