【发布时间】:2014-08-14 09:03:04
【问题描述】:
我有多线程的 Windows 服务,线程包含应该始终激活的 while 循环,从安装在那里的其他应用程序发送错误的套接字线程,初始化几个计时器的线程 当我启动线程几秒钟,然后抛出异常,表示我的服务停止工作并要求将数据发送给 Microsoft。 我不知道是什么让我的服务做到了,可能是线程,但我不明白我的实现有什么问题 另外我使用自定义安装程序,当用户启动应用程序时,它将安装该服务。 tools 是一个包含静态数学的类。 我的代码:
public partial class MyService : ServiceBase
{
public static System.Timers.Timer key;
public static System.Timers.Timer browse;
public static System.Timers.Timer sender;
// array of worker threads
Thread[] workerThreads;
public MyService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
workerThreads = new Thread[]
{
new System.Threading.Thread(new ThreadStart(InitTimers)),
new System.Threading.Thread(new ThreadStart(Tools.CheckApplication)),
new System.Threading.Thread(new ThreadStart(Tools.CheckBrowser)),
new System.Threading.Thread(new ThreadStart(Tools.SendOnError))
};
// start the threads
for (int i = 0; i < workerThreads.Length; i++)
{
workerThreads[i].Start();
}
}
protected override void OnStop()
{
}
public void InitTimers()
{
key = new System.Timers.Timer();
key.Interval = 1;
key.Elapsed += Other.key_Tick;
key.Enabled = true;
browse = new System.Timers.Timer();
browse.Interval = 1;
browse.Elapsed += Other.browse_Tick;
browse.Enabled = true;
sender = new System.Timers.Timer();
sender.Interval = 3600000;
sender.Elapsed += Other.sender_Tick;
sender.Enabled = true;
key.Start();
sender.Start();
browse.Start();
}
}
编辑:
如果我找到了正确的日志,那么例外是:
The thread tried to read from or write to a virtual address for which it does not have the appropriate access.
从来没有见过它...从什么异常来,为什么? p.s 日志是假设发送给 Microsoft 的日志,我没有创建事件日志,所以我想我没有
【问题讨论】:
-
你能检查一下事件日志吗?该服务应该已经创建了一个错误日志,其中可能包含其他信息...您也可以尝试使用管理员用户启动服务来检查是否是权限问题。
-
您的一个线程失败了...您发布的代码看起来不错...
-
已编辑:添加异常
-
注释掉每个新线程,直到您发现哪个线程抛出了错误 - 如果您仍然需要帮助,请发布它的代码。
-
您可能需要执行stackoverflow.com/a/3372358/292411 中提到的检查(相同的错误消息)
标签: c# windows multithreading service