當創建一個新線程時,使用ThreadStart()
如何將多個參數傳遞給該函數?C++/CLR在線程中傳遞多個參數
下面是一個例子:
using namespace System;
using namespace System::Threading;
public ref class Animal
{
public:
void Hungry(Object^ food, int quantity);
};
void Animal::Hungry(Object^ food, int quantity)
{
Console::WriteLine("The animal eats " + quantity.ToString() + food);
}
void main()
{
Animal^ test = gcnew Animal;
Thread^ threads = gcnew Thread(gcnew ParameterizedThreadStart(test, &Animal::Hungry));
threads->Start("Grass", 1); //need to pass 2nd argument!
}
它工作正常,只有一個參數(如果我刪除INT數量,只是有對象^食品),因爲ParameterizedThreadStart只需要一個對象^
你傳遞參數給Thread :: Start()方法。您調用的函數必須採用單個Object ^參數。用safe_cast將它轉換回String ^。 – 2015-04-05 17:57:40
@HansPassant好吧,我可以像你說的那樣讓它工作,但是我怎麼傳遞多個參數呢?此外,我不得不將ThreadStart()更改爲ParameterizedThreadStart() – Joesph 2015-04-05 18:25:14
我已更新問題 – Joesph 2015-04-05 18:34:28