简体   繁体   English

线程计时器不起作用?

[英]Threading Timer Not working?

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? 计时器基本功能假定在Web服务中终身运行...问题是计时器工作一次或两次,并且计时器停止了,我不知道为什么? 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 StartedTimer Pass:0输入的报价:0退出的报价:1线程''(0xb18)已退出,代码为0(0x0)。 Timer Pass:1Entered Quotes:1Exits Quotes:2The thread '' (0x153c) has exited with code 0 (0x0). 计时器通过:1输入的报价:1退出的报价:2线程''(0x153c)已退出,代码为0(0x0)。 Timer Pass:2Entered Quotes:2Exits Quotes:3The thread '' (0xf10) has exited with code 0 (0x0). 计时器通过:2输入的报价:2退出的报价:3线程''(0xf10)已退出,代码为0(0x0)。 Timer Pass:3Entered Quotes:3Exits Quotes:4The thread '' (0x1044) has exited with code 0 (0x0). 计时器通过:3输入的引号:3退出的引号:4线程''(0x1044)已退出,代码为0(0x0)。 Timer Pass:4Entered Quotes:4Exits Quotes:5The thread '' (0xd24) has exited with code 0 (0x0). 计时器通过:4输入的报价:4退出的报价:5线程''(0xd24)已退出,代码为0(0x0)。 Timer Pass:5Entered Quotes:5Exits Quotes:6The thread '' (0x688) has exited with code 0 (0x0). 计时器通过:5输入的报价:5退出的报价:6线程''(0x688)已退出,代码为0(0x0)。 Timer Pass:6Entered Quotes:6Exits Quotes:7The thread '' (0x17c8) has exited with code 0 (0x0). 计时器通过:6输入的报价:6退出的报价:7线程''(0x17c8)已退出,代码为0(0x0)。 Timer Pass:7Entered Quotes:7Exits Quotes:8The thread '' (0xf34) has exited with code 0 (0x0). 计时器通过:7输入的报价:7退出的报价:8线程''(0xf34)已退出,代码为0(0x0)。 Timer Pass:8Entered Quotes:8Exits Quotes:9The thread '' (0x16f4) has exited with code 0 (0x0). 计时器通过:8输入报价:8退出报价:9线程''(0x16f4)已退出,代码为0(0x0)。 Timer Pass:9Entered Quotes:9Exits Quotes:10The thread '' (0xf74) has exited with code 0 (0x0). 计时器通过:9输入报价:9退出报价:10线程''(0xf74)已退出,代码为0(0x0)。 Timer Pass:10Entered Quotes:10Exits Quotes:11The thread '' (0x938) has exited with code 0 (0x0). 计时器通过:10输入的引号:10退出的引号:11线程''(0x938)已退出,代码为0(0x0)。 Timer Pass:11Entered Quotes:11Exits Quotes:12The thread '' (0x1048) has exited with code 0 (0x0). 计时器通过:11输入的报价:11退出的报价:12线程''(0x1048)已退出,代码为0(0x0)。 Timer Pass:12Entered Quotes:12Exits Quotes:13The thread '' (0x17dc) has exited with code 0 (0x0). 计时器传递:12输入报价:12退出报价:13线程''(0x17dc)已退出,代码为0(0x0)。 Timer Pass:13Entered Quotes:13Exits Quotes:14The thread '' (0x174c) has exited with code 0 (0x0). 计时器通过:13输入的报价:13退出的报价:14线程''(0x174c)已退出,代码为0(0x0)。 Timer Pass:14Entered Quotes:14Exits Quotes:15The thread '' (0x132c) has exited with code 0 (0x0). 计时器通过:14输入报价:14退出报价:15线程''(0x132c)已退出,代码为0(0x0)。 Timer Pass:15Entered Quotes:15Exits Quotes:16The thread '' (0x3f8) has exited with code 0 (0x0). 计时器通过:15输入的报价:15退出的报价:16线程''(0x3f8)已退出,代码为0(0x0)。 Timer Pass:16Entered Quotes:16Exits Quotes:17The thread '' (0x10cc) has exited with code 0 (0x0). 计时器通过:16输入报价:16退出报价:17线程''(0x10cc)已退出,代码为0(0x0)。 Timer Pass:17The thread '' (0x1534) has exited with code 0 (0x0). 计时器通过:17线程''(0x1534)已退出,代码为0(0x0)。 Entered Quotes:17Exits Quotes:18The thread '' (0x1050) has exited with code 0 (0x0). 输入的报价:17退出的报价:18线程''(0x1050)已退出,代码为0(0x0)。 The thread '' (0x850) has exited with code 0 (0x0). 线程''(0x850)已退出,代码为0(0x0)。 Timer Pass:18Entered Quotes:18Exits Quotes:19The thread '' (0xca0) has exited with code 0 (0x0). 计时器通过:18输入报价:18退出报价:19线程''(0xca0)已退出,代码为0(0x0)。 Timer Pass:19Entered Quotes:19Exits Quotes:20The thread '' (0x1120) has exited with code 0 (0x0). 计时器通过:19输入报价:19退出报价:20线程''(0x1120)已退出,代码为0(0x0)。 Timer Pass:20Entered Quotes:20Exits Quotes:21The thread '' (0x508) has exited with code 0 (0x0). 计时器通过:20输入的报价:20退出的报价:21线程''(0x508)已退出,代码为0(0x0)。 Timer Pass:21Entered Quotes:21Exits Quotes:22The thread '' (0x1598) has exited with code 0 (0x0). 计时器通过:21输入的报价:21退出的报价:22线程''(0x1598)已退出,代码为0(0x0)。 Timer Pass:22Entered Quotes:22Exits Quotes:23The thread '' (0xb2c) has exited with code 0 (0x0). 计时器通过:22输入的报价:22退出的报价:23线程''(0xb2c)已退出,代码为0(0x0)。 Timer Pass:23Entered Quotes:23Exits Quotes:24The thread '' (0x1600) has exited with code 0 (0x0). 计时器通过:23输入报价:23退出报价:24线程''(0x1600)已退出,代码为0(0x0)。 Timer Pass:24Entered Quotes:24Exits Quotes:25The thread '' (0x13bc) has exited with code 0 (0x0). 计时器通过:24输入的报价:24退出的报价:25线程''(0x13bc)已退出,代码为0(0x0)。 Timer Pass:25Entered Quotes:25Exits Quotes:26The thread '' (0x131c) has exited with code 0 (0x0). 计时器传递:25输入报价:25退出报价:26线程''(0x131c)已退出,代码为0(0x0)。 Timer Pass:26Entered Quotes:26Exits Quotes:27The thread '' (0x5f4) has exited with code 0 (0x0). 计时器通过:26输入报价:26退出报价:27线程''(0x5f4)已退出,代码为0(0x0)。 Timer Pass:27Entered Quotes:27Exits Quotes:28The thread '' (0x128) has exited with code 0 (0x0). 计时器传递:27输入报价:27退出报价:28线程''(0x128)已退出,代码为0(0x0)。 Timer Pass:28Entered Quotes:28Exits Quotes:29The thread '' (0x1420) has exited with code 0 (0x0). 计时器通过:28输入的报价:28退出的报价:29线程''(0x1420)已退出,代码为0(0x0)。 Timer Pass:29The thread '' (0xc64) has exited with code 0 (0x0). 计时器通过:29线程''(0xc64)已退出,代码为0(0x0)。 Entered Quotes:29Exits Quotes:30The thread '' (0xedc) has exited with code 0 (0x0). 输入的报价:29退出的报价:30线程''(0xedc)已退出,代码为0(0x0)。 Timer Pass:30Entered Quotes:30Exits Quotes:31The thread '' (0x136c) has exited with code 0 (0x0). 计时器通过:30输入的报价:30退出的报价:31线程''(0x136c)已退出,代码为0(0x0)。 Timer Pass:31Entered Quotes:31Exits Quotes:32The thread '' (0xeb8) has exited with code 0 (0x0). 计时器传递:31输入报价:31退出报价:32线程''(0xeb8)已退出,代码为0(0x0)。 Timer Pass:32Entered Quotes:32Exits Quotes:33The thread '' (0x690) has exited with code 0 (0x0). 计时器通过:32输入的报价:32退出的报价:33线程''(0x690)已退出,代码为0(0x0)。 Timer Pass:33Entered Quotes:33Exits Quotes:34The thread '' (0x1320) has exited with code 0 (0x0). 计时器传递:33输入的报价:33退出的报价:34线程''(0x1320)已退出,代码为0(0x0)。 Timer Pass:34Entered Quotes:34Exits Quotes:35The thread '' (0x11b8) has exited with code 0 (0x0). 计时器通过:34输入报价:34退出报价:35线程''(0x11b8)已退出,代码为0(0x0)。 Timer Pass:35Entered Quotes:35Exits Quotes:36The thread '' (0x1684) has exited with code 0 (0x0). 计时器通过:35输入的报价:35退出的报价:36线程''(0x1684)已退出,代码为0(0x0)。 Timer Pass:36Entered Quotes:36Exits Quotes:37The thread '' (0x1294) has exited with code 0 (0x0). 计时器通过:36输入报价:36退出报价:37线程''(0x1294)已退出,代码为0(0x0)。 The thread '' (0x1240) has exited with code 0 (0x0). 线程''(0x1240)已退出,代码为0(0x0)。 The thread '' (0xde8) has exited with code 0 (0x0). 线程''(0xde8)已退出,代码为0(0x0)。

and is stops.... 并停止....

and done...no good 完成...不好

Solution Put the line Timer timer; 解决方案:将线路Timer定时器置为; on the top of the class 一流的

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

InitializeLifetimeService() returns an object that you're supposed to do something with. InitializeLifetimeService()返回您应该使用的对象。 The default lease uses a timeout of 10 secs, same as your interval. 默认租约使用10秒的超时时间,与您的间隔相同。 Seems plausible that you timer is cancelled if processing takes a little too long. 如果处理时间过长,似乎取消了计时器。

The timer instance is declared within the ctor, then it will be garbaged within a relatively short time. 计时器实例在ctor中声明,然后将在较短的时间内将其丢弃。 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 干杯

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. 您说这是一个Web服务-不能保证计时器在Web服务中正常工作,因为不能保证拥有线程的生存期。 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. 这里的一个重要因素是_stopPolling布尔值,您可以使用它以友好的方式关闭线程。

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. 如果您位于Asp.Net中,那么该SO: Application_End和后台进程会在Aristos的答复下正常退出ASP.Net应用程序 ,这将帮助您以最佳方式关闭线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM