System.Threading.Timer , am I doing this correctly ? 
Author Message
 System.Threading.Timer , am I doing this correctly ?

I have the following class in which a method will be called via the Timer.

 public class FetchResource
 {
  public System.Threading.Timer refTimer;
  private int i=0;
  private EventLog myLog;

  public FetchResource(EventLog arg)
  {
   myLog = arg;
  }

  public void QueryResourcesToMonitor()
  {
   Thread.Sleep(5000);
   i +=1;
   if(i==20)
   {
    refTimer.Dispose();
    refTimer = null;
   }
  }
 }

And, inside my form buttonclick ( This will not be the place ultimately this
will work, this is just for a test)

   FetchResource ResourceGatherer = new FetchResource(myLog);

   //Create a timercall back
   TimerCallback callBack = new
TimerCallback(ResourceGatherer.QueryResourcesToMonitor);

   //Now instantiate the timer object
   System.Threading.Timer ResourceGathererTimer = new
System.Threading.Timer(callBack,null,1000,1000);

   ResourceGatherer.refTimer= ResourceGathererTimer;

   while( ResourceGatherer.refTimer !=null)
   {
    Thread.Sleep(0);
   }

Now, when I compile, It says

Method 'ServiceTest.FetchResource.QueryResourcesToMonitor()' does not match
delegate 'void System.Threading.TimerCallback(object)'

1) What what is wrong here ?

2) Finaly,all this will go inside an NT Service. What I want to do is,
        (a) Keep on querying a database and fill a queue.
        (b) Deque those data and pass them to a ThreadPool for further
processing

What I have thought of doing is, to query the database and queue data, in a
seperate thread using the timer, like above.
Create another thread which will deque , package and push to a Threadpool to
do further processing (these tasks may take some time)

But, this way, I have 2 threads which I manage by myself and a threadpool

Do any of you guys have any better suggesstions ?

3) Now even If I only READ the queue by one thread, should I lock the queue
as lock( queue.SyncRoot ) ? becuse
one thread, will be only writing to the queue, while other is only reading.

Regards
KK



Thu, 14 Oct 2004 07:15:11 GMT  
 System.Threading.Timer , am I doing this correctly ?
change
Quote:
>   public void QueryResourcesToMonitor()

to
    public void QueryResourcesToMonitor( object state )
Quote:
> System.Threading.Timer(callBack,null,1000,1000);

    The "null" will be passed to the "state" parameter.

    I find the combination of

Quote:
> System.Threading.Timer(callBack,null,1000,1000);

    with this in the callback
Quote:
>    Thread.Sleep(5000);

    to be very interesting in how it will perform.  "i" will probably reach
25 before the refTimer is Disposed(), due to the 5 second delay.
Since the 3rd value determines how long to wait before calling callBack the
first time, and the 4th value determines how long to wait for each
subsequent call, you should be able to find a more affective way of writing
this
    System.Threading.Timer(callBack,null,6000,1000);
... would do about the same, except for creating 20 threads, instead of the
built in delay creating 25 threads.  (How many threads exist at any time
depend on how long it takes your threads to run)  If each thread has a short
life, especially if under one second, it may be easier to create one thread
to run 20 times.  Of course, the threads, as written would be very very
short, but I assume your final callback method, whatever it may be, will
take somewhat longer, so you'll need to decide how to handle that.

    I did create a program that ran 20 processing threads at a time, but
each thread took many seconds to a few minutes to run, and when any given
thread had finished, it went to retrieve the next file to process.  Between
2000 and 3000 files were dumped in every morning, from all over the US, so
it worked out pretty well.

    The following effectively blocks your main thread while you are creating
the TimerCallback threads.

Quote:
>    while( ResourceGatherer.refTimer !=null)
>    {
>     Thread.Sleep(0);
>    }

    I assume you're just doing this for debugging purposes?

    While the following may be debatable, you'll find most programmers
prefer, instead of,

Quote:
>    i +=1;

To use
    i++;

This doesn't answer many of your questions, but points out some things to
think about.

Chris R.

*************************************************************

Quote:

> I have the following class in which a method will be called via the Timer.

>  public class FetchResource
>  {
>   public System.Threading.Timer refTimer;
>   private int i=0;
>   private EventLog myLog;

>   public FetchResource(EventLog arg)
>   {
>    myLog = arg;
>   }

>   public void QueryResourcesToMonitor()
>   {
>    Thread.Sleep(5000);
>    i +=1;
>    if(i==20)
>    {
>     refTimer.Dispose();
>     refTimer = null;
>    }
>   }
>  }

> And, inside my form buttonclick ( This will not be the place ultimately
this
> will work, this is just for a test)

>    FetchResource ResourceGatherer = new FetchResource(myLog);

>    //Create a timercall back
>    TimerCallback callBack = new
> TimerCallback(ResourceGatherer.QueryResourcesToMonitor);

>    //Now instantiate the timer object
>    System.Threading.Timer ResourceGathererTimer = new
> System.Threading.Timer(callBack,null,1000,1000);

>    ResourceGatherer.refTimer= ResourceGathererTimer;

>    while( ResourceGatherer.refTimer !=null)
>    {
>     Thread.Sleep(0);
>    }

> Now, when I compile, It says

> Method 'ServiceTest.FetchResource.QueryResourcesToMonitor()' does not
match
> delegate 'void System.Threading.TimerCallback(object)'

> 1) What what is wrong here ?

> 2) Finaly,all this will go inside an NT Service. What I want to do is,
>         (a) Keep on querying a database and fill a queue.
>         (b) Deque those data and pass them to a ThreadPool for further
> processing

> What I have thought of doing is, to query the database and queue data, in
a
> seperate thread using the timer, like above.
> Create another thread which will deque , package and push to a Threadpool
to
> do further processing (these tasks may take some time)

> But, this way, I have 2 threads which I manage by myself and a threadpool

> Do any of you guys have any better suggesstions ?

> 3) Now even If I only READ the queue by one thread, should I lock the
queue
> as lock( queue.SyncRoot ) ? becuse
> one thread, will be only writing to the queue, while other is only
reading.

> Regards
> KK



Fri, 15 Oct 2004 02:15:40 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. System.Timers.Timer & System.threading.Timer

2. System.Threading.Timer is not working properly.

3. System.Threading.Timer question (help!)

4. Use System.Threading.Timer

5. System.Timers.Timer can't throw Exception?

6. free() done correctly?

7. Am I using GetDIBits correctly?

8. GetWindowRect, am I using this correctly?

9. Am I using GetDIBits correctly?

10. Am I using GetDIBits correctly?

11. Wot am I doing rong??

12. Am I doing this right?

 

 
Powered by phpBB® Forum Software