我正在制作一个使用线程的Windows窗体应用程序,线程正在运行并且它可以工作,但我希望我的线程能够访问表单组件。 就像在线程函数中看不到的BtnMain一样。 我怎么解决这个问题?这是我的代码
#pragma once
namespace winAppSocket {
struct struttura{
char c;
int num;
} mystruct;
UINT ServerThread(LPVOID pParam)
{
printf("Starting up TCP server\r\n");
SOCKET server;
WSADATA wsaData;
sockaddr_in local;
int wsaret=WSAStartup(0x101,&wsaData);
if(wsaret!=0)
{
return 0;
}
local.sin_family=AF_INET;
local.sin_addr.S_un.S_addr=INADDR_ANY;
local.sin_port=htons((u_short)20248);
server=socket(AF_INET,SOCK_STREAM,0);
if(server==INVALID_SOCKET)
{
return 0;
}
if(bind(server,(sockaddr*)&local,sizeof(local))!=0)
{
return 0;
}
if(listen(server,10)!=0)
{
return 0;
}
SOCKET client;
sockaddr_in from;
int fromlen=sizeof(from);
int buffer;
char buff;
while(true)
{
client=accept(server,(struct sockaddr*)&from,&fromlen);
//printf("pppp\n");
//con la struct
int numByte=recv(client,(char*) &mystruct, (int) sizeof(mystruct), 0);
//printf("%c\t%d\n", mystruct.c, mystruct.num);
//ricevere un char funziona
//int numByte=recv(client, &buff, (int) sizeof(buff), 0);
//printf("%c\t%d\n", buff, numByte);
//int numByte=recv(client, buffer, (int) strlen(buffer), 0);
//questo funziona
//int numByte=recv(client, (char*)&buffer, (int) sizeof(buffer), 0);
//printf("%d\t%d\n", buffer, numByte);
//printf("Connection from %s\n", inet_ntoa(from.sin_addr));
closesocket(client);
}
closesocket(server);
WSACleanup();
return 0;
}
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
public: System::Windows::Forms::Button^ BtnMain;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->BtnMain = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// BtnMain
//
this->BtnMain->Location = System::Drawing::Point(139, 21);
this->BtnMain->Name = L"BtnMain";
this->BtnMain->Size = System::Drawing::Size(120, 36);
this->BtnMain->TabIndex = 0;
this->BtnMain->Text = L"Start Main";
this->BtnMain->UseVisualStyleBackColor = true;
this->BtnMain->Click += gcnew System::EventHandler(this, &Form1::BtnMain_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Controls->Add(this->BtnMain);
this->Name = L"Form1";
this->Text = L"Package Analyzer";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void BtnMain_Click(System::Object^ sender, System::EventArgs^ e) {
AfxBeginThread(ServerThread,0);
//while(_getch()!=27);
}
};
}
答案 0 :(得分:2)
为什么在使用托管C ++时不使用.NET线程?使事情变得更容易......但是,当您从另一个线程访问控件时,您将获得异常 - 您必须使用Invoke
来更新线程中的控件。
修改强>
好吧,我只能写C#,但我相信你可以轻松地将它转换为C ++:
private void BtnMain_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(ThreadMethod));
t.Start();
}
private void ThreadMethod(object state)
{
// state would be set if you used ParameterizedThreadStart and t.Start(...) above
// DO YOUR STUFF HERE
...
// Set text of label on form. You must use this.Invoke, as otherwise
// you'll run into an exception for changing controls from a different thread
this.Invoke((Action)delegate()
{
label1.Text = "Hello";
});
}
但是在你的情况下甚至不需要这样做 - 使用.NET TCP类!
您应避免使用非托管代码进行混合管理,而应学会使用框架的功能。