8288分类目录 8288分类目录 8288分类目录
  当前位置:海洋目录网 » 站长资讯 » 教育考试 » 文章详细 订阅RssFeed

C# 线程中操作窗体控件

来源:本站原创 浏览:75次 时间:2022-12-09

1. 在线程中给textBox1

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:委托实现:

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;              }          }          */

   }}

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

试试看

主窗体代码:

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中代码:

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");

       }
   }}

运行结果:

哎呦,卧槽

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

主窗体:

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";
       }
  • 上一篇: C#多线程委托ParameterizedThreadStart应用
  • 下一篇: C# 使用File.Create方法创建文件时,报进程被占用
  •   推荐站点

    • At-lib分类目录At-lib分类目录

      At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

      www.at-lib.cn
    • 中国链接目录中国链接目录

      中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

      www.cnlink.org
    • 35目录网35目录网

      35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

      www.35mulu.com
    • 就要爱网站目录就要爱网站目录

      就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

      www.912219.com
    • 伍佰目录伍佰目录

      伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

      www.wbwb.net