6

我真的在用 Visual C++/CLR 中的线程编程苦苦挣扎。我搜索了很多,并在互联网上找到了很多材料,包括官方资源,但我仍然感到困惑。C++/CLR 的资源很少。其中大多数是用于 C# 或旧式 C++ 的。我尝试运行这个简单的代码。我选择了一个 clr 控制台应用程序类型的新项目并将以下代码放在那里,但我遇到了我不理解的错误。

// thread_clr.cpp : main project file.

#include "stdafx.h"

using namespace System;
using namespace System;
using namespace System::Threading;
class MessagePrinter;

// class ThreadTester demonstrates basic threading concepts
class ThreadTester
{
    static int Main()
    {
        // Create and name each thread. Use MessagePrinter's
        // Print method as argument to ThreadStart delegate.
        MessagePrinter printer1 = gcnew MessagePrinter();
        Thread thread1 = gcnew Thread ( gcnew ThreadStart( printer1.Print ) );
        thread1.Name = "thread1";

        MessagePrinter printer2 = gcnew MessagePrinter();
        Thread thread2 = gcnew Thread ( gcnew ThreadStart( printer2.Print ) );
        thread2.Name = "thread2";

        MessagePrinter printer3 = gcnew MessagePrinter();
        Thread thread3 = gcnew Thread ( gcnew ThreadStart( printer3.Print ) );
        thread3.Name = "thread3";

        Console.WriteLine( "Starting threads" );
        // call each thread's Start method to place each
        // thread in Started state
        thread1.Start();
        thread2.Start();
        thread3.Start();

        Console.WriteLine( "Threads started\n" );
    } // end method Main
}; // end class ThreadTester

class MessagePrinter
{
    private int sleepTime;
    private static Random random = gcnew Random();

    // constructor to initialize a MessagePrinter object
    public MessagePrinter()
    {
        // pick random sleep time between 0 and 5 seconds
        sleepTime = random.Next( 5001 );
    }

    //controls Thread that prints message
    public void Print()
    {
        // obtain reference to currently executing thread
        Thread current = Thread.CurrentThread;

        // put thread to sleep for sleepTime amount of time
        Console.WriteLine(current.Name + " going to sleep for " + sleepTime );

        Thread.Sleep ( sleepTime );

        // print thread name
        Console.WriteLine( current.Name + " done sleeping" );

    } // end method Print
} // end class MessagePrinter

请帮忙。或者更好的是,请指导我一些教程或其他影响。我知道 SO 不是一个教程网站,我也不是在问一个,但如果有人能至少指出 C++/CLR 线程的资源,我将不胜感激。C++/CLR Winform 线程。真的很感激

问候

一些错误是:

'printer1' 使用未定义的类 'MessagePrinter' 'Print' : 不是 'System::Int32' 的成员 'System::Threading::ThreadStart' : 委托构造函数需要 2 个参数 'System::Threading:: Thread::Thread' : 没有合适的默认构造函数可用 'System::Threading::Thread::Thread' : 没有合适的默认构造函数可用 'syntax error : 'int' 前面应该有 ':' 'cannot declare a managed 'random ' 在非托管 'MessagePrinter' 中可能不会声明全局或静态变量,或引用 gc 堆 'MessagePrinter::random' 中的对象的本机类型的成员:您不能嵌入引用类型的实例,'System ::Random',在原生类型 'MessagePrinter::random' 中:只能在类中初始化静态 const 整数数据成员 'MessagePrinter' 前面应该有 ':' 'void' 应该前面有 ':'

4

2 回答 2

11

我为您修复了该代码,但让我先说一件事:您确实需要先学习基础知识。这甚至不是 C++/CLI 代码,您没有找到或无法修复最明显的语法错误。暂时忘记线程,它们很难。先学基本的东西。

#include "stdafx.h"

using namespace System;
using namespace System::Threading;

ref class MessagePrinter
{
private:
    int sleepTime;
    static Random^ random = gcnew Random();

// constructor to initialize a MessagePrinter object
public:
    MessagePrinter()
    {
        // pick random sleep time between 0 and 5 seconds
        sleepTime = random->Next( 5001 );
    }

    //controls Thread that prints message
    void Print()
    {
        // obtain reference to currently executing thread
        Thread^ current = Thread::CurrentThread;

        // put thread to sleep for sleepTime amount of time
        Console::WriteLine(current->Name + " going to sleep for " + sleepTime );

        Thread::Sleep ( sleepTime );

        // print thread name
        Console::WriteLine( current->Name + " done sleeping" );

    } // end method Print
}; // end class MessagePrinter  

int main()
{
    // Create and name each thread. Use MessagePrinter's
    // Print method as argument to ThreadStart delegate.
    MessagePrinter^ printer1 = gcnew MessagePrinter();
    Thread^ thread1 = gcnew Thread(gcnew ThreadStart( printer1, &MessagePrinter::Print ) );
    thread1->Name = "thread1";

    MessagePrinter^ printer2 = gcnew MessagePrinter();
    Thread^ thread2 = gcnew Thread ( gcnew ThreadStart( printer2, &MessagePrinter::Print ) );
    thread2->Name = "thread2";

    MessagePrinter^ printer3 = gcnew MessagePrinter();
    Thread^ thread3 = gcnew Thread ( gcnew ThreadStart( printer3, &MessagePrinter::Print ) );
    thread3->Name = "thread3";

    Console::WriteLine( "Starting threads" );
    // call each thread's Start method to place each
    // thread in Started state
    thread1->Start();
    thread2->Start();
    thread3->Start();

    Console::WriteLine( "Threads started\n" );
    Console::ReadLine();

    return 0;
}
于 2013-07-12T07:45:13.270 回答
2

请注意,您离开了 Main 函数,因此所有线程都将被终止......所以Console.ReadLine()在“main”的最后一行放置一个......

另外:您的源代码是 C# 而不是 C++/CLI ...

于 2013-07-12T06:51:10.110 回答