0

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.

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.

Thanks

Almas Adilbek
  • 4,371
  • 10
  • 58
  • 97
JMRC
  • 1,473
  • 1
  • 17
  • 36
  • The error seems to strongly suggest that `` is not supported in CLR: `#error directive: ERROR: Concurrency Runtime is not supported when compiling /clr.` .NET does have threading as well. – chris Mar 03 '13 at 18:13
  • Yes, that's why I'm looking for an alternative. – JMRC Mar 03 '13 at 18:28
  • Just don't compile native C++ code with /clr. The point of using C++/CLI is that it can do both. – Hans Passant Mar 03 '13 at 20:55
  • This is not an option, since I want the users to be able to integrate this code into their own code without problems. Frequently used and generic libraries like std and boost should be compatible. – JMRC Mar 03 '13 at 21:02

2 Answers2

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:

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.

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;
};

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.

Sam Xia
  • 171
  • 11
0

This is an old question, but in case someone hits the same problem: boost::thread is an "affordable" and practical replacement (provided you can use boost in your project). Strangely, it bypasses the incompatibility.

DarkWanderer
  • 8,739
  • 1
  • 25
  • 56