使用clr和std :: thread [英] using clr and std::thread

查看:77
本文介绍了使用clr和std :: thread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为台式机创建UI抽象层.现在,我正在实现.NET框架的功能.令人讨厌的事情是,如果我让用户在Visual Studio中创建CLR Windows窗体应用程序,则他们将无法使用所有标准库,例如 std :: thread ,并且如果我让他们创建另一种类型的应用程序,,控制台就会显示出来.

I'm creating an UI abstraction layer for desktops. Now I'm implementing the functionality of the .NET framework. The annoying thing is that if I let the users create a CLR Windows Forms Application in Visual studio they can't use all the standard libraries like std::thread and if I let them create another type of application, the console shows up.

是否可以将 clr std :: thread 一起使用,或者甚至更好的是,有一种方法可以防止控制台启动(或对两者隐藏)屏幕和任务栏)使用CLR控制台或CLR空项目.

Is there a way to use clr with std::thread or, even better, is there a way to prevent the console from starting (or hide it from both the screen and the taskbar)with a CLR Console or CLR Empty project.

谢谢

推荐答案

可能是一个老问题,但我之前也曾研究过同样的问题.由于CLR不允许您在编译时包含 std :: thead ,因此您可以尝试仅在链接时使用它.通常,您可以解决此问题,方法是在标头中声明该类,并将其仅包括在cpp文件中.但是,您可以在头文件中转发声明自己的类,但不能用于名称空间std中的类.根据C ++ 11标准17.6.4.2.1:

Might be an old question, but I looked into this same problem before. Since CLR does not allow you to include std::thead at compile time, you could try to use it only at linking time. Normally you could resolve this be forward declaring the class in your header and including them only in your cpp files. However you can forward declare your own classes in header files, but you can't for classes in namespace std. According to the C++11 standard, 17.6.4.2.1:

如果C ++程序添加了声明或命名空间std或命名空间std中的命名空间的定义除非另有说明.

The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified.

此问题的一种解决方法是创建一个线程类,该线程类继承自可以向前声明的 std :: thread .该类的头文件如下所示:

A workaround for this problem is to create a threading class that inherits from std::thread that you can forward declare. The header file for this class would look like:

#pragma once
#include <thread>
namespace Threading
{
    class Thread : std::thread
    {
    public:
        template<class _Fn, class... _Args> Thread(_Fn fn, _Args... args) : std::thread(fn, std::forward<_Args>(args)...)
        {

        }
    private:

    };
}

在要使用该线程的头文件中,可以像下面这样向前声明它:

In the header file that you would like to use the thread you can do forward declare it like:

#pragma once

// Forward declare the thread class 
namespace Threading { class Thread; }
class ExampleClass
{
    public:
        ExampleClass();
        void ThreadMethod();
    private:
        Threading::Thread * _thread;
};

然后,您可以在源文件中使用theading类,例如:

In your source file you can then use the theading class like:

#include "ExampleClass.h"
#include "Thread.h"

ExampleClass::ExampleClass() :
{
    _thread = new Threading::Thread(&ExampleClass::ThreadMethod, this);
}

void ExampleClass::ThreadMethod()
{
}

希望它可能对任何人都有帮助.

Hope it might help anyone.

这篇关于使用clr和std :: thread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