2012-11-21 47 views
1

我有以下代碼:線程與參數失敗

Thread userThread = new Thread(() => UserPasswordSpawn.InputBox(ref userName, ref password)); 
//UserPassWordSpawn.InputBox(ref userName, ref password); 
/* do some calculations while user puts in data */ 

userThread.Join(); 

線程不工作,從來沒有進入的InputBox()函數(我設置一個斷點,而這是從來沒有達到),而它的工作原理如果我執行註釋部分(但是在系統等待用戶輸入數據時應該發生的計算),那就好了。我怎樣才能用2個輸入參數正確地產生一個線程(兩次參考字符串)。

請注意,我得到的錯誤是關於Thread.JoinInternal(),但主要的事情可能是該函數未被調用。

與()=>的ThreadStart()的調用,我從How to pass parameters to ThreadStart method in Thread?

+1

'userThread.Start();':) –

+0

你沒有啓動線程。爲什麼不使用像這樣創建的tpl.threads代價高昂 –

+0

我沒有看到userThread.start()。你是否也可以包含你的代碼的一部分? – ryadavilli

回答

3

得到通過這樣的:

Thread userThread = new Thread(() => UserPasswordSpawn.InputBox(ref userName, ref password)); 

你創建新的thread實例。創建線程,並在加入之前之後

userThread.Start(); 
+0

謝謝,這的確是原因。不知何故,我習慣於C++,在那裏不需要明確。 – SinisterMJ

3

呼叫userThread.Start();
現在你應該通過這樣的方式啓動它。