求助:XP vs2008 vc++ 一个简单的多线程建立后立即运行出错
写了一个简单的多线程程序,单击开始按钮,建立一个新线程,当
CreateThread函数第五个参数为CREATE_SUSPENDED时,不立即运行新线程不会出错,但设为0时调试时出错。怀疑ThreadFunc()定义错误或者没有生成新线程,查看CreateThread返回的hThread不是NULL,但ThreadFunc()很简单,也不应该有什么错,麻烦高手帮忙看看。
#pragma once
#include <string>
#include <stdlib.h>
#include <tchar.h>
#include <Windows.h>
#pragma comment(lib,"User32.lib")
#pragma comment(lib,"Kernel32.lib")
static void WINAPI ThreadFunc();
namespace duoxiancheng {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
void ThreadFunc()
{
//HWND hWnd;
//hWnd=::FindWindow(NULL,_T("Form1"));
//char a[4];
for (int i=1;i<=999;i++)
{
//i.ReleaseBuffer();
/*_itoa_s(i,a,10);
System::String ^ s0 = gcnew System::String(a);
System::Windows::Forms::TextBox^ textBox1=gcnew TextBox;
textBox1->Location = System::Drawing::Point(64, 92);
textBox1->Size = System::Drawing::Size(66, 12);
textBox1->Text=s0;
//Form1->Controls->Add(Form1->textBox1);
UpdateWindow(hWnd);*/
Sleep(1000);
if ( flag==false)
break;
}
}
public ref class Form1 : public System::Windows::Forms::Form
{
//unsigned long * ThreadID;
HANDLE hThread;
public:
Form1(void)
{
InitializeComponent();
//
//TODO: 在此处添加构造函数代码
//
}
protected:
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::Button^ button2;
public: System::Windows::Forms::TextBox^ textBox2;
private:
private:
/// <summary>
/// 必需的设计器变量。
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(206, 82);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(57, 24);
this->button1->TabIndex = 0;
this->button1->Text = L"开始";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::start_click);
//
// button2
//
this->button2->Location = System::Drawing::Point(206, 174);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(57, 24);
this->button2->TabIndex = 1;
this->button2->Text = L"停止";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::stop_click);
//
// textBox2
//
this->textBox2->Font = (gcnew System::Drawing::Font(L"宋体", 16, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(134)));
this->textBox2->Location = System::Drawing::Point(64, 122);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(66, 32);
this->textBox2->TabIndex = 3;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 296);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"多线程1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void start_click(System::Object^ sender, System::EventArgs^ e)
{
int ThreadID;
hThread=CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)ThreadFunc,
NULL,
0,
(LPDWORD)&ThreadID);
//GetDlgItem(button1)->EnableWindow(FALSE);
//GetDlgItem(button2)->EnableWindow(TRUE);
}
private: System::Void stop_click(System::Object^ sender, System::EventArgs^ e) {
flag=false;
// GetDlgItem(button1)->EnableWindow(TRUE);
//GetDlgItem(button2)->EnableWindow(FALSE);
}
};
}
vc++ 多线程
[解决办法]
托管c++ 最好用System::Threading::Thread
用api createthread 涉及到托管与非托管的交互可能会有错误