プロが教えるわが家の防犯対策術!

Visual C++ 2005 Express Editionを使用している初心者です。

タイトルの方法がどうしてもわからないので教えてください。

うまく説明できないので聞きたいところのコードを載せます。


//SAMPLE.cpp
#include "stdafx.h"
#include "Form1.h"

using namespace SAMPLE;

int main(array<System::String ^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);

Application::Run(gcnew Form1());
}



//Form1.h
#include <process.h>
#include <vcclr.h>

namespace SAMPLE {

unsigned __stdcall counter(void *arg);
intthread_id1;
unsigneddummy;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

public ref class Form1 : public System::Windows::Forms::Form
{
public:Form1(void)
{
InitializeComponent();

//別スレッド起動
thread_id1=_beginthreadex(NULL,0,counter,(void *)1,0,&dummy);
}

private: System::Windows::Forms::TextBox^ textBox1;

private: System::ComponentModel::Container ^components;

void InitializeComponent(void)
{
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
//省略(コンポーネント初期化)
}

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
};


unsigned __stdcall counter(void *arg){//別スレッド

//ここで文字列を記述して上のtextBox1に表示したい

return 0;
}
}


初歩的なことかもしれませんが、
よろしくお願いします。

A 回答 (1件)

フォームやコントロールを作成したスレッド以外のスレッドがフォームや


コントロールを直接操作することはできません。
フォーム・コントロールが持つ Invoke メソッドなどを介して行います。

スレッド作成に _beginthreadex を使用しているようですが、
この関数はWindows APIが提供するスレッドハンドルを返すので、
明示的に CloseHandle でハンドルを閉じなければなりません。
System::Threading::Thread クラスを使ってはどうでしょう。
    • good
    • 0
この回答へのお礼

System::Threading::Threadを使って別スレッドをForm1クラスの中に入れ、実現することができました。
ありがとうございました。

お礼日時:2007/02/18 04:01

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!