Здравствуйте! Не могу разобраться как работать с thread в windows forms. Пишу в visual studio 2015. Идея в том что бы по нажатию на 2 разные кнопки запускались 2 разных потока и выводили значения в соответствующий listbox с определенными задержками, а по нажатию на 3-ю кнопку в 3-й listbox я в ручную выводил цифры. Код | #pragma once #include <windows.h> #include <thread> #define ERROR_CREATE_THREAD -11 #define SUCCESS 0 namespace StreamsForms { 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 MyForm /// </summary> //----------------------------------------------------------------------- int but1 = 1; int but2 = 1; int but3 = 1; //----------------------------------------------------------------------- public ref class MyForm : public System::Windows::Forms::Form { public: MyForm(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// <summary> /// Clean up any resources being used. /// </summary> ~MyForm() { if (components) { delete components; } } private: System::Windows::Forms::ListBox^ listBox1; protected: private: System::Windows::Forms::ListBox^ listBox2; private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::Button^ button2; private: System::Windows::Forms::ListBox^ listBox3; private: System::Windows::Forms::Button^ button3; private: System::Windows::Forms::Button^ button4; private: /// <summary> /// Required designer variable. /// </summary> System::ComponentModel::Container ^components; #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->listBox1 = (gcnew System::Windows::Forms::ListBox()); this->listBox2 = (gcnew System::Windows::Forms::ListBox()); this->button1 = (gcnew System::Windows::Forms::Button()); this->button2 = (gcnew System::Windows::Forms::Button()); this->listBox3 = (gcnew System::Windows::Forms::ListBox()); this->button3 = (gcnew System::Windows::Forms::Button()); this->button4 = (gcnew System::Windows::Forms::Button()); this->SuspendLayout(); // // listBox1 // this->listBox1->FormattingEnabled = true; this->listBox1->Location = System::Drawing::Point(12, 12); this->listBox1->Name = L"listBox1"; this->listBox1->Size = System::Drawing::Size(70, 160); this->listBox1->TabIndex = 0; // // listBox2 // this->listBox2->FormattingEnabled = true; this->listBox2->Location = System::Drawing::Point(88, 12); this->listBox2->Name = L"listBox2"; this->listBox2->Size = System::Drawing::Size(70, 160); this->listBox2->TabIndex = 1; // // button1 // this->button1->Location = System::Drawing::Point(12, 178); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(70, 23); this->button1->TabIndex = 2; this->button1->Text = L"Старт"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click); // // button2 // this->button2->Location = System::Drawing::Point(88, 178); this->button2->Name = L"button2"; this->button2->Size = System::Drawing::Size(70, 23); this->button2->TabIndex = 3; this->button2->Text = L"Старт"; this->button2->UseVisualStyleBackColor = true; this->button2->Click += gcnew System::EventHandler(this, &MyForm::button2_Click); // // listBox3 // this->listBox3->FormattingEnabled = true; this->listBox3->Location = System::Drawing::Point(164, 12); this->listBox3->Name = L"listBox3"; this->listBox3->Size = System::Drawing::Size(70, 160); this->listBox3->TabIndex = 4; // // button3 // this->button3->Location = System::Drawing::Point(164, 178); this->button3->Name = L"button3"; this->button3->Size = System::Drawing::Size(70, 23); this->button3->TabIndex = 5; this->button3->Text = L"Добавить"; this->button3->UseVisualStyleBackColor = true; this->button3->Click += gcnew System::EventHandler(this, &MyForm::button3_Click); // // button4 // this->button4->Location = System::Drawing::Point(12, 207); this->button4->Name = L"button4"; this->button4->Size = System::Drawing::Size(222, 23); this->button4->TabIndex = 6; this->button4->Text = L"Очистить"; this->button4->UseVisualStyleBackColor = true; this->button4->Click += gcnew System::EventHandler(this, &MyForm::button4_Click); // // MyForm // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(246, 240); this->Controls->Add(this->button4); this->Controls->Add(this->button3); this->Controls->Add(this->listBox3); this->Controls->Add(this->button2); this->Controls->Add(this->button1); this->Controls->Add(this->listBox2); this->Controls->Add(this->listBox1); this->Name = L"MyForm"; this->Text = L"Потоки"; this->ResumeLayout(false); } #pragma endregion private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { thread func_thread(stream1); } private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { thread func_thread(stream2); } private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) { mulone(); } private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) { listBox1->Items->Clear(); listBox2->Items->Clear(); listBox3->Items->Clear(); } //----------------------------------------------------------------------- void mulone() { if (but3 < 10) { listBox3->Items->Add(but3); but3++; } else if (but3 = 10) { listBox3->Items->Add(but3); but3 = 1; } listBox3->TopIndex = listBox3->Items->Count - 1; } void stream1() { for (int i = 0; i < 5; i++) { listBox1->Items->Add(i); listBox1->TopIndex = listBox1->Items->Count - 1; Sleep(500); } } void stream2() { for (int i = 0; i < 5; i++) { listBox2->Items->Add(i); listBox2->TopIndex = listBox2->Items->Count - 1; Sleep(1000); } } }; }
|
Выдает следующие ошибки: Серьезность Код Описание Проект Файл Строка Состояние подавления Ошибка (активно) директива #error: <thread> is not supported when compiling with /clr or /clr: pure. StreamsForms c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thread 8 Ошибка (активно) идентификатор "thread" не определен StreamsForms e:\Projects\Visual Studio\StreamsForms1\StreamsForms\MyForm.h 176 Ошибка (активно) требуется точка с запятой ";" StreamsForms e:\Projects\Visual Studio\StreamsForms1\StreamsForms\MyForm.h 176 Ошибка (активно) идентификатор "thread" не определен StreamsForms e:\Projects\Visual Studio\StreamsForms1\StreamsForms\MyForm.h 180 Ошибка (активно) требуется точка с запятой ";" StreamsForms e:\Projects\Visual Studio\StreamsForms1\StreamsForms\MyForm.h 180 Ошибка C1189 #error: <thread> is not supported when compiling with /clr or /clr: pure. StreamsForms C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thread 8 Я полагаю стандарт что-то со стандартами языка не так? В общем как выйти из положения?
|