2009-11-12 202 views
0

我的Windows服务能够在Win XP中启动线程(启动ThreadStart委托),但在Win 2003 Server中它不能,它也不会引发异常...线程根本无法启动。Windows服务无法启动Win 2003服务器中的线程

我做了一个在(OnStart)事件处理程序中具有相同代码的测试Windows服务,它在Win XP和Win 2003 Server上工作,这让我发疯,我不知道我的原始服务有什么问题,为什么它不能启动线程。

这里是我的两个服务取胜的问题,并在测试服务赢得了代码工作就好了:

private Thread trd; 
    StreamWriter sw; 
    int i = 0; 


    protected override void OnStart(string[] args) 
    { 
     // TODO: Add code here to start your service. 
     sw = new StreamWriter("c:\\TestingService.txt", true); 

     trd = new Thread(new ThreadStart(this.LoopingThread)); 
     trd.IsBackground = false; 
     trd.Priority = ThreadPriority.Highest; 
     trd.Start(); 
    } 

    protected override void OnStop() 
    { 
     // TODO: Add code here to perform any tear-down necessary to stop your service. 
    } 

    private void LoopingThread() 
    { 
     while (i < 100) 
     { 
      lock (sw) 
      { 
       sw.WriteLine("hello from thread i="+i.ToString()); 
       sw.Flush(); 
      } 
      i++; 
      Thread.Sleep(1000); 
     } 
    } 

这个代码是在两个服务取胜“正是”相同。 我原来的服务(有问题)得到了其他DLL多次提到,其“使用”列表是:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Diagnostics; 
using System.ServiceProcess; 
using System.Text; 
using System.IO; 
using System.Xml; 
using System.Security.Principal; 
using System.Reflection; 
using System.Threading; 
using System.Management; 

和其他using语句是相关的一些机密的DLL(第三方) 但我我实际上并没有创建任何对象......有效的代码就是我发布的内容。

我无法弄清楚,为什么我的服务赢得不能在2003年赢得服务器启动线程

+0

什么是在事件日志中为此?它会给我们更多的线索来帮助你 – AutomatedTester 2009-11-12 08:17:20

+0

我有点困惑 - 你是说你发布的代码本身工作正常(在Win2003上),但是当你添加引用到第三方(和其他DLL)和使用语句显示它停止工作? – 2009-11-12 08:35:46

回答

0

这是以非常愚蠢的方式解决的! 我刚刚在Windows服务中创建了另一个类,将所有代码复制到它,然后在program.cs中创建代码,而不是旧服务类的实例。

之后一切正常,我不知道发生了什么!

感谢所有那些试图帮助的人

1

在你的OnStart()方法的开头放至System.Diagnostics.Debugger.Break()的调用,并在调试编译。当您启动该服务时,系统会提示您启动调试会话。进入调试器后,从“调试”菜单中打开“异常”对话框,并查看“抛出”列以了解公共语言运行时异常。如果发生异常,您的服务将暂停。

如果我不得不猜测,我会说你的线程没有启动的原因是因为它没有那么做。根据您提供的代码,我会说由于某种原因,StreamWriter的创建失败。例如,您可能没有Win 2003 Server计算机上的C驱动器的写入权限。