带有多个参数的ParameterizedThreadStart

时间:2013-11-29 00:03:33

标签: multithreading parameters c++-cli

我有一个由主应用程序表单调用的子表单的代码,它将一些参数传递给它:

public ref class MyForm2 : public System::Windows::Forms::Form
{
public:
        MyForm2(String^ string1, String^ string2, String^ string3, String^ string4)
        {
                InitializeComponent();
                DoSomething(string1, string2, string3, string4);
        }
        //...
        void DoSomething(String^ string1, String^ string2, String^ string3, String^ string4)
        {
                //A lot of stuff in here...
        }
};

但是由于DoSomething()包含其他几个执行长循环并且还更新我的GUI控件(标签和进度条)的函数,我想移动到一个单独的线程,所以我的GUI不会冻结。我正在尝试使用ParameterizedThreadStart执行此操作,但似乎我无法找到传递所有参数的方法。 有任何想法吗?提前谢谢!

编辑 - 感谢evhen14!这是固定的工作代码。

public ref class ThreadParams
{
public:
    String^ mystring1;
    String^ mystring2;
    String^ mystring3;
    String^ mystring4;
    ThreadParams(String^ string1, String^ string2, String^ string3, String^ string4)
    {
        mystring1 = string1;
        mystring2 = string2;
        mystring3 = string3;
        mystring4= string4;
    }
};
public ref class MyForm2 : public System::Windows::Forms::Form
{
public:
    MyForm2(String^ mystring1, String^ mystring2, String^ mystring3, String^ mystring4)
    {
        InitializeComponent();
        ThreadParams^ param = gcnew ThreadParams(mystring1, mystring2, mystring3, mystring4);
        Thread^ newThread = gcnew Thread(gcnew ParameterizedThreadStart(this, &MyForm2::DoSomething));
        newThread->Start(param);
    }
    //...
    void DoSomething(Object^ paramObj)
    {
        ThreadParams^ param = (ThreadParams^)paramObj;
        String^ mystring1 = param->string1;
        String^ mystring2 = param->string2;
        String^ mystring3 = param->string3;
        String^ mystring4 = param->string4;
        //A lot of stuff in here...
    }
};

2 个答案:

答案 0 :(得分:3)

您需要创建一个包含参数的类,并将该类的实例传递给您的方法。因此,您需要重新编写方法以接受新类作为参数

public ref class MyForm2 : public System::Windows::Forms::Form
{
public:
        MyForm2(String^ string1, String^ string2, String^ string3, String^ string4)
        {
                InitializeComponent();
                ThreadParams^ param = gcnew ThreadParams();

                param->string1 = "blah";
                param->string2 = "blah";   //etc
                Thread^ newThread = gcnew Thread(gcnew ParameterizedThreadStart(MyForm2::DoSomething));
                thread->Start(param)

        }
        //...
        void DoSomething(Object^ paramObj)
        {
                ThreadParams param = (ThreadParams)paramObj; 
                //here use param.string1, param.string2, etc
                //A lot of stuff in here...
        }
};

public ref ThreadParams
{
  public:
    String^ string1;
    String^ string2;
    String^ string3;
    String^ string4;
}

PS:无法访问IDE,因此您可能需要修复一些语法,但这个想法应该是明确的

答案 1 :(得分:0)

我不知道为什么我之前没有想过这个,但是另一个快速执行此操作而不创建新类来携带参数的解决方案是创建空的String ^变量,在InitializeComponent()之后立即设置它们的值并调用它们稍后在DoSomething()中,不在线程创建中传递任何参数:

public ref class MyForm2 : public System::Windows::Forms::Form
{
public:
    MyForm2(String^ mystring1, String^ mystring2, String^ mystring3, String^ mystring4)
    {
        InitializeComponent();
        string1 = mystring1;
        string2 = mystring2;
        string3 = mystring3;
        string4 = mystring4;
        Thread^ newThread = gcnew Thread(gcnew ThreadStart(this, &MyForm2::DoSomething));
        newThread->Start();
    }
    //...
    void DoSomething()
    {
        String^ mystring1 = string1;
        String^ mystring2 = string2;
        String^ mystring3 = string3;
        String^ mystring4 = string4;
        //A lot of stuff in here...
    }
private:
    String^ string1;
    String^ string2;
    String^ string3;
    String^ string4;
};