日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

c#中在工作线程创建窗体并操作

發布時間:2023/12/18 C# 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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#中在工作线程创建窗体并操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。