英文:
Threading in C++/CLI, CLR
问题
Here's the translated code part:
.h:
ref class BgWorker
{
private:
void MainLoop();
public:
void Start();
};
.cpp:
void BgWorker::MainLoop()
{
while(true)
{
...
}
}
void BgWorker::Start()
{
System::Threading::Thread^ td = gcnew System::Threading::Thread(MainLoop); // v1; didn't working
System::Threading::Thread^ td = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(MainLoop)); // v2; didn't
System::Threading::Thread^ td = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(this, &MainLoop)); // v3; didn't
std::thread td(MainLoop); // v4, didn't
}
Also tried via System::ComponentModel::DoWorkEventHandler()
, most likely I did something wrong.
I want to use this constructor, but I can't pull out a link to MainLoop:
public: Thread(System::Threading::ThreadStart^ start)
Same problem with standard stream std::thread
, MainLoop is not a function reference.
I couldn't find anything on my question on Google. Many thanks for the help!
英文:
Faced a problem. Need to call a class member method on a separate thread C++/CLI
.h:
ref class BgWorker
{
private:
void MainLoop();
public:
void Start();
};
.cpp:
void BgWorker::MainLoop()
{
while(true)
{
...
}
}
void BgWorker::Start()
{
System::Threading::Thread^ td = gcnew System::Threading::Thread(MainLoop); // v1; didn't working
System::Threading::Thread^ td = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(MainLoop)); // v2; didn't
System::Threading::Thread^ td = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(this, &MainLoop)); // v3; didn't
std::thread td(MainLoop); // v4, didn't
}
Also tried via System::ComponentModel::DoWorkEventHandler()
, most likely i did something wrong
I want to use this constructor, but I can't pull out a link to MainLoop:
public : Thread(System::Threading::ThreadStart^ start)
Same problem with standard stream std::thread
, MainLoop is not a function reference
I couldn't find anything on my question on google. Many thanks for the help!
答案1
得分: -1
如果有人感兴趣,这是我的做法:
BgWorker.h:
ref class BgWorker
{
public:
void MainLoop();
}
BgWorker.cpp:
#include "pch.h"
#include "BgWorker.h"
void BgWorker::MainLoop()
{
...
}
myProgramm.cpp:
#include "pch.h"
#include "BgWorker.h"
int main(array<System::String ^> ^args)
{
BgWorker^ bg = gcnew BgWorker();
System::Threading::Thread^ ml = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(bg, &BgWorker::MainLoop));
ml->Name = "MainLoop";
ml->Start();
}
这并不是我想要的完全(不希望在主函数中管理线程),但作为入门,这是一个很好的结果。我还不得不摆脱现在不再需要的 BgWorker::Start()
函数。
而且,如果有人知道如何启动由内部 ref 类方法管理的新线程,我将非常高兴看到一个例子。
感谢 Ben Voigt;现在这个问题看起来非常简单了。
在第一个结构中:
void BgWorker::Start()
{
System::Threading::Thread^ ml = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(this, &BgWorker::MainLoop));
ml->Name = "MainLoop";
ml->Start();
}
现在可以关闭这个问题了。
英文:
If anyone is interested, here's what I did:
BgWorker.h:
ref class BgWorker
{
public:
void MainLoop();
}
BgWorker.cpp:
#include "pch.h"
#include "BgWorker.h"
void BgWorker::MainLoop()
{
...
}
myProgramm.cpp:
#include "pch.h"
#include "BgWorker.h"
int main(array<System::String ^> ^args)
{
BgWorker^bg = gcnew BgWorker();
System::Threading::Thread^ ml = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(bg, &BgWorker::MainLoop));
ml->Name = "MainLoop";
ml->Start();
}
It's not exactly what I wanted (didn't want to manage threads in main), but for starters, it's a great result. I also had to get rid of the now unnecessary BgWorker::Start()
function.
Аnd yet, if someone knows how to start a new thread managed by an internal ref class method, I would be very happy with an example.
EDITED
Many thanks to Ben Voigt; it this question just looks ridiculously easy now
With the first structure:
void BgWorker::Start()
{
System::Threading::Thread^ ml = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(this, &BgWorker::MainLoop));
ml->Name = "MainLoop";
ml->Start();
}
This question can be closed now.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论