【发布时间】:2013-07-11 12:05:05
【问题描述】:
我正在尝试按照http://www.drdobbs.com/cpp/ccli-threading-part-i/184402018 的教程在 Visual c++ 中使用 winform 进行线程编程。我打开了一个 win32 控制台项目,并在其中添加了一个空的 cpp 文件,我在其中放置了如下代码:
using namespace System;
using namespace System::Threading;
public class ThreadX{
int loopStart;
int loopEnd;
int dispFrequency;
public:
ThreadX(int startValue, int endValue, int frequency)
{
loopStart = startValue;
loopEnd = endValue;
dispFrequency = frequency;
}
void ThreadEntryPoint()
{
String^ threadName = Thread::CurrentThread->Name;
for (int i = loopStart; i <= loopEnd; ++i)
{
if ( i % dispFrequency == 0)
{
Console::WriteLine("{0} : i = {1,10}", threadName, i);
}
}
Console::WriteLine("{0} thread terminating", threadName);
}
};
int main()
{
ThreadX o1 = gcnew ThreadX(0, 1000000,200000);
Thread^ t1 = gcnew Thread(gcnew ThreadStart(o1, &ThreadX::ThreadEntryPoint));
t1->Name = "t1";
ThreadX o2 = gcnew ThreadX(-1000000, 0, 200000);
Thread^ t2 = gcnew Thread(gcnew ThreadStart(o2, &ThreadX::ThreadEntryPoint));
t1->Name = "t2";
t1->Start();
t2->Start();
Console::WriteLine("Primary Thread Terminating");
}
但是这给了我错误,例如:
- 错误 C2726:“gcnew”只能用于创建具有 托管类型
- 错误 C2440:“正在初始化”:不能 从 'ThreadX *' 转换为 'ThreadX' 没有构造函数可以接受 源类型或构造函数重载决议不明确
- 错误 C3364:“System::Threading::ThreadStart”:委托构造函数的参数无效;委托目标需要是 指向成员函数的指针
【问题讨论】:
标签: multithreading visual-studio-2010 visual-c++ c++-cli