1. 程式人生 > 實用技巧 >C# 執行緒中操作窗體控制元件

C# 執行緒中操作窗體控制元件

如果需要檢視更多文章,請微信搜尋公眾號csharp程式設計大全,需要進C#交流群群請加微信z438679770,備註進群, 我邀請你進群! ! !

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


    }
}

  

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

        }


        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.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.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程式碼

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

        }
      
    }
}