Process thread Ctrip

process

A process is a movement activity of a program in a computer on a certain data set. It is the basic unit of resource allocation and scheduling in 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
p.Kill();

// 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")) {
        // It is possible to operate on a process based on its name 
        Console.WriteLine(name);
    } 
}  

Debug mode: The running program is to find an .exe file in the Debug folder under the bin folder under the folder where the script is located. This is the startup item of the program. But it cannot be released for others to use.

Release mode: run the program first, find an .exe file in 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

A thread is a single sequential control flow in a program, a relatively independent and schedulable execution unit within a process, a basic unit for the system to independently schedule and allocate CPU s, and a scheduling unit for running programs.

A program has at least one process, and a process has at least one thread (main thread)
Threads are really working, not processes

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 open
            thread1.Start();
            thread2.Start();
            // thread close
            thread1.Abort();
        }
    }

    class Shop {
        public static int Number;

        public Shop() {
            Number = 1;
        }

        // sell
        public void Sell() {
            // thread lock
            lock (this)
                if (Number > 0) {
                    // Delay execution, milliseconds
                    Thread.Sleep(1000);
                    Console.WriteLine("The sale is successful, the remaining quantity is" + --Number);
                }
        }
    }
}

If multiple threads execute different tasks, they can process tasks in parallel and greatly speed up the progress of the tasks, but if multiple threads execute a task, they will compete for the same task, and an error may be reported at this time. A lock can be introduced here to prevent multiple threads from grabbing a resource.
Although lock plays a security role when the most threads call a task, but the previous thread is not completed, other threads will idle in place, and the performance will be greatly reduced. At this time, the concept of coroutine is introduced.

coroutine

The concept of multi-threading represented by a single thread is a coroutine.
A frame in Unity is still relatively abundant compared to the CPU time. In the time Unity executes one frame, it is actually executed (main thread, thread one, thread two), but Unity is still single-threaded, so it will not There is a problem of resource grabbing in multithreading.

void Start() {
    // start coroutine
    StartCoroutine(enumerator());
    // close coroutine
    StopCoroutine(co);
}

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

If there is an infinite loop in the coroutine, the main thread is not affected.
Coroutines: mainly deal with delay logic.
If loading resources are placed in the main thread, the program cannot do other things when downloading resources, so resources such as resource loading can be placed in Ctrip.
Download web images

public string url;
public Image image;
void Start()
{
    // Open Ctrip, because there is no fixed time for image loading
    StartCoroutine(DownImage(url));
}

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

Posted by mdomel on Sun, 06 Nov 2022 14:17:27 +0300