2013-01-01 22 views
1
class1 st = new class1(); 
string a = addresse.Text; 
System.Threading.Thread th1 = new System.Threading.Thread(new System.Threading.ThreadStart(st.start)); 
th1.Start(); 

,這是一流的我們有什麼我如何發送文本到一個線程?

class class1 
{ 
    public void start(string m) 
    { 
     System.Diagnostics.Process.Start(m); 
    } 
} 

注: 用戶,輸入運行文件 的地址,並希望通過我們把它 GET地址的類來運行的線程文件運行文件

問題是線程不接受來自文本框的地址。

我該怎麼辦?

+0

爲什麼要在新線程中啓動進程?無論如何,這是一種非阻塞方法。 – Rotem

回答

0

將近有

將其更改爲:

public void start(object m) 
{ 
    System.Diagnostics.Process.Start((string) m); 
} 
1

您應該保存文本變量傳遞之前。但爲什麼使它變得複雜?

 ThreadPool.QueueUserWorkItem(delegate 
    { 
     System.Diagnostics.Process.Start(addresse.Text); 
    }); 
2
class1 st = new class1(); 
System.Threading.Thread th1 = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(st.start)); 
th1.Start(textBox1.Text); 

class class1 
{ 
    public void start(object o) 
    { 
     string m = (string)o; 
     System.Diagnostics.Process.Start(m); 
    } 
} 

或者乾脆

new Thread(() => new class1().start(textBox1.Text)).Start(); 
0

如果是您可以使用此代碼文本框:

delegate string GetFilePath(); 

string getFilePath() 
{ 

    if (a.InvokeRequired) 
    { 
     Invoke(new GetFilePath(getFilePath), null); 
    } 
    else 
    { 
     return a.Text; 
    } 
} 

使用委託安全址O ver diffrent線程

相關問題