本帖最后由 hnxxd 于 2013-11-24 00:43 编辑
我模拟了20个线程,而且定时性都设定不能,然后显示一些信息,按道理,我不关闭控制台程序,它是不会停止工作的,但是不知道为何,一般运行到8、9分钟后,前面19个定时器全部没有反应了,只有最后一个线程20,还在动作,这是为何呢?(附源码)
先谢过了!
[C#] 纯文本查看 复制代码 using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Transfer
{
public class ztThreading
{
private string strid;
private string strzd;
private string strqc;
private Int64 counter;
public ztThreading(string text0, string text1, string text2, int number)
{
strid = text0;
strzd = text1;
strqc = text2;
counter = number;
}
public void ThreadProc(Object e)
{
//这里加入数据抽取及去重插入新数据
try
{
counter += 1;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("线程" + strid + "," + strzd + strqc + ":执行次数:{0}", counter);
}
catch (System.Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("线程" + strid+"发生错误{0}",ex.ToString());
}
}
}
public class Program
{
static void Main(string[] args)
{
for (int i = 1; i < 21; i++)
{
ztThreading s = new ztThreading(Convert.ToString(i),"字段","去重", 0);
Timer time1 = new Timer(s.ThreadProc, s, 0, i * 1000);
}
while (true)
{
Thread.Sleep(5);
}
}
}
} |