Unity - process / thread / coprocess

A process is a movement of a program on a data set in a computer. It is the basic unit of resource allocation and scheduling of the system and the basis of the operating system structure.

// Open the specified program
Process p = Process.Start(@"C:\Program Files\Google\Chrome\Application\chrome.exe");
// Kill Ctrip

// Get all currently running processes
            Process[] list = Process.GetProcesses();            
for (int i = 0; i < list.Length; i++) {
    var name = list[i].ProcessName;
    if (name.Contains("QQ")) {
        // You can operate on a process by its name 

Debug mode: the running program is to find a. exe file under the debug folder under the bin folder under the folder where the script is located. This is the startup item of this program. But it cannot be released to others.

Release mode: run the program first, and find a. exe file under the release folder under the bin folder under the running script. This is the packaged program. You can send this to a friend to run the test.

Thread is a single sequential control process in a program. It is a relatively independent and schedulable execution unit in the process. It is the basic unit for the system to independently schedule and allocate CPU. It is the scheduling unit of the program running.

There is at least one process in a program and at least one thread (main thread) in a process
What really works is a thread, not a process

using System;
using System.Threading;

namespace BigTalk {

    class Program {
        static void Main(string[] args) {

            Shop shop = new Shop();
            Thread thread1 = new Thread(shop.Sell);
            Thread thread2 = new Thread(shop.Sell);
            // Thread on
            // Thread shutdown

    class Shop {
        public static int Number;

        public Shop() {
            Number = 1;

        // Sell
        public void Sell() {
            // Line program lock
            lock (this)
                if (Number > 0) {
                    // Delayed execution, MS
                    Console.WriteLine("Sold successfully, the remaining quantity is" + --Number);

When multithreads execute different tasks, they can process the tasks in parallel, greatly speeding up the progress of the task. However, when multithreads execute a task, they will compete for the same task, which may generate an error. Here, lock can be introduced to prevent multiple threads from seizing a resource.
lock: when the most threads call a task, although it plays a safety role, if the previous thread is not completed, the other threads will idle in place and the performance will be greatly reduced. At this time, the concept of collaboration is introduced

Synergetic process
Using a single thread to represent the concept of multithreading is called a coprocess.
Compared with the CPU, a frame in Unity has a relatively abundant time. During the execution of a frame in Unity, it is actually executed (main thread, thread 1 and thread 2). However, Unity is still a single thread, so there is no problem of resource grabbing in multiple threads.

void Start() {
    // Start collaboration
    // Close collaboration

// Synergetic process
IEnumerator enumerator() {
    // Delay execution by one second
    yield return new WaitForSeconds(1);
    // Delay by one frame
    yield return new WaitForEndOfFrame();
    // Delay by one frame
    yield return null;
    // Delay by one frame
    yield return 0;
    // Delay by one frame
    yield return 100;

If a dead loop occurs in a coroutine, the main thread is not affected.
Coroutine: mainly deals with delay logic.
If the loaded resources are placed in the main thread, the program cannot do other things when downloading the resources. Therefore, resources can be loaded into Ctrip
Download Network pictures

public string url;
public Image image;
void Start()
    // Start Ctrip because there is no fixed time for loading pictures

IEnumerator DownImage(string url) {
    // Load the displayed pictures through WWW
    WWW www = new WWW(url);
    // Wait for the picture to load
    yield return www;
    // Create corresponding bounding box
    Rect rect = new Rect(Vector2.zero, new Vector2(www.texture.width, www.texture.height));
    // Convert the previously loaded image format into sprite mode
    Sprite sprite = Sprite.Create(www.texture, rect, Vector3.zero);
    // Send the converted sprite mode picture to Image
    image.sprite = sprite;
    // Set the Image size to the original Image size

Posted by chings on Wed, 17 Aug 2022 13:39:38 +0930