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. help in this program

help in this program

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

    Hi I have to do a program in c# windows application such that form employs with a textbox, Progress bar, label and abutton. I have used system.threading.timer such that , when a time is given in the textbox, (say 02:30 PM),then the progress bar progress and at last after progress bar finishes, the label which is in the form should turn red colour at that specified time(ie, 02:30 PM).. but, I had a doen a program to such extent that when a time is given in textbox(say 02:30 pm) then the progress bar progresses and finishes soon and the label turns into red colour. Burt not at the specified time(ie, 02:30 pm).. here is ,my part of coding. Please help me to do the the thing. 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() { // Create a timer passing in a delegate to the TimerElapsedmethod // null is to be passed as a parameter to Timerelapsed //0-start immediately //10--cal a callback everysecond _timer=new System.Threading.Timer(new TimerCallback (this.TimerElapsed),null,1000,1000); 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; } } } //then inside teh button click private void button1_Click(object sender, System.EventArgs e) { DateTime current = System.DateTime.Now; DateTime end = DateTime.Parse(textBox1.Text); TimeSpan ts = end.Subtract(current); //value entered in the textbox int timeToRun= System.Convert.ToInt32(ts.Seconds); //set the maximum range of value progressBar1.Maximum=timeToRun; progressBar1.Step=1; ThreadRunner tr=new ThreadRunner(this,timeToRun); Thread t=new Thread(new ThreadStart(tr.Go)); t.Start(); } this is the coding part which i had done.please explain and tell me wher to change and finish it . dhol

    K 1 Reply Last reply
    0
    • D dhol

      Hi I have to do a program in c# windows application such that form employs with a textbox, Progress bar, label and abutton. I have used system.threading.timer such that , when a time is given in the textbox, (say 02:30 PM),then the progress bar progress and at last after progress bar finishes, the label which is in the form should turn red colour at that specified time(ie, 02:30 PM).. but, I had a doen a program to such extent that when a time is given in textbox(say 02:30 pm) then the progress bar progresses and finishes soon and the label turns into red colour. Burt not at the specified time(ie, 02:30 pm).. here is ,my part of coding. Please help me to do the the thing. 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() { // Create a timer passing in a delegate to the TimerElapsedmethod // null is to be passed as a parameter to Timerelapsed //0-start immediately //10--cal a callback everysecond _timer=new System.Threading.Timer(new TimerCallback (this.TimerElapsed),null,1000,1000); 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; } } } //then inside teh button click private void button1_Click(object sender, System.EventArgs e) { DateTime current = System.DateTime.Now; DateTime end = DateTime.Parse(textBox1.Text); TimeSpan ts = end.Subtract(current); //value entered in the textbox int timeToRun= System.Convert.ToInt32(ts.Seconds); //set the maximum range of value progressBar1.Maximum=timeToRun; progressBar1.Step=1; ThreadRunner tr=new ThreadRunner(this,timeToRun); Thread t=new Thread(new ThreadStart(tr.Go)); t.Start(); } this is the coding part which i had done.please explain and tell me wher to change and finish it . dhol

      K Offline
      K Offline
      KaptinKrunch
      wrote on last edited by
      #2

      From what I can see, this line dhol wrote: //value entered in the textbox int timeToRun= System.Convert.ToInt32(ts.Seconds); is not providing the correct number of seconds.You will need to add the number of seconds for the hours, minutes and secounds. //This should work for your needs. int timeToRun = ((ts.Hours * 60) * 60) + (ts.Minutes * 60) + ts.Seconds;

      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