Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to start Square with C # multithread

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report