Threading in C++/CLI, CLR

huangapple go评论57阅读模式
英文:

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 &quot;pch.h&quot;
#include &quot;BgWorker.h&quot;

void BgWorker::MainLoop()
{
	...
}

myProgramm.cpp:

#include &quot;pch.h&quot;
#include &quot;BgWorker.h&quot;

int main(array&lt;System::String ^&gt; ^args)
{
    BgWorker^bg = gcnew BgWorker();
    System::Threading::Thread^ ml = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(bg, &amp;BgWorker::MainLoop));
    ml-&gt;Name = &quot;MainLoop&quot;;
    ml-&gt;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, &amp;BgWorker::MainLoop));
	ml-&gt;Name = &quot;MainLoop&quot;;
	ml-&gt;Start();
}

This question can be closed now.

huangapple
  • 本文由 发表于 2023年4月17日 20:34:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定