您好,欢迎来到华佗小知识。
搜索
您的当前位置:首页Unity3d 创建线程 子线程与主线程通信

Unity3d 创建线程 子线程与主线程通信

来源:华佗小知识

创建子线程

一,不带参数

Thread   resourcesLoadThread=new Thread (this.resourceLoadTxt);

resourcesLoadThread.Start();

void resourceLoadTxt(){

}

二,带参数;

第一种:使用ParameterizedThreadStart。

调用 System.Threading.Thread.Start(System.Object) 重载方法时将包含数据的对象传递给线程。

Thread   resourcesLoadThread=new Thread (this.resourceLoadTxt);

object  o="wujjjj";

resourcesLoadThread.Start(o);

void resourceLoadTxt(object str){

   // 类型转换

//程序代码

}

线程间通信:

用委托事件,delegate\event

internal  delegate  void ResourceLoadDelegate(string  textStr);

internal  static event  ResourceLoadDelegate resourceLoadDelegate;

 

Thread   resourcesLoadThread;

string  path="/Txt/information";

 

void Start () {

 

resourcesLoadThread=new Thread (this.resourceLoadTxt);

object  o="wujjjj";

resourcesLoadThread.Start(o);

}

 

void resourceLoadTxt(object str){

Debug.Log(str);

 

 

if(resourceLoadDelegate!=null)

  resourceLoadDelegate(str.ToString());

 

resourcesLoadThread.Abort();

}

 

 

转载于:https://www.cnblogs.com/kuangwu/p/3275207.html

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务