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?
8 2207
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?
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?
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 - void timer0( float secs)
-
{
-
float a = 0;
-
DateTime CurrTime = DateTime.Now;
-
CurrTime = CurrTime.AddSeconds(secs);
-
while (!(DateTime.Now.Second == CurrTime.Second))
-
a = +1;
-
return;
-
}
well, it makes sense to me.
Thank you for your help
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.
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?
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 - using System;
-
using System.Text;
-
using System.Windows.Forms;
-
using System.Threading;
-
-
namespace threadtest6
-
{
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
Thread thread1;
-
-
private void Form1_Load(object sender, EventArgs e)
-
{
-
a();
-
}
-
-
public void a()
-
{
-
thread1.Start();
-
textBox1.Text = "1";
-
thread1.Abort();
-
}
-
}
-
}
-
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
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... - 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 ;)
Thank-you
A weekend of reading approaches. But I have a feeling i will have more questions1
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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:
...
|
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
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |