Problem with timers

  • Thread starter Thread starter Ole
  • Start date Start date
O

Ole

Hi,

Is it possible to change a threading timers delay and interval from another
threading timers callback routine?

Thanks
Ole
 
Hi,

Is it possible to change a threading timers delay and interval from another
threading timers callback routine?

Thanks
Ole

Yes, here's an example.

using System;
using System.Threading;

namespace threadfun
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
ThreadTest o = new ThreadTest();
o.Go();
Console.WriteLine("Finished");
Console.Read();
}
}
public class ThreadTest
{
private Timer _t1;
private Timer _t2;
private ManualResetEvent _m1;
private ManualResetEvent _m2;
private int _c=10;
private int _tick = 500;

public void Go()
{
Thread myThread = new Thread(new ThreadStart(NewThread));
myThread.Start();
_m1 = new ManualResetEvent(false);
Thread.Sleep(10);
_t1 = new Timer(new TimerCallback(ThreadTestTick),null,0,300);
while(true)
{
if(_m1.WaitOne(10,false))
{
break;
}
}
}
private void ThreadTestTick(object o)
{
if(_c>0)
{
_c--;
_tick -=45;
_t2.Change(0,_tick);
}
else
{
_m1.Set();
}
}
private void NewThread()
{
using(_t2 = new Timer(new TimerCallback(NewThreadTick),null,
0,_tick))
{
while(true)
{
if(_m1.WaitOne(10,false))
{
break;
}
}
}
}
private void NewThreadTick(object o)
{
Console.WriteLine("Tick");
}
}
}
 
Thanks - great!


DeveloperX said:
Hi,

Is it possible to change a threading timers delay and interval from
another
threading timers callback routine?

Thanks
Ole

Yes, here's an example.

using System;
using System.Threading;

namespace threadfun
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
ThreadTest o = new ThreadTest();
o.Go();
Console.WriteLine("Finished");
Console.Read();
}
}
public class ThreadTest
{
private Timer _t1;
private Timer _t2;
private ManualResetEvent _m1;
private ManualResetEvent _m2;
private int _c=10;
private int _tick = 500;

public void Go()
{
Thread myThread = new Thread(new ThreadStart(NewThread));
myThread.Start();
_m1 = new ManualResetEvent(false);
Thread.Sleep(10);
_t1 = new Timer(new TimerCallback(ThreadTestTick),null,0,300);
while(true)
{
if(_m1.WaitOne(10,false))
{
break;
}
}
}
private void ThreadTestTick(object o)
{
if(_c>0)
{
_c--;
_tick -=45;
_t2.Change(0,_tick);
}
else
{
_m1.Set();
}
}
private void NewThread()
{
using(_t2 = new Timer(new TimerCallback(NewThreadTick),null,
0,_tick))
{
while(true)
{
if(_m1.WaitOne(10,false))
{
break;
}
}
}
}
private void NewThreadTick(object o)
{
Console.WriteLine("Tick");
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top