Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何向线程发送文本?_C#_Asp.net_.net - Fatal编程技术网

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;
    }
}