Ver Mensaje Individual
  #2 (permalink)  
Antiguo 26/04/2014, 05:12
menghi
 
Fecha de Ingreso: abril-2014
Ubicación: Málaga, Andalucía, España
Mensajes: 7
Antigüedad: 10 años, 4 meses
Puntos: 2
Respuesta: Necesito un código Escaner de Puertos en C++ usando Threads

Hola!

La verdad es que hace mucho que no toco Threads en C++, pero tengo la documentacion de un proyecto antiguo donde los use, a ver si te sirve de algo:

CThread.h
Código C++:
Ver original
  1. class CThread
  2. {
  3. private:
  4.     HANDLE thandle;
  5.     long thid;
  6. public:
  7.     CThread();
  8.     ~CThread();
  9.     int Start(unsigned int (__stdcall *func)(void *), void * params);
  10.     HANDLE CThread::GetHandle();
  11.     void Resume();
  12.     void Pause();
  13.     void Stop();
  14. };

CThread.cpp
Código C++:
Ver original
  1. #include "CThread.h"
  2. CThread::CThread(){
  3.     this->thid = NULL;
  4. }
  5.  
  6. int CThread::Start(unsigned int (__stdcall * func)(void *), void * params)
  7. {
  8.     this->thandle = (HANDLE) _beginthreadex(NULL, 0, func, params, CREATE_SUSPENDED, &this->thid);
  9.  
  10.     if(!this->thandle)
  11.     {
  12.         return NULL;
  13.     }
  14.     return 1;
  15. }
  16.  
  17. HANDLE CThread::GetHandle(){ return this->thandle; }
  18.  
  19. void CThread::Resume(){ ResumeThread(this->thandle); }
  20. void CThread::Pause(){ SuspendThread(this->thandle); }
  21. void CThread::Stop(){ _endthreadex(this->thid); }
  22. CThread::~CThread(){ this->Stop(); CloseHandle((void*)this->thid); }

Y tal y como lo uso mas adelante es mas o menos de la siguiente forma:

Código C++:
Ver original
  1. unsigned int WINAPI callback(void * params)
  2. {
  3.     //Esta funcion se ejecutara durante la vida del hilo...
  4. }
  5.  
  6. void main()
  7. {
  8.     CThread * th = new CThread();
  9.     th->Start(callback, params);
  10.     th->Resume();
  11. }

Un saludo!

Última edición por menghi; 26/04/2014 a las 05:38 Razón: Agregar mas información