Einzelnen Beitrag anzeigen

Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#7

Re: Ping über Hostname mit timeout

  Alt 30. Sep 2008, 13:45
Warum Events verwenden? Es geht auch ohne:
Delphi-Quellcode:
program Project1;

{$APPTYPE CONSOLE}

uses
  Windows,
  Messages;

function MyThread(p: pOinter): Integer;
begin
  MessageBox(0, 'MessageBox in Thread', 'Thread', 0);
  Result := 0;
end;

var
  Thread: THandle;
  ThreadID: DWORD;
  WaitResult: DWORD;

begin
  Thread := BeginThread(nil, 0, @MyThread, nil, 0, ThreadID);
  WaitResult := WaitForSingleObject(Thread, 5000);
  case WaitResult of
    WAIT_OBJECT_0: Writeln('MessageBox closed by user');
    WAIT_TIMEOUT:
    begin
      SendMessage(FindWindow(nil, 'Thread'), WM_CLOSE, 0, 0);
      Writeln('Tinmeout');
    end;
  end;
  CloseHandle(Thread);
  Readln;
end.
Hier noch schnell meine C++ Version:
Code:
#include <windows.h>
#include <iostream>

using namespace std;

DWORD WINAPI ThreadProc( LPVOID lpParam )
{
   MessageBox(0, L"Messagebox in Thread", L"Thread", 0);
   return 0;
}

int wmain( int argc, wchar_t *argv[ ], wchar_t *envp[ ] )
{
   HANDLE hThread;
   DWORD dwThreadID;
   
   hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, &dwThreadID);
   if (hThread == NULL)
   {
      cout << GetLastError();
      CloseHandle(hThread);
      return 1;
   }

   DWORD dwWaitResult = WaitForSingleObject(hThread, 5000);
   switch (dwWaitResult)
   {
      case WAIT_OBJECT_0:
         cout << "MessageBox closed by user";
         break;
      case WAIT_TIMEOUT:
         SendMessage(FindWindow(NULL, L"Thread"), WM_CLOSE, 0, 0);
         cout << "Timeout";
         break;
   }   
   CloseHandle(hThread);
   return 0;
}
@sniper_w: Du schliesst das ThreadHandle nicht.
Michael
  Mit Zitat antworten Zitat