|
막막한초보님의 막막한 질문(?)을 보고
이런 것을 원하지 않나 해서 만들어 봤습니다.
여기저기 빠진 부분도 있지만 힌트를 얻는데엔 문제가 없을 것 같습니다.
행복하세요.
** Unit1.Cpp **
//---------------------------------------------------------------------------
#include <vcl.h>
#include <Classes.hpp>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int g_nThreadId = 0;
//---------------------------------------------------------------------------
class TMyThread : public TThread
{
private:
int m_nId;
int m_nCnt;
void __fastcall Put( void );
protected:
void __fastcall Execute();
public:
__fastcall TMyThread(bool CreateSuspended, int nId);
};
//---------------------------------------------------------------------------
__fastcall TMyThread::TMyThread(bool CreateSuspended, int nId) : TThread(CreateSuspended)
{
FreeOnTerminate = true;
m_nId = nId; // ID가 중복되지 않도록 검사하는 루틴이 필요.
m_nCnt = 0;
}
//---------------------------------------------------------------------------
void __fastcall TMyThread::Execute( void )
{
while( !Terminated ) {
Synchronize( Put );
::Sleep( 100 );
}
}
//---------------------------------------------------------------------------
void __fastcall TMyThread::Put( void )
{
AnsiString asStr;
asStr.sprintf( "Thread ID. %d의 실행 횟수 = %d", m_nId, ++m_nCnt );
Form1->Memo1->Lines->Add( asStr );
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1( void )
{
// 보관된 Thread 객체 해제 처리부 ....
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMyThread *pThread = new TMyThread( false, g_nThreadId );
if( pThread != NULL ) {
ComboBox1->Items->AddObject( AnsiString( "Thread " ) + g_nThreadId++, pThread );
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if( ComboBox1->ItemIndex >= 0 ) {
((TMyThread *)ComboBox1->Items->Objects[ComboBox1->ItemIndex])->Suspend();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
if( ComboBox1->ItemIndex >= 0 ) {
((TMyThread *)ComboBox1->Items->Objects[ComboBox1->ItemIndex])->Resume();
}
}
//---------------------------------------------------------------------------
** Unit1.h **
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TComboBox *ComboBox1;
TMemo *Memo1;
TButton *Button2;
TButton *Button3;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
__fastcall ~TForm1( void );
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
** unit1.dfm **
object Form1: TForm1
Left = 276
Top = 115
Width = 327
Height = 198
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 12
Top = 20
Width = 75
Height = 25
Caption = '생성'
TabOrder = 0
OnClick = Button1Click
end
object ComboBox1: TComboBox
Left = 92
Top = 20
Width = 209
Height = 21
ImeName = '한국어 입력 시스템 (IME 2000)'
ItemHeight = 13
TabOrder = 1
end
object Memo1: TMemo
Left = 92
Top = 52
Width = 209
Height = 97
ImeName = '한국어 입력 시스템 (IME 2000)'
Lines.Strings = (
'Memo1')
TabOrder = 2
end
object Button2: TButton
Left = 12
Top = 72
Width = 75
Height = 25
Caption = '일시중지'
TabOrder = 3
OnClick = Button2Click
end
object Button3: TButton
Left = 12
Top = 124
Width = 75
Height = 25
Caption = '계속'
TabOrder = 4
OnClick = Button3Click
end
end
막막한초보 님이 쓰신 글 :
: threadlist에 대해서 검색했는데... 아직 해결이 안됐어요...
: 비슷한 질문이 있던데.. 그 대답으로는 해결이 안되네요.. ㅠ.ㅠ
: 제가 하고 싶은 것은 말 그대로 thread를 버튼 클릭 할때 마다 하나씩 만들어서
: Threadlist 에 저장하고 그 중에서 제가 원하는 Thread를 찾아서 죽이거나 멈추고
: 그런것을 하고 싶거든요...
: 그래서 threadlist에 넣은 다음 각각의 스레드에는 어떻게 접근 하죠??
: 그것이 궁금합니다.
:
: TList *pList = MyThreadList->LockList();
:
: try
: {
: for (int X = 0; X < pList->Count; X++)
: Something(pList->Items[X]);
: }
: __finally
: {
: MyThreadList->UnlockList();
: }
: 이 도움말로는 잘 모르겠습니다...
: 조금의 설명을 해 주세요... 사용 예제가 있으면 더 좋구요..
: 긴글 읽어 주셔셔 감사합니다.
|