自建ThreadStatus
来源:互联网 发布:windows一键重装助手 编辑:程序博客网 时间:2024/04/27 22:09
public partial class testService : ServiceBase
{
private Thread workThread;
private DateTime lastUpdateTime = DateTime.Now.AddDays(-1);
private ThreadStatus Running = ThreadStatus.None;
public testService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
workThread = new Thread(new ThreadStart(this.do));
workThread.Start();
SysLog.WriteTrace("HHTravel.SCM.Exchange.ETLPkgService已启动");
while (!workThread.IsAlive) ;
}
protected override void OnStop()
{
Running = ThreadStatus.Stopping;
for (int i = 0; i < 10; i++)
{
Thread.Sleep(2000);
if (Running == ThreadStatus.Stoped)
break;
}
SysLog.WriteTrace("HHTravel.SCM.Exchange.ETLPkgService已停止");
}
private void do()
{
Running = ThreadStatus.Running;
int intervalMinuts = int.Parse(ConfigurationManager.AppSettings["ETLIntervalMinuts"]);
while (Running == ThreadStatus.Running)
{
TimeSpan ts = DateTime.Now - lastUpdateTime;
if (ts.TotalMinutes >= intervalMinuts)
{
try
{
Execute exe = new Execute();
exe.Process();
}
catch (Exception e)
{
SysLog.WriteException("HHTravel.SCM.Exchange.ETLPkgService", e);
}
lastUpdateTime = DateTime.Now;
}
Thread.Sleep(1000);
}
Running = ThreadStatus.Stoped;
}
//线程三态,线程五态、七态,少了一个wailt(sleep、blocked),不过这里不控制
//如同订单状态的变迁一样,在一定条件下可以转换
private enum ThreadStatus
{
None,
Running,
Stopping,
Stoped
}
====================================
using System;
using System.Threading;
class Test
{
public static void Main()
{
Thread newThread = new Thread(new ThreadStart(TestMethod));
Console.WriteLine(newThread.ThreadState.ToString());
newThread.Start();
Console.WriteLine(newThread.ThreadState.ToString());
Thread.Sleep(1000);
// Abort newThread.
Console.WriteLine("Main aborting new thread.");
stop = true;
// newThread.Abort("Information from Main.");
Console.WriteLine(newThread.ThreadState.ToString());
// Wait for the thread to terminate.
newThread.Join();
Console.WriteLine(newThread.ThreadState.ToString());
Console.WriteLine("New thread terminated - Main exiting.");
Console.ReadLine();
}
static void TestMethod()
{
try
{
while(true)
{
Console.WriteLine("New thread running.");
Thread.Sleep(1000);
}
}
catch(ThreadAbortException abortException)
{
Console.WriteLine((string)abortException.ExceptionState);
}
}
}
- 自建ThreadStatus
- 自建 AppRTC
- 自建搜索引擎
- 自建CA
- 自建ASP服务器
- 自建引入表
- 自建DataSet/webForm
- SAP 自建搜索帮助
- 自建表注意事项
- 自建控件:ascx
- IIS自建网站
- 基于FUNCTION自建数据源
- freebsd 自建服务脚本
- android 自建键盘
- python自建对象
- linux自建命令别名
- 自建JavaScript函数库
- 自建项目动静分离
- numeric_limits
- javascript数组操作方法
- 数据库优化的几条【忠告】,数据库优化者进。
- 学习笔记——进程和线程的区别
- 框架指针的省略(FPO)
- 自建ThreadStatus
- 爱情表白谜语
- Windows 安装 xampp 开启服务 出错
- 动手练习,将前面的知识用于实践
- 项目测试总结
- Eclipse快捷键大全
- OCP-043 ORA_SDTZ time zone enviroment variables
- 如何发送信息到Twitter上面
- 浅谈android的selector背景选择器