控制台输出线程 [英] Console output from thread
本文介绍了控制台输出线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚开始使用线程体验,无法获得一些基础知识。我怎么能写到控制台从线程与间隔说10毫秒?所以我有一个线程类:
I've just started experiencing with thread and can't get some basics. How can i write to Console from thread with interval say 10 msec? So i have a thread class:
public ref class SecThr
{
public:
DateTime^ dt;
void getdate()
{
dt= DateTime::Now;
Console::WriteLine(dt->Hour+":"+dt->Minute+":"+dt->Second);
}
};
int main()
{
Console::WriteLine("Hello!");
SecThr^ thrcl=gcnew SecThr;
Thread^ o1=gcnew Thread(gcnew ThreadStart(SecThr,&thrcl::getdate));
}
我无法在Visual c ++ 2010 c ++ cli中编译它,错误C3924,C2825,C2146
I cannot compile it in my Visual c++ 2010 c++ cli, get a lot of errors C3924, C2825, C2146
推荐答案
最明显的错误:
- 缺少使用命名空间指令类,例如System :: Threading,如果您没有完全编写System :: Threading :: Thread,则需要。
- 使用类型为DateTime的^ hat类型值,不会发出编译错误,
- 没有正确构造一个委托对象,第一个参数是目标对象,第二个参数是函数指针。
- missing using namespace directives for the classes you use, like System::Threading, required if you don't write System::Threading::Thread in full.
- using the ^ hat on value types like DateTime, not signaled as a compile error but very detrimental to program efficiency, it will cause the value to be boxed.
- not constructing a delegate object correctly, first argument is the target object, second argument is the function pointer.
使用命名空间System重写此工具:
Rewriting it so it works:
using namespace System;
using namespace System::Threading;
public ref class SecThr
{
DateTime dt;
public:
void getdate() {
dt= DateTime::Now;
Console::WriteLine(dt.Hour + ":" + dt.Minute + ":" + dt.Second);
}
};
int main(array<System::String ^> ^args)
{
Console::WriteLine("Hello!");
SecThr^ thrcl=gcnew SecThr;
Thread^ o1=gcnew Thread(gcnew ThreadStart(thrcl, &SecThr::getdate));
o1->Start();
o1->Join();
Console::ReadKey();
}
这篇关于控制台输出线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文