博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#中在工作线程创建窗体并操作
阅读量:4302 次
发布时间:2019-05-27

本文共 3684 字,大约阅读时间需要 12 分钟。

实例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线程不阻塞
        }
    }

转载地址:http://nrows.baihongyu.com/

你可能感兴趣的文章
laravel 定时任务秒级执行
查看>>
浅析 Laravel 官方文档推荐的 Nginx 配置
查看>>
Swagger在Laravel项目中的使用
查看>>
Laravel 的生命周期
查看>>
CentOS Docker 安装
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
Mysql出现Table 'performance_schema.session_status' doesn't exist
查看>>
MySQL innert join、left join、right join等理解
查看>>
vivado模块封装ip/edf
查看>>
sdc时序约束
查看>>
Xilinx Jtag Access/svf文件/BSCANE2
查看>>
NoC片上网络
查看>>
开源SoC整理
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
已知子网掩码,确定ip地址范围
查看>>
判断时间或者数字是否连续
查看>>
docker-daemon.json各配置详解
查看>>