Get the App
SLTechnology News&Howtos  ›  Development  › 

How to start Square with C # multithread

Shulou Source: shulou.com Published: 2022-06-02 09:07:59 10月04日 Update

This article mainly explains "C#multithreading how to start Square", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "C#multithreading how to start Square"!

In practice, you also need to use many of the complex features of C#multithreading. One of the problems is how to pass data from a program to or from a constructor of a thread class. A procedure placed in another thread cannot have parameters passed to it or return values from it, because a procedure passed to a thread constructor cannot have any parameters or return values. To solve this problem, you can encapsulate procedures in a class so that method parameters use fields from the class.

This paper gives a simple example of computing the square of a number. To use this procedure in a new thread, wrap it in a class and start the Square procedure in a new C#multithread.

public class SquareClass { publicdouble Value; public double Square; public void CalcSquare() { Square = Value * Value; } } private void button1_Click(object sender, System.EventArgs e) { SquareClass oSquare =new SquareClass(); t2 = new Thread(new ThreadStart(oSquare.CalcSquare)); oSquare.Value = 30; t2.Start(); }

In the above example, the square value in the class is not checked after the thread starts, because even if the thread's start method is called, there is no guarantee that the method will finish executing immediately. There are several ways to get the desired value from another thread, one of which is to fire an event when the thread completes. The code shown below adds an event declaration to SquareClass.

public delegate void EventHandler(double sq);//Specify delegate type public class SquareClass { publicdouble Value; public double Square; public event EventHandler ThreadComplete;//Specify event object public void CalcSquare() { Square = Value * Value; //Specify event handler ThreadComplete+=new EventHandler(SquareEventHandler); if( ThreadComplete!= null)ThreadComplete(Square);//trigger event} public static void SquareEventHandler(doubleSquare ) //define event handler { MessageBox.Show(Square.ToString ());} }

For this approach, note that the event handler SquareEventHandler runs in the thread t2 that produced the event, not in the thread where the form executes.

At this point, I believe everyone has a deeper understanding of "C#multithreading how to start Square", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Tags: Threads events methods caching procedures programs parameters processing examples content actual that is problems constructors learning encapsulation running complex practical deeper Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Huawei NVidia OPPO Reno MariaDB