如何将文本发送到线程?

时间:2013-01-01 17:33:31

标签: 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();

这是我们所拥有的课程

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

请注意: user,输入正在运行的文件的地址 并希望通过我们为其提供的类来运行带有线程的文件 获取地址并运行文件

问题是该主题不接受文本框中的地址。

我该怎么办?

4 个答案:

答案 0 :(得分: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();

答案 1 :(得分:1)

您应该在传递文本之前将文本保存到变量。但为什么要让它复杂化呢?

     ThreadPool.QueueUserWorkItem(delegate
     {
        System.Diagnostics.Process.Start(addresse.Text);
     });

答案 2 :(得分:0)

几乎就在那里

将其更改为:

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

答案 3 :(得分:0)

如果a是文本框,则可以使用此代码:

delegate string GetFilePath();

string getFilePath()
{

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

使用委托来安全访问不同的线程