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();

}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM