private void Btn_start_Click(object sender, EventArgs e) { //设置按钮状态 btn_start.Enabled = false; btn_import.Enabled = false; combo_api.Enabled = false; tb_thread.Enabled = false; btn_stop.Enabled = true; lv_domains.Items.Clear(); //获取数据 currentApi = combo_api.SelectedIndex; threadCount = int.Parse(tb_thread.Text); threadSleep = int.Parse(tb_sleep.Text); threadPauseTime = int.Parse(txt_threadPause.Text); //设置flag workStarted = true; //设置变量 processedCount = 0; initStats(); UpdateProgressBar(0); //开始工作 WorkHandler handler = new WorkHandler(StartWork); handler.BeginInvoke(threadSleep, new AsyncCallback(WorkCallback), null); }
static void Main(string[] args) { WorkHandler wh = DoSomething; Thread.CurrentThread.Name = "Main Thread"; var ar = wh.BeginInvoke(20, asyncResult => { Console.WriteLine("DoSomething Done"); }, null); for (int i = 0; i < 20; i++) { IntReturner ir = GetInteger; var ar2 = ir.BeginInvoke(asyncResult => { Console.WriteLine("GetInteger Done"); }, null); } int remainingWorkerThreads, remainingIOThreads; ThreadPool.GetAvailableThreads(out remainingWorkerThreads, out remainingIOThreads); Console.WriteLine($"While the worker thread is doing its work, here we are in the {Thread.CurrentThread.Name} again. Remaining threads: {remainingWorkerThreads}"); Console.ReadKey(); }