我的asp.net后台程序中有两个线程,一个队列(Queue myQueue)。
其中一个线程是用来接受对方通过url发送过来的数据,并将数据添加到队列(myQueue.EnQueue());
另一个线程用来不断从队列中取出数据,并处理。
我的问题是,我如何能确保第二个线程不断的执行;
这如果在WinForm下不难实现,无非是使用一个Timer,可我对WebForm的运行机理不是很了解,我在猜想WebForm是不是只有在接受url的时候才运行?!
请高手指点,多谢拉!

解决方案 »

  1.   

    启动一个线程,让每段时间运行一下!!线程有设置时间,请查MSDN
      

  2.   

    如果Windows行的话,那你在asp.net的Global.asax中放置一个Timer控件进行操作,相当于服务。
      

  3.   

    启动的线程,要重新写类,用单件模式(保证线程不是每次刷新网页重新创建一个线程),时间设置请参考下面程序
    using System;
    using System.Threading;class TimerExampleState 
    {
       public int counter = 0;
       public Timer tmr;
    }class App 
    {
       public static void Main()
       {
       TimerExampleState s = new TimerExampleState();   // Create the delegate that invokes methods for the timer.
       TimerCallback timerDelegate = new TimerCallback(CheckStatus);   // Create a timer that waits one second, then invokes every second.
       Timer timer = new Timer(timerDelegate, s,1000, 1000);
        
       // Keep a handle to the timer, so it can be disposed.
       s.tmr = timer;   // The main thread does nothing until the timer is disposed.
       while(s.tmr != null)
             Thread.Sleep(0);
       Console.WriteLine("Timer example done.");
       }
       // The following method is called by the timer's delegate.   static void CheckStatus(Object state)
       {
       TimerExampleState s =(TimerExampleState)state;
       s.counter++;
       Console.WriteLine("{0} Checking Status {1}.",DateTime.Now.TimeOfDay, s.counter);
       if(s.counter == 5)
          {
          // Shorten the period. Wait 10 seconds to restart the timer.
          (s.tmr).Change(10000,100);
          Console.WriteLine("changed...");
          }
       if(s.counter == 10)
          {
          Console.WriteLine("disposing of timer...");
          s.tmr.Dispose();
          s.tmr = null;
          }
       }
    }
      

  4.   

    类似的例子
    Simulate a Windows Service using ASP.NET to run scheduled jobs
    Run scheduled jobs 24x7 using ASP.NET without requiring a Windows Service. 
    http://www.codeproject.com/aspnet/ASPNETService.asp
      

  5.   

    秋枫说的方法我觉得有可能会实现,现在的问题集中到如何确保,多个url在请求页面的时候并不是为每个用户创建两个新的线程,而是使用的是同样的两个线程,并保证只要队列中有数据存在第二个线程的持续执行