2016-06-27 13 views
0

Windowsフォームでスレッドを作成する方法については、this exampleに従っています。純粋にこの例に従うと、ビルド時に複数の構文エラーが発生します。"oldSyntax"スイッチを使用してVisual C++ 2005でスレッド例をコンパイルできません

私はこの例をコンパイルするために/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); 
     } 
    } 
} 

答えて

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

コンパイル結果:エラーなし。

(注:私はVS2005はもうインストールしていない私のテストのためにVS2012を使用し、エラーメッセージは、2005年には、わずかに異なる場合があります)

+0

私が見るああ、ありがとう!私は/ clr:oldSyntaxの代わりに/ clrを使用して終了し、チュートリアルで書かれたいくつかのコードを手動で更新して動作させました – akivjh

関連する問題