namespace MultiThread {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Threading;
/// <summary>
/// Сводка для Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
TimeCounter = 0;
StopFlag = false;
//
//TODO: добавьте код конструктора
//
}
protected:
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TableLayoutPanel^ tableLayoutPanel1;
private: System::Windows::Forms::Label^ lbTimer;
protected:
private: System::Windows::Forms::Button^ btnStart;
private: System::Windows::Forms::Button^ btnStop;
private: System::Windows::Forms::Button^ btnRenew;
private:
int TimeCounter;
bool StopFlag;
Thread^ myTimerThread;
// делегат занимается обновлением таймера на форме
delegate void UpdateLabelCallback();
/// <summary>
/// Требуется переменная конструктора.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Обязательный метод для поддержки конструктора - не изменяйте
/// содержимое данного метода при помощи редактора кода.
/// </summary>
void InitializeComponent(void)
{
this->tableLayoutPanel1 = (gcnew System::Windows::Forms::TableLayoutPanel());
this->lbTimer = (gcnew System::Windows::Forms::Label());
this->btnStart = (gcnew System::Windows::Forms::Button());
this->btnStop = (gcnew System::Windows::Forms::Button());
this->btnRenew = (gcnew System::Windows::Forms::Button());
this->tableLayoutPanel1->SuspendLayout();
this->SuspendLayout();
//
// tableLayoutPanel1
//
this->tableLayoutPanel1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
| System::Windows::Forms::AnchorStyles::Left)
| System::Windows::Forms::AnchorStyles::Right));
this->tableLayoutPanel1->ColumnCount = 2;
this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent,
50)));
this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent,
50)));
this->tableLayoutPanel1->Controls->Add(this->lbTimer, 0, 0);
this->tableLayoutPanel1->Controls->Add(this->btnStart, 0, 1);
this->tableLayoutPanel1->Controls->Add(this->btnStop, 1, 1);
this->tableLayoutPanel1->Controls->Add(this->btnRenew, 0, 2);
this->tableLayoutPanel1->Location = System::Drawing::Point(2, 4);
this->tableLayoutPanel1->Name = L"tableLayoutPanel1";
this->tableLayoutPanel1->RowCount = 3;
this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Absolute, 80)));
this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent, 50)));
this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent, 50)));
this->tableLayoutPanel1->Size = System::Drawing::Size(289, 268);
this->tableLayoutPanel1->TabIndex = 0;
//
// lbTimer
//
this->lbTimer->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
| System::Windows::Forms::AnchorStyles::Left)
| System::Windows::Forms::AnchorStyles::Right));
this->lbTimer->AutoSize = true;
this->tableLayoutPanel1->SetColumnSpan(this->lbTimer, 2);
this->lbTimer->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 48, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->lbTimer->Location = System::Drawing::Point(3, 0);
this->lbTimer->Name = L"lbTimer";
this->lbTimer->Size = System::Drawing::Size(283, 80);
this->lbTimer->TabIndex = 0;
this->lbTimer->Text = L"0";
this->lbTimer->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
//
// btnStart
//
this->btnStart->Anchor = System::Windows::Forms::AnchorStyles::None;
this->btnStart->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 20.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->btnStart->ForeColor = System::Drawing::Color::DarkGreen;
this->btnStart->Location = System::Drawing::Point(17, 94);
this->btnStart->Name = L"btnStart";
this->btnStart->Size = System::Drawing::Size(109, 66);
this->btnStart->TabIndex = 1;
this->btnStart->Text = L"ПУСК";
this->btnStart->UseVisualStyleBackColor = true;
this->btnStart->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// btnStop
//
this->btnStop->Anchor = System::Windows::Forms::AnchorStyles::None;
this->btnStop->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 20.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->btnStop->ForeColor = System::Drawing::Color::DarkRed;
this->btnStop->Location = System::Drawing::Point(162, 94);
this->btnStop->Name = L"btnStop";
this->btnStop->Size = System::Drawing::Size(109, 66);
this->btnStop->TabIndex = 1;
this->btnStop->Text = L"СТОП";
this->btnStop->UseVisualStyleBackColor = true;
this->btnStop->Click += gcnew System::EventHandler(this, &Form1::btnStop_Click);
//
// btnRenew
//
this->btnRenew->Anchor = System::Windows::Forms::AnchorStyles::None;
this->tableLayoutPanel1->SetColumnSpan(this->btnRenew, 2);
this->btnRenew->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 20.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->btnRenew->ForeColor = System::Drawing::Color::Black;
this->btnRenew->Location = System::Drawing::Point(16, 192);
this->btnRenew->Name = L"btnRenew";
this->btnRenew->Size = System::Drawing::Size(256, 57);
this->btnRenew->TabIndex = 1;
this->btnRenew->Text = L"Сброс";
this->btnRenew->UseVisualStyleBackColor = true;
this->btnRenew->Click += gcnew System::EventHandler(this, &Form1::btnRenew_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 273);
this->Controls->Add(this->tableLayoutPanel1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->tableLayoutPanel1->ResumeLayout(false);
this->tableLayoutPanel1->PerformLayout();
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
if (TimeCounter == 1000)
{
MessageBox::Show(L"Достигнут предел! Сбросьте счетчик", L"Ошибка");
return;
}
//Запуск потока
if (StopFlag == true) {
StopFlag = false;
myTimerThread = gcnew Thread(gcnew ThreadStart(this, &Form1::TimerThread));
myTimerThread->Start();
}
}
private: System::Void btnRenew_Click(System::Object^ sender, System::EventArgs^ e) {
TimeCounter = 0;
lbTimer->Text = TimeCounter.ToString();
}
private: System::Void btnStop_Click(System::Object^ sender, System::EventArgs^ e) {
if (!StopFlag) StopFlag = true;
}
public: void TimerThread()
{
// объявляем делегат
UpdateLabelCallback^ d = gcnew UpdateLabelCallback(this, &Form1::UpdateLabel);
try
{
this->Invoke(d);
}
catch (...) {}
while (TimeCounter < 1000 && !StopFlag)
{
Thread::Sleep(1000);
TimeCounter++;
if (!StopFlag) {
try
{
this->Invoke(d);
}
catch (...) {}
}
}
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
StopFlag = true;
}
private: System::Void UpdateLabel()
{
lbTimer->Text = TimeCounter.ToString();
}
};
}
|