C#多线程与Unity中的使用

来源:互联网 发布:php call 编辑:程序博客网 时间:2024/06/07 09:50

C#的多线程是一个非常有用的功能,测试如下:

(自动加i)

using UnityEngine;using System.Collections;using System.Threading;public class Test : MonoBehaviour {    Thread myThread;//定义的线程    bool _myBool = true;//控制执行线程的变量// Use this for initializationvoid Start () {        this.myThread = new Thread(new ThreadStart(myStartingMethod));        myThread.Start();}// Update is called once per framevoid Update () {        if (Input.GetKeyDown(KeyCode.X))        {            _myBool = false;//如果按X,停止线程执行        }}    void myStartingMethod()    {        int i = 0;        while (_myBool)        {             i++;             print(i);            if (i > 50000)            {                _myBool = false;            }        }    }}

在这次测试中,对线程使用Above()方法并没有什么用,线程还是停不下来。最后使用上面的全局变量,轻松停下来了

0 0
原创粉丝点击