| Threadオブジェクト利用方法 | BCB Tips! Last modified : 2002/11/29 |
| 追加コンポート名 | プロパティ名 | 値(設定した値) |
| ProgressBar1 | Max | 500 |
| Smooth | true | |
| Step | 1 | |
| Button1 | Caption | Start |
| Button2 | Caption | Stop |

| MainUnit.h | MainUnit.cpp |
//-------------------------------------
#ifndef MainUnitH
#define MainUnitH
//-------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
//-------------------------------------
class TMainForm : public TForm
{
__published: // IDE 管理のコンポーネント
TButton *Button1;
TButton *Button2;
TProgressBar *ProgressBar1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // ユーザー宣言
public: // ユーザー宣言
__fastcall TMainForm(TComponent* Owner);
TTestThread *mTest; // スレッドの生成1/2
};
//-------------------------------------
extern PACKAGE TMainForm *MainForm;
//-------------------------------------
#endif
|
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "TestThread.h"
#include "MainUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Button1Click(TObject *Sender)
{
Button1->Enabled = false;
Button2->Enabled = true;
Button2->SetFocus();
mTest = new TTestThread(true); // スレッドの生成2/2、
// 引数「true」では、停止した状態でスレッドが生成される
mTest->FreeOnTerminate = true; // スレッド終了時にメモリを自動的に解放
mTest->Resume(); // スレッドを開始
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Button2Click(TObject *Sender)
{
mTest->Terminate(); // スレッドへ終了要求を行う
Button1->Enabled = true;
Button2->Enabled = false;
Button1->SetFocus();
}
|
| TestThread.h | TestThread.cpp |
//-------------------------------------
#ifndef TestThreadH
#define TestThreadH
//-------------------------------------
#include <Classes.hpp>
//-------------------------------------
class TTestThread : public TThread
{
private:
protected:
void __fastcall Execute();
void __fastcall TTestThread::StepLine(void);
public:
__fastcall TTestThread(bool CreateSuspended);
};
//-------------------------------------
#endif
|
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <mmsystem.h> // for timeGetTime()
#include "TestThread.h"
#include "MainUnit.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall TTest::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//
//---------------------------------------------------------------------------
__fastcall TTestThread::TTestThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall TTestThread::Execute()
{
// ---- Place thread code here ----
unsigned int t;
MainForm->ProgressBar1->Position=0;
for(int i=MainForm->ProgressBar1->Max; i>0; i--){
if(Terminated) break; // スレッド終了要求がないかチェック
Synchronize(StepLine); // スレッド内からメソッドの呼び出し
t = timeGetTime(); // 今の時間を記憶
while(10 > timeGetTime()-t){ // 記憶した時間から 50ミリ秒 たつまでループ
Application->ProcessMessages();
}
}
MainForm->mTest->Terminate(); // スレッドへ終了要求を行う
MainForm->Button2Click(this);
}
//---------------------------------------------------------------------------
void __fastcall TTestThread::StepLine(void)
{
MainForm->ProgressBar1->StepIt();
MainForm->ProgressBar1->Update();
}
|
| Type | Name | Note
| 書籍 | C++Builder 6コンポーネント活用ガイド&実践プログラミング Vol.2 | 実践テクニック編 著者:田中和明、手塚忠則 | 発行:カットシステム |
|---|