代码如下
public void Test()
{
for (int i = 0; i < 10; i++)
ExFaceSrv();
}
private void ExFaceSrv()
{
if (string.IsNullOrEmpty(启动时间))
{//第一次运行
启动时间 = DateTime.Now.ToString();
Loger.WriteLogEx(“主线程已启动”);
if (!backgroundWorker1.IsBusy)
{
backgroundWorker1.RunWorkerAsync();
}
Loger.WriteLogEx(“Main:主线程已停止”);
{
for (int i = 0; i < 10; i++)
ExFaceSrv();
}
private void ExFaceSrv()
{
if (string.IsNullOrEmpty(启动时间))
{//第一次运行
启动时间 = DateTime.Now.ToString();
Loger.WriteLogEx(“主线程已启动”);
if (!backgroundWorker1.IsBusy)
{
backgroundWorker1.RunWorkerAsync();
}
Loger.WriteLogEx(“Main:主线程已停止”);
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
while (!backgroundWorker1.CancellationPending)
{
FaceAutoComParePro.Start();
Thread.Sleep(1000);
//sn++;
//Loger.WriteLogEx(“Main,当前SN:” + sn);
}
}
catch (Exception ex)
{
string es = ex.Message;
}
}
public static void Start()
{
BLL bll = new BLL();
dts = bll.GetDataTables();
list = bll.GetCompareWfList();
Loger.WriteLogEx(“本次比对量:” + list.Count);
if (CompareingQueue.Count <= 0)
CheckDbWait();
if (CompareingQueue.Count <= 0)
{
return;
}
ThreadStart ts = new ThreadStart(DoWork);
for (int i = 0; i < Process_MaxLength; i++)
{
tr[i] = new Thread(ts);//指定线程起始设置
tr[i].Start();
}
}
解决方案:40分
子线程执行完了会自动销毁,但频繁的创建-销毁线程是绝对应该避免的,系统中提供了线程池,尽量用这个
自己查下ThreadPool或者Task的用法
自己查下ThreadPool或者Task的用法