前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C# 线程中操作窗体控件

C# 线程中操作窗体控件

作者头像
zls365
发布2020-08-19 11:03:34
1.1K0
发布2020-08-19 11:03:34
举报
文章被收录于专栏:CSharp编程大全

1. 在线程中给textBox1

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Threading;

namespace WindowsFormsApp12
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

           // CheckForIllegalCrossThreadCalls = false;
           // Class1 cs1 = new Class1();
            Thread th = new Thread(test1);
            th.Start();
            // cs1.ShowEvent = test;

        }

        public void test1()
        {

           // ShowlbDevTem("123");
            textBox1.Text = "123";

        }


        private void button1_Click(object sender, EventArgs e)
        {

           // ShowlbDevTem("112");

        }

        /*   public void test(string str)
          {

              ShowlbDevTem(str);
          }
          
          public delegate void SWTDelegate(string AddStr);
           //public delegate void ComsumerTextDelegate(int Index, string AddStr);
           public  void ShowlbDevTem(string AddStr)
           {
               if (textBox1.InvokeRequired)
               {
                   SWTDelegate pd = new SWTDelegate(ShowlbDevTem);
                   textBox1.Invoke(pd, new object[] { AddStr });
               }
               else
               {
                   textBox1.Text = AddStr;
                   //richTextBox1.AppendText(AddStr);
               }
           }

           public void dataShow(int row, int column, string str)
           {
               row = row % 30;
               ShowMessage(dataGridViewX1, str, row, column);

           }


          delegate void ShowMessageDelegate(DataGridView dg, string message, int row, int column);
          private void ShowMessage(DataGridView dg, string message, int row, int column)
          {
              if (dg.InvokeRequired)
              {
                  ShowMessageDelegate showMessageDelegate = ShowMessage;
                  dg.Invoke(showMessageDelegate, new object[] { dg, message, row, column });
              }
              else
              {

                  dg.Rows[row].Cells[column].Value = message;
              }
          }
          */


    }
}

运行结果:

线程中是不允许直接操作窗体空间的

怎么解决呢?

方法1: CheckForIllegalCrossThreadCalls = false;

运行ok

方法2:委托实现:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Threading;

namespace WindowsFormsApp12
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

           //CheckForIllegalCrossThreadCalls = false;
           // Class1 cs1 = new Class1();
            Thread th = new Thread(test1);
            th.Start();
            // cs1.ShowEvent = test;

        }

        public void test1()
        {

            ShowlbDevTem("123");
            //textBox1.Text = "123";

        }


        private void button1_Click(object sender, EventArgs e)
        {

           // ShowlbDevTem("112");

        }

        /*  public void test(string str)
          {

              ShowlbDevTem(str);
          }*/
          
          public delegate void SWTDelegate(string AddStr);
           //public delegate void ComsumerTextDelegate(int Index, string AddStr);
           public  void ShowlbDevTem(string AddStr)
           {
               if (textBox1.InvokeRequired)
               {
                   SWTDelegate pd = new SWTDelegate(ShowlbDevTem);
                   textBox1.Invoke(pd, new object[] { AddStr });
               }
               else
               {
                   textBox1.Text = AddStr;
                   //richTextBox1.AppendText(AddStr);
               }
           }

         /*  public void dataShow(int row, int column, string str)
           {
               row = row % 30;
               ShowMessage(dataGridViewX1, str, row, column);

           }


          delegate void ShowMessageDelegate(DataGridView dg, string message, int row, int column);
          private void ShowMessage(DataGridView dg, string message, int row, int column)
          {
              if (dg.InvokeRequired)
              {
                  ShowMessageDelegate showMessageDelegate = ShowMessage;
                  dg.Invoke(showMessageDelegate, new object[] { dg, message, row, column });
              }
              else
              {

                  dg.Rows[row].Cells[column].Value = message;
              }
          }
          */


    }
}

然后我想在类中对空间进行操作,这样还可以么?

试试看

主窗体代码:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Threading;

