Home Backend Development C#.Net Tutorial C# multi-thread parameter passing

C# multi-thread parameter passing

Feb 17, 2017 am 10:51 AM

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;
        }
    }
}
Copy after login

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);
        }
    }
}
Copy after login

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;  
        }
Copy after login


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);
        }
Copy after login

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);
        }
Copy after login

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();
Copy after login

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());
        }
    }
}
Copy after login


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)
        {
            //内部处理省略            
        }
Copy after login

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();          
        }
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

See all articles