C#多线程预期结果,按下按钮时两个线程交替执行; 实际结果,task1强制执行完释放后才执行task2。
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int test1 = 0;
Thread Ttask1 = null;
public Form1()
{
InitializeComponent();
Thread Ttask2 = new Thread(new ThreadStart(tttask2));
Ttask2.IsBackground = true;
Ttask2.Start();
}
private delegate void TASK_DELT();
private void task2()
{
if (test1++ % 2 == 0)
{
pictureBox1.BackColor = Color.Blue;
}
else
{
pictureBox1.BackColor = Color.Red;
}
textBox1.Clear();
textBox1.Text = test1.ToString();
textBox1.Refresh();
}
private void tttask2()
{
while (true)
{
Thread.Sleep(250);
//如果是跨线程调用
if (InvokeRequired)
{
this.textBox1.BeginInvoke(new TASK_DELT(task2));
}
else
{
task2();
}
}
}
private void delays(int t)
{
for (int i = 0; i < t; i++)
{
textBox1.Clear();
textBox1.Text = i.ToString();
textBox1.Refresh();
Thread.Sleep(1);
for (int j = 0; j < 0x08f00000; j++) ;
}
pictureBox2.BackColor = Color.Green;
}
private delegate void TASKPRA_DELT(int i);
private void task1()
{
//如果是跨线程调用
if (InvokeRequired)
{
this.textBox1.Invoke(new TASKPRA_DELT(delays), 10);
}
else
{
delays(10);
}
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox2.BackColor = Color.Transparent;
Ttask1 = new Thread(new ThreadStart(task1));
Ttask1.IsBackground = true;
Ttask1.Start();
}
}
}
请问怎么保证两个线程交替执行? 展开
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int test1 = 0;
Thread Ttask1 = null;
public Form1()
{
InitializeComponent();
Thread Ttask2 = new Thread(new ThreadStart(tttask2));
Ttask2.IsBackground = true;
Ttask2.Start();
}
private delegate void TASK_DELT();
private void task2()
{
if (test1++ % 2 == 0)
{
pictureBox1.BackColor = Color.Blue;
}
else
{
pictureBox1.BackColor = Color.Red;
}
textBox1.Clear();
textBox1.Text = test1.ToString();
textBox1.Refresh();
}
private void tttask2()
{
while (true)
{
Thread.Sleep(250);
//如果是跨线程调用
if (InvokeRequired)
{
this.textBox1.BeginInvoke(new TASK_DELT(task2));
}
else
{
task2();
}
}
}
private void delays(int t)
{
for (int i = 0; i < t; i++)
{
textBox1.Clear();
textBox1.Text = i.ToString();
textBox1.Refresh();
Thread.Sleep(1);
for (int j = 0; j < 0x08f00000; j++) ;
}
pictureBox2.BackColor = Color.Green;
}
private delegate void TASKPRA_DELT(int i);
private void task1()
{
//如果是跨线程调用
if (InvokeRequired)
{
this.textBox1.Invoke(new TASKPRA_DELT(delays), 10);
}
else
{
delays(10);
}
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox2.BackColor = Color.Transparent;
Ttask1 = new Thread(new ThreadStart(task1));
Ttask1.IsBackground = true;
Ttask1.Start();
}
}
}
请问怎么保证两个线程交替执行? 展开
2个回答
展开全部
问题在于,你没有理解Control.Invoke这个方法的机制。
for (int j = 0; j < 0x08f00000; j++) ;
你这里是非常耗时间的。但执行这段代码的线程,不是Ttask1,而是当前的UI线程。
task2方法,也不是由Ttask2调用。也是在当前的UI线程执行。
所以在执行delays时,task2会处于等待状态。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int test1 = 0;
Thread Ttask1 = null;
public Form1()
{
InitializeComponent();
Thread Ttask2 = new Thread(new ThreadStart(tttask2));
Ttask2.IsBackground = true;
Ttask2.Start();
}
private delegate void TASK_DELT();
private void task2()
{
if (test1++ % 2 == 0)
{
Action func1 = delegate {
pictureBox1.BackColor = Color.Blue;
};
this.pictureBox1.Invoke(func1);
}
else
{
Action func2 = delegate {
pictureBox1.BackColor = Color.Red;
};
this.pictureBox1.Invoke(func2);
}
Action func3 = delegate {
textBox1.Clear();
textBox1.Text = test1.ToString();
textBox1.Refresh();
};
this.textBox1.Invoke(func3);
}
private void tttask2()
{
while (true)
{
Thread.Sleep(250);
//如果是跨线程调用
//if (InvokeRequired)
//{
// this.textBox1.BeginInvoke(new TASK_DELT(task2));
//}
//else
{
task2();
}
}
}
private void delays(int t)
{
for (int i = 0; i < t; i++)
{
Action func1 = delegate {
textBox1.Clear();
textBox1.Text = i.ToString();
textBox1.Refresh();
};
this.textBox1.Invoke(func1);
Thread.Sleep(1);
for (int j = 0; j < 0x08f00000; j++) ;
}
Action func2 = delegate {
pictureBox2.BackColor = Color.Green;
};
this.pictureBox2.Invoke(func2);
}
private delegate void TASKPRA_DELT(int i);
private void task1()
{
//如果是跨线程调用
//if (InvokeRequired)
//{
// this.textBox1.Invoke(new TASKPRA_DELT(delays), 10);
//}
//else
{
delays(10);
}
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox2.BackColor = Color.Transparent;
Ttask1 = new Thread(new ThreadStart(task1));
Ttask1.IsBackground = true;
Ttask1.Start();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询