C# 如何向线程发送文本?
这就是我们的课堂C# 如何向线程发送文本?,c#,asp.net,.net,C#,Asp.net,.net,这就是我们的课堂 class1 st = new class1(); string a = addresse.Text; System.Threading.Thread th1 = new System.Threading.Thread(new System.Threading.ThreadStart(st.start)); th1.Start(); 注: 用户,输入运行文件的地址 并希望通过我们为其设置的类运行带有线程的文件 获取地址并运行文件 问题是线程不接受文本框中的地址 我该怎么办?快
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);
}
}
在传递变量之前,应将文本保存到变量。但为什么要把它复杂化呢
public void start(object m)
{
System.Diagnostics.Process.Start((string) m);
}
或者干脆
class class1
{
public void start(object o)
{
string m = (string)o;
System.Diagnostics.Process.Start(m);
}
}
如果是文本框,则可以使用以下代码:
new Thread(() => new class1().start(textBox1.Text)).Start();
使用委托安全访问不同的线程为什么要在新线程中启动进程?无论如何,这是一种非阻塞方法。
class class1
{
public void start(object o)
{
string m = (string)o;
System.Diagnostics.Process.Start(m);
}
}
new Thread(() => new class1().start(textBox1.Text)).Start();
delegate string GetFilePath();
string getFilePath()
{
if (a.InvokeRequired)
{
Invoke(new GetFilePath(getFilePath), null);
}
else
{
return a.Text;
}
}