c#中在工作线程创建窗体并操作
作者:互联网
实例1
public void CycleShow()//循环绘图
{
Task.Factory.StartNew(() =>
{
// threadB = new Thread(
//() => {
AllDealWithSpectrumDicSingle.Clear();
Form form = new MyForm();
form.ShowInTaskbar = true;
form.FormBorderStyle = FormBorderStyle.Sizable;
form.StartPosition = FormStartPosition.Manual;
form.Location = new Point(MP_MappingChildFormPosition.SingleSpectrumPosition_X, MP_MappingChildFormPosition.SingleSpectrumPosition_Y);
Pb_Single = new PictureBox();
form.BringToFront();
form.Width = MP_MappingChildFormPosition.SingleSpectrumPosition_Width;
form.Height = MP_MappingChildFormPosition.SingleSpectrumPosition_Height;
// form.ControlBox = false;
form.Controls.Add(Pb_Single);
Pb_Single.Dock = DockStyle.Fill;
Pb_Single.Visible = true;
form.Visible = true;
//form.Show();
while (!MP_LogicManager.StopExit)
{
var va = new KeyValuePair<Point, SpectrumBaseInManager>();
lock (memotryLockSingle)
{
if (AllDealWithSpectrumDicSingle.Count <= 0) continue;
va = AllDealWithSpectrumDicSingle.First();
}
Application.DoEvents();
DrawSingleSpec(va);//画单张谱图
//Thread.Sleep(1);
form.TopMost = true;
form.BringToFront();
}
form.Close();
//}
//);
// threadB.IsBackground = true;
// threadB.Start();
});
// threadA = new Thread(
//() => {
Task.Factory.StartNew(() =>
{
AllDealWithSpectrumDicMulti.Clear();
Form form2 = new MyForm();
form2.ShowInTaskbar = true;
form2.FormBorderStyle = FormBorderStyle.Sizable;
form2.Location = new System.Drawing.Point(MP_MappingChildFormPosition.MultiSpectrumPosition_X, MP_MappingChildFormPosition.MultiSpectrumPosition_Y);
Pb_Multi = new PictureBox();
form2.BringToFront();
form2.Width = MP_MappingChildFormPosition.MultiSpectrumPosition_Width;
form2.Height = MP_MappingChildFormPosition.MultiSpectrumPosition_Height;
// form2.ControlBox = false;
form2.Controls.Add(Pb_Multi);
Pb_Multi.Dock = DockStyle.Fill;
Pb_Multi.Visible = true;
form2.StartPosition = FormStartPosition.Manual;
form2.Visible = true;
form2.Show();
int count = 0;
AllDealWithSpectrumDicMulti.Clear();
while (!MP_LogicManager.StopExit)
{
var va = new KeyValuePair<Point, SpectrumBaseInManager>();
Dictionary<Point, SpectrumBaseInManager> AllDealWithSpectrumDic_Draw=new Dictionary<Point, SpectrumBaseInManager>();
lock (memotryLockMulti)
{
if (AllDealWithSpectrumDicMulti.Count <= 0) continue;
va = AllDealWithSpectrumDicMulti.Last();
if(AllDealWithSpectrumDicMulti.Count>2)
{
for(int i=0;i<2;i++)
{
var va2 = AllDealWithSpectrumDicMulti.ElementAt(AllDealWithSpectrumDicMulti.Count - 1 - i);
AllDealWithSpectrumDic_Draw.Add(va2.Key,va2.Value);
}
}
else
{
for(int i=0;i< AllDealWithSpectrumDicMulti.Count;i++)
{
var va2 = AllDealWithSpectrumDicMulti.ElementAt(i);
AllDealWithSpectrumDic_Draw.Add(va2.Key, va2.Value);
}
}
}
Application.DoEvents();
DrawMultiSpec(va, AllDealWithSpectrumDic_Draw);//画多张谱图
count++;
//if (count > 2)
//{
// form2.Close();
// return;
//}
//Thread.Sleep(500);
form2.TopMost = true;
form2.BringToFront();
}
form2.Close();
//}
//);
// threadA.IsBackground = true;
// threadA.Start();
});
}
实例2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Task.Factory.StartNew(() => {
Form form = new Form();
form.StartPosition = FormStartPosition.CenterScreen;
form.Width = 500;
form.Height = 500;
Button button = new Button();
form.Controls.Add(button);
button.Location = new Point(0, 0);
button.Width = 50;
button.Height = 20;
button.Click += new EventHandler(button_Test_Click);
form.ShowDialog();
//或
//Application.Run(form)
// Application.DoEvents();唤醒窗体
//form.Show();
});
}
private void button_Test_Click(object sender, EventArgs e)
{
Thread.Sleep(10000);//创建此button的工作线程阻塞,但是其他的,例如主ui线程不阻塞
}
}
标签:线程,form,c#,button,form2,窗体,MP,new,true 来源: https://blog.csdn.net/u011555996/article/details/103936297