スレッドのタイマーに関する質問がありますか?ThreadPoolとThreading.Timer?
マイコード:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Testing testing = new Testing();
Thread thread = new Thread(new ThreadStart(delegate
{
testing.Start();
}));
thread.IsBackground = true;
thread.Start();
}
}
public class Testing
{
System.Threading.Timer timer = null;
public void Start()
{
if (timer == null)
timer = new System.Threading.Timer(timerTick, null, 500, 500);
List<ManualResetEvent> amanual = new List<ManualResetEvent>();
for (int index = 0; index < 10; index++)
{
ManualResetEvent manual = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object context)
{
this.RunningThreadPool(context);
manual.Set();
}
));
amanual.Add(manual);
}
WaitHandle.WaitAll(amanual.ToArray());
Console.WriteLine("Task complete");
}
void timerTick(object sender)
{
Console.WriteLine("Timer running");
}
public void RunningThreadPool(object context)
{
Console.WriteLine("Thread in Threadpool is running...");
}
}
}
そして、これは、スレッドプール内 スレッドが実行されている結果とされて...スレッドプール内 スレッドは、スレッドプール内 スレッドが実行されている...実行しています... スレッドプール内のスレッドが実行中です... スレッドプール内のスレッドが実行中です... スレッドプール内のスレッドが実行中です... スレッドプール内のスレッドが実行中です... スレッド内スレッドプールは、私はどのように行うことを質問がある
を実行している タイマーを実行している タイマーを実行している タイマーを実行している...スレッドプール内 スレッドが実行されている...スレッドプール内 スレッドが実行されている... タスク完全 タイマーを実行していますスレッドプールが実行される前にタイマーが実行されます。スレッドプールのタイマーを設定します。なぜなら私はスレッドプール上でタイマーチェックスレッドを必要とするからです。
しかし、私はThreadPoolにタイマーを入れていません。これのための任意の解決策@spender? –
@HùngLêXuânいいえ、タイマーはThreadPoolでコールバックをキューに入れます。 http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx –
でRemarksで2番目の箇条書きを読んでください。これは、ThreadPoolで大量のワークロードを実行し、作業でそれを圧倒することが問題である理由です。多くの.Net APIは、ThreadPoolの残りの流体に依存しており、アプリケーションで予期しない遅延が発生する可能性があります。自分のスレッドで作業を実行することを検討してください。 – spender