using namespace System;
using namespace System::Threading;
namespace SystemThreadingExample
{
public ref class Work
{
public:
void StartThreads()
{
// To start a thread using a shared thread procedure, use
// the class name and method name when you create the
// ParameterizedThreadStart delegate.
// AddressOf Work.DoWork)
//
Thread^ newThread = gcnew
Thread(gcnew ParameterizedThreadStart(Work::DoWork));
// Use the overload of the Start method that has a
// parameter of type Object. You can create an object that
// contains several pieces of data, or you can pass any
// reference type or value type. The following code passes
// the integer value 42.
newThread->Start(42);
// To start a thread using an instance method for the thread
// procedure, use the instance variable and method name when
// you create the ParameterizedThreadStart delegate.
Work^ someWork = gcnew Work;
newThread =
gcnew Thread(
gcnew ParameterizedThreadStart(someWork,
&Work::DoMoreWork));
// Pass an object containing data for the thread.
//
newThread->Start("The answer.");
}
static void DoWork(Object^ data)
{
Console::WriteLine("Static thread procedure. Data='{0}'",
data);
}
void DoMoreWork(Object^ data)
{
Console::WriteLine("Instance thread procedure. Data='{0}'",
data);
}
};
}
//Entry point of example application
int main()
{
SystemThreadingExample::Work^ samplework =
gcnew SystemThreadingExample::Work();
samplework->StartThreads();
}
// This code example produces the following output (the order
// of the lines might vary):
// Static thread procedure. Data='42'
// Instance thread procedure. Data='The answer'