Home  >  Article  >  Backend Development  >  C# multi-thread parameter passing

C# multi-thread parameter passing

黄舟
黄舟Original
2017-02-17 10:51:301721browse

1. Pass through the entity class (you can pass multiple parameters and get the return value), the demo is as follows:

Functions that need to be called in the thread:

namespace ThreadParameterDemo
{
    public class FunctionClass
    {
        public static string TestFunction(string name, int age)
        {
            //内部处理省略
            return name + " 的年龄是:" + age;
        }
    }
}

Encapsulated through entities:

namespace ThreadParameterDemo
{
    /// <summary>
    /// 过渡类
    /// </summary>
    public class TransitionalClass
    {
        private string name = string.Empty;
        private int age;
        public string acceptResults = string.Empty;
        public TransitionalClass(string name, int age)
        {
            this.name = name;
            this.age = age;
        }

        public void TestFunction()
        {
            acceptResults = FunctionClass.TestFunction(this.name, this.age);
        }
    }
}

Call:

  private void Form1_Load(object sender, EventArgs e)
        {
            //实例化ThreadWithState类,为线程提供参数  
            TransitionalClass tc = new TransitionalClass(" Jack", 42);
            // 创建执行任务的线程,并执行  
            Thread t = new Thread(new ThreadStart(tc.TestFunction));
            t.Start();
            //获取返回值,通过 tc.acceptResults;  
        }


Small note:

You must pay attention to the issue of IsBackground. If IsBackground is false, the Windows program will not automatically exit the thread for you when it exits. That is, your application is not actually ended.

MSDN recommendation: The best way to provide parameters for multi-threaded method calls is to wrap the target method in a class and assign it to the class Define fields that will be used as parameters for the new thread.

The advantage of this method is that any time you want to start a new thread, you can create a new instance of the class with its own parameters.

## BackgroundWorker class

The functions in ThreadStart have no return value and parameters

2. Parameters and return values ​​in asynchronous calls
The perfect solution to parameters and return values ​​is to use asynchronous calls. Compared with Thread, one of the biggest disadvantages of asynchronous calls is that they cannot control their priority.

The specific code is as follows:

##

        public delegate string delegateFunction(string name,int age);//委托
        delegateFunction df;
        private void Form1_Load(object sender, EventArgs e)
        {
            //指向需要调用的方法
            df = new delegateFunction(FunctionClass.TestFunction);
            string name = "my name";//输入参数 
            int age = 19;
            IAsyncResult result = df.BeginInvoke(name,age, null, null);
            string myResult = df.EndInvoke(result);//用于接收返回值 
            MessageBox.Show(myResult);
        }

Simplified:

 public Func<string, int, string>  df;//委托
        private void Form1_Load(object sender, EventArgs e)
        {
            //指向需要调用的方法
            df += FunctionClass.TestFunction;
            string name = "my name";//输入参数 
            int age = 19;
            IAsyncResult result = df.BeginInvoke(name, age, null, null);
            string myResult = df.EndInvoke(result);//用于接收返回值 
            MessageBox.Show(myResult);
        }

Small note:

Generating a new thread in this way is Running in the background (background), the priority is normal


##3. Use BackgroundWorker

The simplest way to return a value from multiple threads is to use the BackgroundWorker component to manage threads, raise an event when the task is completed, and then use an event handler to handle the result.


Small Note:

BackgroundWorker component is used to perform time-consuming asynchronous operations such as database transactions and file downloads.
Add a BackgroundWorker instance to the application. If you are using VS, you can drag it directly from the tool to the application:

##
BackgroundWorker backgroundWorker1 = new BackgroundWorker();

In order to start operating in the background, the RunWorkerAsync() method of BackgroundWorker must be called. When this method is called, BackgroundWorker starts performing background operations by triggering the DoWork event. The code of the DoWork event is executed in another thread. When the background operation is completed, whether it is completed or canceled, the RunWorkerCompleted event is triggered. Through this method, the completion result of the background operation can be fed back to the user. In addition, use the Cancelled property of the RunWorkerCompletedEventArgs instance to determine whether the background operation is terminated by the Cancel operation.



The specific demo is as follows:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //TransitionalClass tc = new TransitionalClass("xiaoming", 10);
            //ThreadPool.QueueUserWorkItem(new WaitCallback(TransitionalClass.TestFunction), tc);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.TestArea2();
        }

        private System.ComponentModel.BackgroundWorker BackgroundWorker1
    = new System.ComponentModel.BackgroundWorker();

        private void TestArea2()
        {
            InitializeBackgroundWorker();

            AreaClass2 AreaObject2 = new AreaClass2();
            AreaObject2.Base = 30;
            AreaObject2.Height = 40;

            // Start the asynchronous operation.
            BackgroundWorker1.RunWorkerAsync(AreaObject2);
        }

        private void InitializeBackgroundWorker()
        {
            // Attach event handlers to the BackgroundWorker object.
            BackgroundWorker1.DoWork +=
                new System.ComponentModel.DoWorkEventHandler(BackgroundWorker1_DoWork);
            BackgroundWorker1.RunWorkerCompleted +=
                new System.ComponentModel.RunWorkerCompletedEventHandler(BackgroundWorker1_RunWorkerCompleted);
        }

        private void BackgroundWorker1_DoWork(
            object sender,
            System.ComponentModel.DoWorkEventArgs e)
        {
          //在执行DoWork 事件时,DoWorkEventArgs 实例的Result 属性,返回值到用户;在RunWorkerCompleted 事件里,RunWorkerCompletedEventArgs 实例的Result 属性接收值;
            AreaClass2 AreaObject2 = (AreaClass2)e.Argument;
            // Return the value through the Result property.
            e.Result = AreaObject2.CalcArea();
        }

        private void BackgroundWorker1_RunWorkerCompleted(
            object sender,
            System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            // Access the result through the Result property. 
            double Area = (double)e.Result;
            MessageBox.Show("The area is: " + Area.ToString());
        }
    }
}


The demo code comes from MSDN:

Click to open the link



Reference article:
Click to open the link

4. If Instead of returning a value, how should we write it elegantly? Anonymous function

C# multi-thread parameter passingFunctionClass class is added, the test function is as follows:

   public static void TestFunction2(string name, int age)
        {
            //内部处理省略            
        }

The call is as follows:

 private void Form1_Load(object sender, EventArgs e)
        {
            Thread t1 = new Thread(new ThreadStart(delegate
            {
                FunctionClass.TestFunction2("eee", 5);
            }));
            t1.Start();          
        }

Small note:

If called through WCF, the thread starting function should be placed on the server. If placed on the client, it is easy to cause the main program to crash inexplicably due to the time limit of the WCF client.

The main reason for the crash is that the client wcf response time is limited.

The above is the content of C# multi-thread parameter passing. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn