本文介绍了暂停并恢复线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 亲爱的所有 这里是我的代码..我想暂停线程并在需要时恢复它 这是我的代码 私人 void sendbutton_Click( object sender,EventArgs e) { if (listView1.Items.Count == 0 ) { MessageBox.Show( 请选择频道); return ; } if (listView2.Items.Count == 0 ) { MessageBox.Show( 请选择whatsapp number); return ; } k = ; countoperation = 0 ; progressBar2.Value = 1 ; progressBar2.Visible = true ; progressBar2.Maximum = listView2.Items.Count; sendbutton.Enabled = false ; ThreadPausee.Enabled = true ; th2 = new 线程( new ThreadStart(sendmessageoperation)); th2.Start(); } private void sendmessageoperation() { for ( int i = 0 ; i < listView1 .Items.Count; i ++) { if (listView2.Items.Count == 0 ) { break ; } sendwithinvoke(i); Thread.Sleep( 300 ); } 调用( new Action(()= > { textBox1.Clear(); progressBar2.Visible = 假; })); k + = \\\\ n + Total Sent + countoperation; savefile(SentSucssesChannel, true ,k); if (th2.IsAlive == true ) th2 .Abort(); } private void sendwithinvoke( int i) { if (InvokeRequired) { this .Invoke( new updatesend(sendwithinvoke),新 对象 [] {i}); return ; } for ( int j = 0 ;Ĵ< Convert.ToInt32(textBox2.Text); J ++) $ { b $ b // 做点什么 } } private void ThreadPausee_Click( object 发件人,EventArgs e) { 尝试 { 如果(ThreadPausee.Text == 暂停) { if (th2.IsAlive == true ) { // 这里我想暂停th2线程 ThreadPausee.Text = 恢复; return ; } } else if (ThreadPausee.Text == 恢复) { // 这里我想恢复th2线程 ThreadPausee.Text = 暂停; return ; } } catch (例外) { throw ; } } 解决方案 添加类级别布尔:调用它pauseRequired并将其设置为false。 然后在你的点击处理程序中将其设置为true或false。 然后更改: Thread.Sleep( 300 ); 对此: do Thread.Sleep( 300 ); while (pauseRequired); 理论上,你应该对变量使用锁定,但在这种情况下它是无关紧要的,因为它只被一个线程改变,并被另一个线程监视。 BTW:你可能最好使用BackgroundWorker,因为它内置了进度报告。 Dear allhere is my code .. i want to pause the thread and resume it when i needhere is my codeprivate void sendbutton_Click(object sender, EventArgs e) { if (listView1.Items.Count == 0) { MessageBox.Show("Please select Channels"); return; } if (listView2.Items.Count == 0) { MessageBox.Show("Please select whatsapp number"); return; } k = ""; countoperation = 0; progressBar2.Value = 1; progressBar2.Visible = true; progressBar2.Maximum = listView2.Items.Count; sendbutton.Enabled = false; ThreadPausee.Enabled = true; th2 = new Thread(new ThreadStart(sendmessageoperation)); th2.Start(); } private void sendmessageoperation() { for (int i = 0; i < listView1.Items.Count; i++) { if (listView2.Items.Count == 0) { break; } sendwithinvoke(i); Thread.Sleep(300); } Invoke(new Action(() => { textBox1.Clear(); progressBar2.Visible = false; })); k += "\r\n" + "Total Sent " + countoperation; savefile(SentSucssesChannel, true, k); if (th2.IsAlive == true) th2.Abort(); } private void sendwithinvoke(int i) { if (InvokeRequired) { this.Invoke(new updatesend(sendwithinvoke), new object[] { i }); return; } for (int j = 0; j < Convert.ToInt32(textBox2.Text); j++) { //Do something }}private void ThreadPausee_Click(object sender, EventArgs e) { try { if (ThreadPausee.Text == "Pause") { if (th2.IsAlive == true) { //Here i want to pause the th2 thread ThreadPausee.Text = "Resume"; return; } } else if (ThreadPausee.Text == "Resume") { //Here i want to Resume the th2 thread ThreadPausee.Text = "Pause"; return; } } catch (Exception) { throw; } } 解决方案 Add a class level bool: call it pauseRequired and set it to false.Then in your click handler set it to true or false appropriately.Then change this:Thread.Sleep(300);To this:do Thread.Sleep(300); while(pauseRequired);In theory, you should use a lock on the variable, but in this case it's irrelevant since it's being changed by only one thread, and monitored by the other.BTW: You would probably be better off using a BackgroundWorker for this as it has progress reporting built in. 这篇关于暂停并恢复线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-29 19:20