【发布时间】:2011-07-20 19:34:03
【问题描述】:
这是我第一次尝试创建调用多个线程的 Windows 服务。我所做的是创建一个控制台应用程序,然后添加一个 Windows 服务和安装程序。我的代码如下。
partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
System.Timers.Timer timer1 = new System.Timers.Timer();
timer1.Interval = 10000;
timer1.Start();
timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
}
public static void timer1_Elapsed(object sender, EventArgs e)
{
Program.ThreadController();
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
}
}
class Program
{
static void Main(string[] args)
{
ThreadController();
}
public static void ThreadController()
{
for (int i = 0; i < 3; i++)
{
new Thread(() => { processEventLogTesting(); });
}
}
public static void processEventLogTesting()
{
EventLog.WriteEntry("ProcessThread1", "Process1 Works");
}
}
我不确定为什么会收到此错误,任何提示都会很棒。
谢谢。
【问题讨论】:
标签: c# multithreading windows-services