Bonjour à tous
Voila j’essaie de comprendre comment marche les thread en c++ avec visual studio .net.
:pfff:
HANDLE g_event;//Les threads en c++//Déclaré en public
DWORD WINAPI Thread1(LPVOID lpParam)
{//Ligne 236
//Application en écoute;
SetEvent(g_event);
return 0;
}
private: System::Void Form1_Load(System::Object * sender, System::EventArgs * e)
{
g_event=CreateEvent(NULL, 0, 0, NULL);//ligne 311
DWORD threadID;//ligne 312
CreateThread(NULL, 0, &Thread1, NULL, 0, &threadID);//ligne 313
WaitForSingleObject(g_event, INFINITE);//ligne 314
}
J’ai deux problèmes de compilation :heink: . Il me donne les problème suivants:
Form1.h(313): error C2276: ‘&’ : opération non conforme sur l’expression d’une fonction membre liée
Form1.h(236): warning C4440: redéfinition de la convention d’appel de '__clrcall ’ en '__stdcall ’ ignorée
Je vous remercie tous de vos réposes.
static DWORD WINAPI Thread1(LPVOID lpParam)
{//Ligne 236
//Application en écoute;
SetEvent(g_event);
return 0;
}
private: System::Void Form1_Load(System::Object * sender, System::EventArgs * e)
{
g_event=CreateEvent(NULL, 0, 0, NULL);//ligne 311
DWORD threadID;//ligne 312
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Thread1, NULL, 0, &threadID);//ligne 313
WaitForSingleObject(g_event, INFINITE);//ligne 314
}
Je ne fait pas de .NET donc je ne suis pas sûr du résultat :neutre:
Le “WaitForSingleObject” n’a jamais marché chez moi :sweet: J’aimerai bien que tu postes un petit mot pour me dire si cela a fonctionné dans ton appli.
Merci.
KarLKoX mercie de ta réponse mais j’ai déjà essayé cette méthode mais ca ne marche pas.
Au niveau de la ligne
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Thread1, NULL, 0, &threadID);//ligne 313
il me dit :
Form1.h(313): error C2440: ‘cast de type’ : impossible de convertir de ‘overloaded-function’ en ‘LPTHREAD_START_ROUTINE’
Bon, je viens de me renseigner, tu ne peux pas mixer du code C++ à l’ancienne dans en environnement .NET, c’est pour cela que Microsoft à créé le concept de code managé : ca te permet d’utiliser le C++ dans un context .NET.
J’ai donc faire une recherche sur la MSDN et j’ai trouvé cette fonction puis adapté ce code, ce qui donne :
#pragma once
namespace VisualNet_Project1 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Threading;
/// <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:
static void Thread1()
{
for ( int i = 0; i < 5; i++ )
Thread::Sleep( 1000 );
}
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
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->SuspendLayout();
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 262);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
Thread^ oThread = gcnew Thread( gcnew ThreadStart( &Thread1 ) );
oThread->Start();
oThread->Join();
}
};
}
La gui doit s’afficher au bout de 5 secondes.