namespace WindowsFormsApp12
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

           //CheckForIllegalCrossThreadCalls = false;
            Class1 cs1 = new Class1();
            Thread th = new Thread(cs1.test);
            th.Start();
            // cs1.ShowEvent = test;

        }

        public void test1()
        {

            ShowlbDevTem("123");
            //textBox1.Text = "123";

        }


        private void button1_Click(object sender, EventArgs e)
        {

           // ShowlbDevTem("112");

        }

        /*  public void test(string str)
          {

              ShowlbDevTem(str);
          }*/
          
          public delegate void SWTDelegate(string AddStr);
           //public delegate void ComsumerTextDelegate(int Index, string AddStr);
           public  void ShowlbDevTem(string AddStr)
           {
               if (textBox1.InvokeRequired)
               {
                   SWTDelegate pd = new SWTDelegate(ShowlbDevTem);
                   textBox1.Invoke(pd, new object[] { AddStr });
               }
               else
               {
                   textBox1.Text = AddStr;
                   //richTextBox1.AppendText(AddStr);
               }
           }

         /*  public void dataShow(int row, int column, string str)
           {
               row = row % 30;
               ShowMessage(dataGridViewX1, str, row, column);

           }


          delegate void ShowMessageDelegate(DataGridView dg, string message, int row, int column);
          private void ShowMessage(DataGridView dg, string message, int row, int column)
          {
              if (dg.InvokeRequired)
              {
                  ShowMessageDelegate showMessageDelegate = ShowMessage;
                  dg.Invoke(showMessageDelegate, new object[] { dg, message, row, column });
              }
              else
              {

                  dg.Rows[row].Cells[column].Value = message;
              }
          }
          */


    }
}

class1中代码:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApp12
{
    class Class1
    {
        //public Action<string> ShowEvent;
        public void test()
        {

           //ShowEvent("123456789");

            Form1 f1 = new Form1();
            f1.ShowlbDevTem("112");


        }
        

        
    }
}

运行结果:

哎呦,卧槽

运行不报错 但是文本框中是空的,怎么办?

主窗体:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Threading;

namespace WindowsFormsApp12
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

           //CheckForIllegalCrossThreadCalls = false;
            Class1 cs1 = new Class1();
            Thread th = new Thread(cs1.test);
            th.Start();
             cs1.ShowEvent = test;

        }

        public void test1()
        {

            ShowlbDevTem("123");
            //textBox1.Text = "123";

        }


        private void button1_Click(object sender, EventArgs e)
        {

           // ShowlbDevTem("112");

        }

         public void test(string str)
          {

              ShowlbDevTem(str);
          }
          
          public delegate void SWTDelegate(string AddStr);
           //public delegate void ComsumerTextDelegate(int Index, string AddStr);
           public  void ShowlbDevTem(string AddStr)
           {
               if (textBox1.InvokeRequired)
               {
                   SWTDelegate pd = new SWTDelegate(ShowlbDevTem);
                   textBox1.Invoke(pd, new object[] { AddStr });
               }
               else
               {
                   textBox1.Text = AddStr;
                   //richTextBox1.AppendText(AddStr);
               }
           }

         /*  public void dataShow(int row, int column, string str)
           {
               row = row % 30;
               ShowMessage(dataGridViewX1, str, row, column);

           }


          delegate void ShowMessageDelegate(DataGridView dg, string message, int row, int column);
          private void ShowMessage(DataGridView dg, string message, int row, int column)
          {
              if (dg.InvokeRequired)
              {
                  ShowMessageDelegate showMessageDelegate = ShowMessage;
                  dg.Invoke(showMessageDelegate, new object[] { dg, message, row, column });
              }
              else
              {

                  dg.Rows[row].Cells[column].Value = message;
              }
          }
          */


    }
}

类中:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApp12
{
    class Class1
    {
        public Action<string> ShowEvent;
        public void test()
        {

           ShowEvent("123456789");

           // Form1 f1 = new Form1();
            //f1.ShowlbDevTem("112");


        }
        

        
    }
}

总算可以了

但是我有两个类,线程在class1,委托在class2回调呢?

主窗体代码不变

class1代码:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApp12
{
    class Class1
    {
        public Action<string> ShowEvent;
        public void test()
        {


            // Class2 cs2 = new Class2();
            // cs2.test(ShowEvent);
            Class2 cs2 = new Class2();
           cs2.test(ShowEvent);

        }




    }
}

class2代码

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Threading;

namespace WindowsFormsApp12
{
    class Class2
    {
       // public Action<string> ShowEvent1;
        public void test(Action<string> ShowEvent)
        {


            ShowEvent("123456789");

        }
      
    }
}

完!!!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-10-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 CSharp编程大全 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档