Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7032051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:53:02+00:00 2026-05-28T00:53:02+00:00

I’m making a windows form application that uses threads, the threads is running and

  • 0

I’m making a windows form application that uses threads, the threads is running and it works, but I want my thread to access the form components.
like BtnMain that is not seen inside the thread function.
how can I solve this?, here is my code

#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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:53:03+00:00Added an answer on May 28, 2026 at 12:53 am

    Why don’t you use .NET threads when using managed C++? Makes things much easier… You will, however, get exceptions when you access your controls from a different thread – you’ll have to use Invoke to update controls from a thread.

    EDIT
    Well, I’ can only write C#, but I’m sure you can easily convert this to 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";
        });
    }
    

    But it’s not even required to do this in your case – use .NET TCP classes!

    You should avoid mixing managed with unmanaged code and instead learn to use the abilities of the framework.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.