C#多线程与Unity中的使用
2015-06-26 17:42
381 查看
C#的多线程是一个非常有用的功能,测试如下:
(自动加i)
using UnityEngine;
using System.Collections;
using System.Threading;
public class Test : MonoBehaviour {
Thread myThread;//定义的线程
bool _myBool = true;//控制执行线程的变量
// Use this for initialization
void Start () {
this.myThread = new Thread(new ThreadStart(myStartingMethod));
myThread.Start();
}
// Update is called once per frame
void 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()方法并没有什么用,线程还是停不下来。最后使用上面的全局变量,轻松停下来了
(自动加i)
using UnityEngine;
using System.Collections;
using System.Threading;
public class Test : MonoBehaviour {
Thread myThread;//定义的线程
bool _myBool = true;//控制执行线程的变量
// Use this for initialization
void Start () {
this.myThread = new Thread(new ThreadStart(myStartingMethod));
myThread.Start();
}
// Update is called once per frame
void 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()方法并没有什么用,线程还是停不下来。最后使用上面的全局变量,轻松停下来了
相关文章推荐
- java-模拟tomcat服务器
- c#调用COM组件
- C#实现把指定数据写入串口
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#通用邮件发送类分享
- C#中this的用法集锦
- C#.NET获取拨号连接的宽带连接方法
- C#中Equality和Identity浅析
- C#生成饼形图及添加文字说明实例代码
- C#判等对象是否相等的方法汇总
- C#简单的向量用法实例教程
- C#托管堆对象实例包含内容分析
- C#实现按照指定长度在数字前补0方法小结
- C#虚方法的声明和使用实例教程
- C#获取文件夹及文件的大小与占用空间的方法
- C#定义简单的反射工厂实例分析
- C#数字图象处理之肤色检测的方法