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線程不阻塞
? ? ? ? }
? ? }
總結
以上是生活随笔為你收集整理的c#中在工作线程创建窗体并操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何和何时使用 CSS 的权重设置 !i
- 下一篇: 集合拷贝通用方法、list<A> 转换成