写了一个简单的多线程程序,单击开始按钮,建立一个新线程,当
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