Multithreading in Windows Forms

TheMohawkNinja 21 Reputation points
2022-07-27T13:10:32.967+00:00

Hello,

I am trying to figure out how multithreading works in Windows Forms in C++. With console applications, it's as simple as:

#include <thread>  
  
void f(int x)  
{  
     //do stuff  
}  
int main()  
{  
     std::thread f_thread(f, 10);  
     f_thread.detach();  
     return 0;  
}`  

However, with Windows Forms, when I try:

#include <thread>  

namespace Project  
{  
     void f(int x)  
     {  
          //do stuff  
     }  
  
//Constructor code for form  
  
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)  
{  
     std::thread f_thread(f, 10);  
     f_thread.detach();  
};  
}  

I get: error C2661: 'std::thread::thread': no overloaded function takes 2 arguments

How do I invoke threads in Windows Forms?

Developer technologies Windows Forms
Developer technologies C++
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2022-07-27T13:43:31.917+00:00

    In .NET Framework you can do this:

    void f( Object^ p )  
    {  
        int x = (int)p;  
      
    }  
      
    System::Void Form1_Load( System::Object^ sender, System::EventArgs^ e )  
    {  
        using namespace System::Threading;  
      
        Thread^ t = gcnew Thread( gcnew ParameterizedThreadStart( this, &Form1::f ) );  
        t->Start( 10 );  
    }  
    

1 additional answer

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2022-07-27T13:43:59.723+00:00

    You can use system.threading.thread
    (samples in C++ at Top-Right, also at system.threading.thread.start or other)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.