user2572521
user2572521

Reputation: 113

Thread Program in visual C++/CLI giving errors

I am trying to follow the tutorial at http://www.drdobbs.com/cpp/ccli-threading-part-i/184402018 to do thread programming in winform in visual c++. I opened a win32 console project and added an empty cpp file to it inside which i placed the code as follows:

    using namespace System;
    using namespace System::Threading;

    public class ThreadX{

        int loopStart;
        int loopEnd;
        int dispFrequency;

        public:


        ThreadX(int startValue, int endValue, int frequency)
        {
            loopStart = startValue;
            loopEnd = endValue;
            dispFrequency = frequency;
        }

        void ThreadEntryPoint()
        {
            String^ threadName = Thread::CurrentThread->Name;

            for (int i = loopStart; i <= loopEnd; ++i)
            {
                if ( i % dispFrequency == 0)
                {
                    Console::WriteLine("{0} : i = {1,10}", threadName, i);
                }
            }
            Console::WriteLine("{0} thread terminating", threadName);
        }
};

int main()
{
    ThreadX o1 = gcnew ThreadX(0, 1000000,200000);
    Thread^ t1 = gcnew Thread(gcnew ThreadStart(o1, &ThreadX::ThreadEntryPoint));
    t1->Name = "t1";

    ThreadX o2 = gcnew ThreadX(-1000000, 0, 200000);
    Thread^ t2 = gcnew Thread(gcnew ThreadStart(o2, &ThreadX::ThreadEntryPoint));
    t1->Name = "t2";

    t1->Start();
    t2->Start();
    Console::WriteLine("Primary Thread Terminating");
}

However this gives me errors such as :

  1. error C2726: 'gcnew' may only be used to create an object with managed type
  2. error C2440: 'initializing' : cannot convert from 'ThreadX *' to 'ThreadX' No constructor could take the source type, or constructor overload resolution was ambiguous
  3. error C3364: 'System::Threading::ThreadStart' : invalid argument for delegate constructor; delegate target needs to be a pointer to a member function

Upvotes: 0

Views: 771

Answers (1)

Jochen Kalmbach
Jochen Kalmbach

Reputation: 3684

You are mixing C++ and C++/CLI which is a different thing. Replace

public class ThreadX

with

public ref class ThreadX

Upvotes: 1

Related Questions