473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to create reusable timer that delays program by varying amounts of time

82
I'm trying to make a timer which is a reusable module. Is this possible? I would like to pass 1 argument to the timer. The argument is the delay that the main program waits before it continues.

The main program would be something like

start
do something (process 1)
pass argument ( delay time eg 2 sec) to timer
after timer has timed out program continues
do something (process 2)
pass argument ( delay time eg 1 sec) to timer
after timer has timed out program continues
do something (process 3)
pass argument ( delay time eg 4 sec) to timer
after timer has timed out program continues
do something (process 4)
pass argument ( delay time eg 0.5 sec) to timer
after timer has timed out program continues
end

As you can see 1 timer is used to provide repeatedly delays to the program by varying amounts of time. I cant stop the thread because other inputs are received when the timer has delayed the program and still have to be acted upon.

Ive looked around but I cant find anything close to what Im trying to do.

Any ideas or pointers?
Jun 16 '10 #1
8 2207
Plater
7,872 Expert 4TB
If the timer is delaying the program, why can't you use thread.sleep?
You could make it smaller increments of thread.sleep in a loop and include Application.DoEvents() or something if you need signal handling?
Jun 16 '10 #2
GaryTexmo
1,501 Expert 1GB
So you want the timer class itself to do the work, then after an interval send a message to the main class and tell it to do something?

I'm really not following your example, but there is a built in .NET Timer object that will tick after a preset time, triggering an event that you can handle. When this event is triggered you can just as easily stop the timer, then change the tick period and/or do something else.

Sorry if I'm not reading your example properly... who is doing the work? The timer or the main program?
Jun 16 '10 #3
mrcw
82
Application.DoEvents() .. I'n not that good a programmer yet!

who is doing the work? Imagine that the processes are motors turning. They dont reach their target position immediately, but the program does. The program needs to be delayed while the motors catch up. If somebody hits the stop switch the motors still need to react to that. So I cant put thread to sleep.

I eventually used

Expand|Select|Wrap|Line Numbers
  1. void timer0( float secs)
  2.         {
  3.             float a = 0;
  4.             DateTime CurrTime = DateTime.Now;
  5.             CurrTime = CurrTime.AddSeconds(secs);
  6.             while (!(DateTime.Now.Second == CurrTime.Second))
  7.                 a = +1;
  8.             return;
  9.         }
well, it makes sense to me.
Thank you for your help
Jun 17 '10 #4
Plater
7,872 Expert 4TB
You have effectively made a busy loop there.
Not only will that stop up the thread, but it will also spike your resource usage.
Adding in a Thread.Sleep(1) inside your loop will greatly aid that problem.
Jun 17 '10 #5
GaryTexmo
1,501 Expert 1GB
Can't you just use events to have the worker threads signal the main thread that they are done and it should proceed? I don't even know that you need a loop, unless you're continuously processing something and are just waiting for, say, a buffer to fill up again.

Maybe have the main thread set up listeners to your worker threads which fire an event called WorkDone when they are finished. The main thread receives this event and processes whatever work was completed, then goes back to waiting.

If your main thread is a gui (ie, form), it can just sit there in the open state and receive events. If it's a console program then yea, you'll need a thread.sleep loop.

Would that help?
Jun 17 '10 #6
mrcw
82
ok I thought I'd better look at threads...from the beginning. I've looked at sites and books but they all show complex examples.

I have written a simple program a form with a textbox on it.
The code for the form is here

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Text;
  3. using System.Windows.Forms;
  4. using System.Threading;
  5.  
  6. namespace threadtest6
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.         public Form1()
  11.         {
  12.             InitializeComponent();
  13.         }
  14.  
  15.         Thread thread1; 
  16.  
  17.         private void Form1_Load(object sender, EventArgs e)
  18.         {
  19.             a(); 
  20.         }
  21.  
  22.         public void a()
  23.         {
  24.             thread1.Start();
  25.             textBox1.Text = "1";
  26.             thread1.Abort();
  27.         }
  28.     }
  29. }
  30.  
My first questions are 1) I get an error 'threadtest6.Form1.thread1' is never assigned to, and will always have its default value null"

How do I assign a value to a thread and remove the error?

2) How do I put the text from a() into textbox1 on the form?

can you recommend any good websites or books about c# threads (remember very simple).
thank-you
Jun 18 '10 #7
GaryTexmo
1,501 Expert 1GB
Here you are, sir!
http://www.albahari.com/threading/

The error message is pretty clear actually. References in C# initialize to null and you haven't initialized thread1. You can either do it before you call it, in the constructor, or at it's declaration.

The simplest way might be...

Expand|Select|Wrap|Line Numbers
  1. Thread thread1 = new Thread(<method to run>);
As for the text... you've modified a property on a control that belongs to the form itself. It should already be on the form. Though really, I'm actually not sure what you're doing there... you start a thread, change text, then immediately stop the thread.

Have a read through that e-book, hopefully it'll help you out ;)
Jun 18 '10 #8
mrcw
82
Thank-you

A weekend of reading approaches. But I have a feeling i will have more questions1
Jun 18 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Tony Marston | last post by:
For those of you who think that using XSL/XML to create your HTML output from a PHP application is not a viable proposition as XSL is a clunky language, it is too verbose and it cannot get the job...
1
by: Rhett | last post by:
Hello,EveryBody! I'm troubled by a design problem, Hoping that you'll save me out! The Situation is: We have two project of code A and B.A is our reusable code base, B is for specified for...
2
by: John | last post by:
Hi, I have a data driven application which has some generalized components. So, for reuse, I am building the components so they can be reused in other projects ... it takes almost no extra...
3
by: Axter | last post by:
I'm wondering about the practical use of dynamic_cast in reusable or generic code. I'm currently working on a smart pointer that can be used on vector and other STL containers. See following...
27
by: Matt Kruse | last post by:
Since this topic has come up several times in other threads, I thought I'd make a separate thread and gather opinions from (hopefully) a more varied range of newsgroup participants. What are...
2
by: DaleMan | last post by:
Where can I learn more about reusable businesss objects and their uses? I've been reading that one of .Net's advantages is that a developer can write reusable business objects. I would like to...
2
by: John David Thornton | last post by:
I've got a Windows Service class, and I put a System.Threading.Timer, and I've coded it as shown below. However, when I install the service and then start it in MMC, I get a peculiar message: ...
3
by: Armen Rizal | last post by:
Hello all, Is there anybody know where I can find reusable pl/pgsql samples or function library ? Thanks, Armen
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.