private ManualResetEvent _shutdownEvent = new ManualResetEvent(false);
private Thread _thread;
protected override void OnStart(string[] args)
{
_thread = new Thread(WorkerThreadFunc);
_thread.Name = "Upload";
_thread.IsBackground = true;
_thread.Start();
} protected override void OnStop()
{
_shutdownEvent.Set();
if (!_thread.Join(3000))// give the thread 3 seconds to stop
{
_thread.Abort();
}
} protected void WorkerThreadFunc()
{
while (!_shutdownEvent.WaitOne(1000))
{
DoWork();
}
}DoWork 是反复扫描一个目录,如果发现有订单数据(其他程序放进去的),就upload到ftp上,当stop这个service时,我想等dowork()结束,在终止service.否则会发生一个订单数据(若干文件组成)一部分传到ftp,一部分还在本地的情况。
private Thread _thread;
protected override void OnStart(string[] args)
{
_thread = new Thread(WorkerThreadFunc);
_thread.Name = "Upload";
_thread.IsBackground = true;
_thread.Start();
} protected override void OnStop()
{
_shutdownEvent.Set();
if (!_thread.Join(3000))// give the thread 3 seconds to stop
{
_thread.Abort();
}
} protected void WorkerThreadFunc()
{
while (!_shutdownEvent.WaitOne(1000))
{
DoWork();
}
}DoWork 是反复扫描一个目录,如果发现有订单数据(其他程序放进去的),就upload到ftp上,当stop这个service时,我想等dowork()结束,在终止service.否则会发生一个订单数据(若干文件组成)一部分传到ftp,一部分还在本地的情况。
这句使用using应该可以吧.
protected override void OnStart(string[] args)
{
using(_thread = new Thread(WorkerThreadFunc))
{
_thread.Name = "Upload";
_thread.IsBackground = true;
_thread.Start();
}
}
_thread.Join() --一直等待