Windows Server 2003 上的 System.Threading.Timer 不计时

发布于 2024-12-11 07:33:22 字数 1010 浏览 0 评论 0原文

目前,我在将 Windows 服务部署到 Win2k3 服务器时遇到 System.Threading.Timer 问题。当我安装它时,它在我的本地计算机上每秒运行得很好(出于调试目的而过度使用),但是在我将它安装到服务器后,该服务成功启动,然后再也没有触发计时器事件。以前有人遇到过类似的情况吗?我的大部分研究都发现了 .NET 1.1 中从很久以前就普遍存在的问题。感谢您的时间和帮助。

   protected override void OnStart(string[] args)
   {
        int interval = Int32.Parse(ConfigurationManager.AppSettings["SleepTime"]) * 1000;
        TimerCallback cb = new TimerCallback(ProcessTimerEvent);
        QueueWorker worker = new QueueWorker();
        workTimer = new Timer(cb, worker, interval, 1000);

    }
    private static void ProcessTimerEvent(object obj)
    {
        if (obj is QueueWorker)
        {
            QueueWorker qw = (QueueWorker)obj;
            qw.doWork();
        }
    }
    class QueueWorker
    {
        public void doWork()
        {
           // work happening here
        }
    }

更新:

我发现这与服务运行所需的用户有关。当服务作为系统运行时,线程工作得很好。有人知道用户需要什么权限才能运行线程吗?我尝试在本地安全设置中使用“作为操作系统的一部分”,但这只会导致服务启动,然后以我添加的用户身份登录时立即失败。

I am currently having trouble with a System.Threading.Timer when deploying my Windows Service to a Win2k3 Server. It works just fine ticking every second (overkill for debugging purposes) on my local machine when I install it, but after I install it to the server, the service starts successfully then never fires the timer event once. Has anyone run into a situation similar to this before? Most of my research has turned up issues that were apparently prevalent in .NET 1.1 from forever ago. Thanks for your time and help.

   protected override void OnStart(string[] args)
   {
        int interval = Int32.Parse(ConfigurationManager.AppSettings["SleepTime"]) * 1000;
        TimerCallback cb = new TimerCallback(ProcessTimerEvent);
        QueueWorker worker = new QueueWorker();
        workTimer = new Timer(cb, worker, interval, 1000);

    }
    private static void ProcessTimerEvent(object obj)
    {
        if (obj is QueueWorker)
        {
            QueueWorker qw = (QueueWorker)obj;
            qw.doWork();
        }
    }
    class QueueWorker
    {
        public void doWork()
        {
           // work happening here
        }
    }

UPDATE:

I've found that it has something to do with the user that the service needs to run as. When the service runs as System, the threads work great. Anyone know what permissions are needed for a user to be able to run the threads? I've tried using the "Act as part of the operating system" in the local security settings, but that just causes the service to start up then immediately fail when logged in as the user I added.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぶ宁プ宁ぶ 2024-12-18 07:33:22

计时器可能正在触发。您可以在目标计算机上运行 DebugView:

http://technet.microsoft.com/ en-us/sysinternals/bb896647.aspx

然后您可以在代码中调用以查看正在执行的内容,如下所示:

System.Diagnostics.Debug.WriteLine("Beginning of Timer event...");

System.Diagnostics.Debug.WriteLine("End of Timer event");

The timer might be firing. You can run DebugView on the target machine:

http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx

Then you can put calls in your code to see what is executing like this:

System.Diagnostics.Debug.WriteLine("Beginning of Timer event...");

System.Diagnostics.Debug.WriteLine("End of Timer event");
脸赞 2024-12-18 07:33:22

我可以建议几个解决方案:

  1. 使用控制台应用程序而不是 Windows 服务。从任务计划程序运行控制台应用程序。
  2. 构建您自己的计时器类。使用您自己的线程并在轮询间隔内睡眠。醒来后,做你的工作。然后再睡觉。

为了完整起见,以下是一些详细介绍实际错误的文章:

A couple of solutions I can suggest:

  1. Use a console app and not a windows service. Run your console app from the task scheduler.
  2. Build your own timer class. Use your own thread(s) and sleep for your poll interval. On wake, do your work. Then sleep again.

And for completeness, here are some of the articles which detail the actual bug:

善良天后 2024-12-18 07:33:22

好吧。感谢您对此的所有建议。我制定了一个非常激进的日志方案,我相信服务器出于某种原因正在杀死我的线程。我添加了一个 GC.KeepAlive(workTimer) ,它似乎解决了计时器不滴答的问题。

以防万一其他人遇到这个问题,在我看来,本地系统获得线程优先级,但其他用户可能会因为某种原因而销毁他们的线程。由于我无法完全访问要部署此功能的服务器,因此我会将修复归功于 KeepAlive 或服务器团队在我背后修复了某些内容。

Alright. Thanks for all your suggestions on this. I made a very aggressive log scheme and I believe that the server was killing my threads for some reason. I added a GC.KeepAlive(workTimer) and it seems to have resolved the issue of the timer not ticking.

Just in case someone else runs into this problem, it appears to me that the Local System gets thread priority, but other users can potentially have their threads destroyed for some reason or the other. Since I don't have complete access to the server that I am deploying this on, I will chalk the fix up to the KeepAlive or the server team fixed something behind my back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文