开发者

Threading Timer Not working?

开发者 https://www.devze.com 2023-03-25 11:11 出处:网络
public class DHSApi { public void StartFetchingTickerInfo() { client = new clientAPI(); Trace.Write(\"Timer Started\" );
public class DHSApi
{
    public void StartFetchingTickerInfo()
    {

        client = new clientAPI();

        Trace.Write("Timer Started" );
        Timer timer = new Timer(new TimerCallback(GetQuotes), null, 0, 10000);

    }

    public void GetQuotes(object obj)
    {
        Trace.Write("Timer Pass:" + timerPasser++);
        if (currentWork > workList.Count - 1)
            currentWork = 0;
        Thread t = new Thread(Quote);
        t.Start(workList);


    }
    private void Quote(object obj)
    {
        Trace.Write("Entered Quotes:" + quotesIn++);

        lock (workList)
        {
            List<Work> works = new List<Work>();
            int error = 0;

            foreach (Work w in works)
            {

                client.CreateClientAPI();
                //Work Work Work
                client.DestroyClientAPI();
            }
            Trace.Write("Exits Quotes:" + d);
            QuotesReturned(works);
        }

    }
    private void QuotesReturned(List<Work> works)
    {
        Model.PIP pip;
        foreach (Work w in works)
        {

        }
    }
}

The timer base function suppose run for life time in a web service... the problem is the timer works once or twice and it stops i don't know why? I can't find the reason why. It must be due to threading. There was same kind of problem when i implemented something like this based on even handlers instead of timer..

Timer StartedTimer Pass:0Entered Quotes:0Exits Quotes:1The thread '' (0xb18) has exited with code 0 (0x0). Timer Pass:1Entered Quotes:1Exits Quotes:2The thread '' (0x153c) has exited with code 0 (0x0). Timer Pass:2Entered Quotes:2Exits Quotes:3The thread '' (0xf10) has exited with code 0 (0x0). Timer Pass:3Entered Quotes:3Exits Quotes:4The thread '' (0x1044) has exited with code 0 (0x0). Timer Pass:4Entered Quotes:4Exits Quotes:5The thread '' (0xd24) has exited with code 0 (0x0). Timer Pass:5Entered Quotes:5Exits Quotes:6The thread '' (0x688) has exited with code 0 (0x0). Timer Pass:6Entered Quotes:6Exits Quotes:7The thread '' (0x17c8) has exited with code 0 (0x0). Timer Pass:7Entered Quotes:7Exits Quotes:8The thread '' (0xf34) has exited with code 0 (0x0). Timer Pass:8Entered Quotes:8Exits Quotes:9The thread '' (0x16f4) has exited with code 0 (0x0). Timer Pass:9Entered Quotes:9Exits Quotes:10The thread '' (0xf74) has exited with code 0 (0x0). Timer Pass:10Entered Quotes:10Exits Quotes:11The thread '' (0x938) has exited with code 0 (0x0). Timer Pass:11Entered Quotes:11Exits Quotes:12The thread '' (0x1048) has exited with code 0 (0x0). Timer Pass:12Entered Quotes:12Exits Quotes:13The thread '' (0x17dc) has exited with code 0 (0x0). Timer Pass:13Entered Quotes:13Exits Quotes:14The thread '' (0x174c) has exited with code 0 (0x0). Timer Pass:14Entered Quotes:14Exits Quotes:15The thread '' (0x132c) has exited with code 0 (0x0). Timer Pass:15Entered Quotes:15Exits Quotes:16The thread '' (0x3f8) has exited with code 0 (0x0). Timer Pass:16Entered Quotes:16Exits Quotes:17The thread '' (0x10cc) has exited with code 0 (0x0). Timer Pass:17The thread '' (0x1534) has exited with code 0 (0x0). Entered Quotes:17Exits Quotes:18The thread '' (0x1050) has exited with code 0 (0x0). The thread '' (0x850) has exited with code 0 (0x0). Timer Pass:18Entered Quotes:18Exits Quotes:19The thread '' (0xca0) has exited with code 0 (0x0). Timer Pass:19Entered Quotes:19Exits Quotes:20The thread '' (0x1120) has exited with code 0 (0x0). Timer Pass:20Entered Quotes:20Exits Quotes:21The thread '' (0x508) has exited with code 0 (0x0). Timer Pass:21Entered Quotes:21Exits Quotes:22The thread '' (0x1598) has exited with code 0 (0x0). Timer Pass:22Entered Quotes:22Exits Quotes:23The thread '' (0xb2c) has exited with code 0 (0x0). Timer Pass:23Entered Quotes:23Exits Quotes:24The thread '' (0x1600) has exited with code 0 (0x0). Timer Pass:24Entered Quotes:24Exits Quotes:25The thread '' (0x13bc) has exited with code 0 (0x0). Timer Pass:25Entered Quotes:25Exits Quotes:26The thread '' (0x131c) has exited with code 0 (0x0). Timer Pass:26Entered Quotes:26Exits Quotes:27Th开发者_StackOverflowe thread '' (0x5f4) has exited with code 0 (0x0). Timer Pass:27Entered Quotes:27Exits Quotes:28The thread '' (0x128) has exited with code 0 (0x0). Timer Pass:28Entered Quotes:28Exits Quotes:29The thread '' (0x1420) has exited with code 0 (0x0). Timer Pass:29The thread '' (0xc64) has exited with code 0 (0x0). Entered Quotes:29Exits Quotes:30The thread '' (0xedc) has exited with code 0 (0x0). Timer Pass:30Entered Quotes:30Exits Quotes:31The thread '' (0x136c) has exited with code 0 (0x0). Timer Pass:31Entered Quotes:31Exits Quotes:32The thread '' (0xeb8) has exited with code 0 (0x0). Timer Pass:32Entered Quotes:32Exits Quotes:33The thread '' (0x690) has exited with code 0 (0x0). Timer Pass:33Entered Quotes:33Exits Quotes:34The thread '' (0x1320) has exited with code 0 (0x0). Timer Pass:34Entered Quotes:34Exits Quotes:35The thread '' (0x11b8) has exited with code 0 (0x0). Timer Pass:35Entered Quotes:35Exits Quotes:36The thread '' (0x1684) has exited with code 0 (0x0). Timer Pass:36Entered Quotes:36Exits Quotes:37The thread '' (0x1294) has exited with code 0 (0x0). The thread '' (0x1240) has exited with code 0 (0x0). The thread '' (0xde8) has exited with code 0 (0x0).

and is stops....

and done...no good

Solution Put the line Timer timer; on the top of the class


The timer instance is declared within the ctor, then it will be garbaged within a relatively short time. Thus the death of the timer. Pin the "timer" var at the class level.

However, you should dispose the timer once you don't use anymore.

Cheers


  Timer timer = new Timer(new TimerCallback(GetQuotes), null, 0, 10000);
  timer.InitializeLifetimeService();

InitializeLifetimeService() returns an object that you're supposed to do something with. The default lease uses a timeout of 10 secs, same as your interval. Seems plausible that you timer is cancelled if processing takes a little too long.


You say this is a webservice - timers are not guaranteed to work correctly in a web service as the lifetime of the owning threads is not guaranteed. Consider using a global worker thread that is created when the app domain starts up.

A basic global worker thread is:

static bool _stopPolling = false;
static Thread _pollerThread;
static object _oneTimeLocker = new object();

private static CreatePollerThread()
{
  if(_pollerThread == null)
  {
    lock(_oneTimeLocker)
    {
      if(_pollerThread == null) //double-check
      {
        _pollerThread = new Thread(
        new ThreadStart(() => 
        { 
          while(true && !_stopPolling)
          { 
            DoWork(); 
            Thread.Sleep(10000); 
          }
        }));  
        _pollerThread.Start();
      }
    }
  }
}

I've used a double-checked lock there in case you want to fire up the global thread in response to a request (not best policy - since you can then have multiple requests trying to intialise the thread, potentially doing it multiple times). This isn't the only way - but it's rolling off my tongue at the moment given that I'm at work and don't have a lot of time!

An important factor here is the _stopPolling boolean that you use to shut the thread down in a friendly manner.

If you're in Asp.Net - then this SO: Application_End and background processes, exiting ASP.Net application gracefully, with answer by Aristos, will help you with how best to shut the thread down.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号