моя ветка не видит windows компоненты формы - PullRequest
1 голос
/ 11 января 2012

Я делаю приложение для формы 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);
}
};
}

1 Ответ

2 голосов
/ 11 января 2012

Почему вы не используете потоки .NET при использовании управляемого C ++?Все становится намного проще ... Однако вы получите исключения при доступе к элементам управления из другого потока - вам придется использовать 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!

Следует избегать смешения управляемого с неуправляемым кодом и вместо этого учиться использовать возможности инфраструктуры.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...