delphi 中使用WaitForMultipleObjects等待线程执行,再执行后续代码

unit1

[delphi]  view plain  copy
  1. unit Unit1;  
  2.   
  3. interface  
  4.   
  5. uses  
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  7.   Dialogs, StdCtrls;  
  8.   
  9. type  
  10.   TForm1 = class(TForm)  
  11.     btn1: TButton;  
  12.     mmo1: TMemo;  
  13.     procedure btn1Click(Sender: TObject);  
  14.   private  
  15.     { Private declarations }  
  16.   public  
  17.     { Public declarations }  
  18.   end;  
  19.   
  20. var  
  21.   Form1: TForm1;  
  22.   
  23.     procedure ThreadTest;stdcall;  
  24. implementation  
  25.   
  26. uses Unit2;  
  27.   
  28. {$R *.dfm}  
  29.   
  30.   
  31. procedure ThreadTest;stdcall;  
  32. var  
  33.     Handles:TWOHandleArray;  
  34.     //Handle:THandle;  
  35.     Test:TTestThread;  
  36.     i:Integer;  
  37. begin  
  38.     for i := 0 to 10 do  
  39.     begin  
  40.         Test := TTestThread.Create(False);  
  41.         Handles[i] := Test.Handle;  
  42.     end;  
  43.     WaitForMultipleObjects( 11, @Handles, True, INFINITE );  
  44.     Form1.mmo1.Lines.Add( '123' );  
  45. end;  
  46.   
  47.   
  48. procedure TForm1.btn1Click(Sender: TObject);  
  49. var  
  50.     ID:Cardinal;  
  51. begin  
  52.     CreateThread( nil0, @ThreadTest, nil0, ID );  
  53. end;  
  54.   
  55.   
  56.   
  57. end.  


 

unit2

[delphi]  view plain  copy
  1. unit Unit2;  
  2.   
  3. interface  
  4.   
  5. uses  
  6.   Classes;  
  7.   
  8. type  
  9.   TTestThread = class(TThread)  
  10.   private  
  11.     { Private declarations }  
  12.   protected  
  13.     procedure Execute; override;  
  14.   end;  
  15.   
  16. implementation  
  17.   
  18. uses Unit1;  
  19.   
  20.   
  21. procedure TTestThread.Execute;  
  22. begin  
  23.   { Place thread code here }  
  24.   { Place thread code here }  
  25.   //FreeOnTerminate := False;  
  26.   form1.mmo1.Lines.Add( 'ok' );  
  27.   
  28. end;  
  29.   
  30. end.  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`WaitForMultipleObjects` 函数可以用于等待多个对象(如线程、事件、互斥体等)的任意一个对象被信号激发。该函数会阻塞当前线程直到有一个对象被激发或者超时。 以下是一个使用 `WaitForMultipleObjects` 函数等待多个线执行完毕的示例代码: ```c #include <windows.h> #include <stdio.h> #define THREAD_COUNT 3 DWORD WINAPI thread_func(LPVOID lpParam) { int id = (int)lpParam; printf("Thread %d started\n", id); // 模拟耗时操作 for (int i = 0; i < 1000000000; i++); printf("Thread %d finished\n", id); return 0; } int main() { HANDLE hThreads[THREAD_COUNT]; for (int i = 0; i < THREAD_COUNT; i++) { hThreads[i] = CreateThread(NULL, 0, thread_func, (LPVOID)i, 0, NULL); if (hThreads[i] == NULL) { printf("Failed to create thread %d\n", i); return 1; } } DWORD waitResult = WaitForMultipleObjects(THREAD_COUNT, hThreads, TRUE, INFINITE); if (waitResult == WAIT_FAILED) { printf("WaitForMultipleObjects failed\n"); return 1; } printf("All threads finished\n"); for (int i = 0; i < THREAD_COUNT; i++) { CloseHandle(hThreads[i]); } return 0; } ``` 这段代码创建了 3 个线程,并使用 `WaitForMultipleObjects` 函数等待所有线执行完毕。其第一个参数是对象个数,第二个参数是对象数组,第三个参数指定是否等待所有对象都被激发,第四个参数指定超时时间。在本例,第三个参数为 `TRUE` 表示等待所有对象都被激发。当等待函数返回时,可以判断返回值以确定哪个对象被激发。需要注意的是,等待函数返回时需要关闭对象句柄,否则可能会导致内存泄漏。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值