新线程启动新form,在主线程中,修改新form的控件值,引起主界面卡死,如何解决~
主界面代码://主界面程序
private void ShowProgress()
{
PB = new ProgressBar();
PB.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
int val = 1;
ProgressThread = new Thread(new ThreadStart(ShowProgress));
ProgressThread.IsBackground = true;
ProgressThread.Start();
do
{
val += 1;
Thread.Sleep(100);
}
while (PB.SetInvokePGB(val));
}
}
//进度条界面程序
public delegate void InvokeControl(int str);
public partial class ProgressBar : Form
{
public ProgressBar()
{
InitializeComponent();
}
public bool SetInvokePGB(int val)
{
if (progressBar1.InvokeRequired)
{
if (val < progressBar1.Maximum)
{
BeginInvoke(new InvokeControl(SetPGB), val);
return true;
}
else
{
BeginInvoke(new InvokeControl(Close), val);
return false;
}
}
else
return false;
}
public void SetPGB(int val)
{
progressBar1.Value = val;
}
public void Close(int val)
{
this.Close();
}
}
运行过程中,为什么主界面会卡死,请问如何处理? --------------------编程问答-------------------- private void ShowProgress()
{
PB.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
ProgressThread = new Thread(new ThreadStart(ShowProgress));
ProgressThread.IsBackground = true;
PB = new ProgressBar();
ProgressThread.Start();
Thread TD = new Thread(new ThreadStart(SS));
TD.Start();
}
public void SS()
{
int val = 1;
do
{
val += 1;
Thread.Sleep(100);
}
while (PB.SetInvokePGB(val));
} --------------------编程问答-------------------- 应该是线程间上下文切换引起的
补充:.NET技术 , C#