无法使用“oldSyntax”开关在Visual C ++ 2005中编译线程示例

时间:2016-06-27 14:28:58

标签: c++-cli

我正在尝试关注如何在Windows窗体中创建线程的this example。纯粹按照这个例子,我在构建时会遇到多个语法错误。

我正在使用/clr:oldSyntax编译器开关来编译示例。

Form1类的初始化是错误的第一个来源:

public ref class Form1 : public System::Windows::Forms::Form
{

错误:

1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(24) : error C2059: syntax error : 'public'
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(24) : error C2059: syntax error : 'public'
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(25) : error C2143: syntax error : missing ';' before '{'
1>d:\programming applications\vs2005\threadexample\threadexample\Form1.h(25) : error C2447: '{' : missing function header (old-style formal list?)

这些错误究竟来自哪里?

完整代码:

#pragma once

using namespace System::Threading;

namespace ThreadExample 
{

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for Form1
    ///
    /// WARNING: If you change the name of this class, you will need to change the
    ///          'Resource File Name' property for the managed resource compiler tool
    ///          associated with all .resx files this class depends on.  Otherwise,
    ///          the designers will not be able to interact properly with localized
    ///          resources associated with this form.
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button1;
    protected: 
    private: System::Windows::Forms::ProgressBar^  progressBar1;

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

    private: Thread *trd;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
            this->SuspendLayout();
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(197, 12);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(75, 23);
            this->button1->TabIndex = 0;
            this->button1->Text = L"button1";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
            // 
            // progressBar1
            // 
            this->progressBar1->Location = System::Drawing::Point(94, 162);
            this->progressBar1->Name = L"progressBar1";
            this->progressBar1->Size = System::Drawing::Size(100, 23);
            this->progressBar1->TabIndex = 1;
            this->progressBar1->Click += gcnew System::EventHandler(this, &Form1::progressBar1_Click);
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(284, 261);
            this->Controls->Add(this->progressBar1);
            this->Controls->Add(this->button1);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
            this->ResumeLayout(false);

        }
#pragma endregion
    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) 
             {
                ThreadStart *myThreadDelegate = new ThreadStart(this, repeat);
                trd = new Thread(myThreadDelegate);
                trd->IsBackground = true;
                trd->Start();

             }
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
             {
                 MessageBox::Show(S"This is the main thread");
             }
    private: System::Void progressBar1_Click(System::Object^  sender, System::EventArgs^  e) 
             {
             }
    };

    __delegate void DelegateThreadTask();
    private: void ThreadTask()
    {
        int stp;
        int newval;
        Random *rnd=new Random();

        if (progressBar1->InvokeRequired == false)
            {
            stp=this->progressBar1->Step*rnd->Next(-1,2);
            newval = this->progressBar1->Value + stp;

            if (newval > this->progressBar1->Maximum)
                newval = this->progressBar1->Maximum;
            else if (newval < this->progressBar1->Minimum)
                newval = this->progressBar1->Minimum;

            this->progressBar1->Value = newval;
            }
        else 
            {
            DelegateThreadTask *myThreadDelegate = new DelegateThreadTask(this,ThreadTask);
            this->Invoke(myThreadDelegate);         
            }
    }
    private: void repeat()
    {   
        while(true)
        {
        ThreadTask();
        Thread::Sleep(100);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

C ++ / CLI的旧语法使用__gc来声明类。

参考文献:

测试1:

public ref class Foo
{
};

编译结果:

  OldSyntax.cpp
OldSyntax.cpp(1): error C2059: syntax error : 'public'
OldSyntax.cpp(2): error C2143: syntax error : missing ';' before '{'
OldSyntax.cpp(2): error C2447: '{' : missing function header (old-style formal list?)

测试2:

public __gc class Foo
{
};

编译结果:没有错误。

(注意:我使用VS2012进行测试。我不再安装VS2005; 2005年的错误信息可能略有不同。)