多线程 – 在Windows 7安装中是否提供Delphi 2009等待链式遍历?
根据“Delphi 2009新功能”,有一个新的调试器功能称为“等待链遍历”.它特别说:“已经添加了一个等待链穿越功能来帮助您解决线程争用或死锁问题.该功能依赖于添加到
Windows Vista操作系统的工具,它向调试器提供有关应用程序线程等待状态的信息等待链的形式“.
当Windows Vista是目前的操作系统时,Delphi 2009被发布.从我的经验来看,Vista中引入的大部分功能也可以在Windows 7中使用.但是,我在Delphi 2009中,通过Delphi XE安装(在Windows 7上都没有)看到这个功能. 我正在调试器的“线程”窗格中查找此功能. 我正在寻找正确的地方的等待链遍历? David M提供了一个很好而明确的答案,但是我仍然没有在“线程”窗格中获得“等待链”列.这是一些代码. 主要形式: unit Main; interface uses Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls,SyncObjs,RanThread; type TForm1 = class(TForm) ListBox1: TListBox; Button1: TButton; Button2: TButton; Label1: TLabel; procedure Button2Click(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } procedure ThreadDone(Sender: TObject); end; var Form1: TForm1; RanGenThread: TRandomizer; implementation uses LoadThread; {$R *.dfm} { TForm1 } procedure TForm1.ThreadDone(Sender: TObject); begin RanGenThread.Free; end; procedure TForm1.Button2Click(Sender: TObject); begin ListBox1.Sorted := True; end; procedure TForm1.Button1Click(Sender: TObject); var Thread: TLoader; begin ListBox1.Items.Clear; ListBox1.Sorted := False; RanGenThread := TRandomizer.Create(True); RanGenThread.ArraySize := 1000; Thread := TLoader.Create(True); with Thread do begin RanGenThread.WaitThread := Thread; FreeOnTerminate := True; OnTerminate := ThreadDone; WaitForThread := RanGenThread; //Use Start in Delphi 2010 or later,where Resume is deprecated Resume; end; RanGenThread.Resume; end; initialization Randomize; end. TRandomizer: unit RanThread; interface uses Classes,Math,SyncObjs; type TRandomizer = class(TThread) private { Private declarations } FArraySize: Integer; protected procedure Execute; override; public WaitThread: TThread; RandNumbers: array of Integer; property ArraySize: Integer read FArraySize write FArraySize; end; implementation uses Main; procedure TRandomizer.Execute; var i: Integer; LowNum,HighNum: Integer; RandNum: Integer; begin if FArraySize = 0 then begin Exit; end; SetLength(RandNumbers,FArraySize); LowNum := Low(RandNumbers); HighNum := High(RandNumbers); //initialize the array for i := LowNum to HighNum do RandNumbers[i] := -1; // generate the random order for i := LowNum to HighNum do while True do begin RandNum := RandomRange(LowNum,HighNum + 1); if RandNumbers[RandNum] = -1 then begin RandNumbers[RandNum] := i + 1; break; end; // if end; // while WaitThread.WaitFor; end; end. TLoader: unit LoadThread; interface uses Classes,RanThread; type TLoader = class(TThread) private FWaitForThread: TRandomizer; procedure UpdateList; { Private declarations } protected procedure Execute; override; public property WaitForThread: TRandomizer read FWaitForThread write FWaitForThread; end; implementation uses Main; procedure TLoader.UpdateList; var i: Integer; begin for i := Low(FWaitForThread.RandNumbers) to High(FWaitForThread.RandNumbers) do Form1.ListBox1.Items.Add(IntToStr(FWaitForThread.RandNumbers[i])); end; procedure TLoader.Execute; begin if WaitForThread <> nil then begin FWaitForThread.WaitFor; Synchronize(UpDateList) end; end; end. 根据David M链接的等待链遍历文档,WTC可用于以下同步对象: > ALPC 我的代码等待一个线程,但它是一个TThread,而不是直接的本机线程.今天晚上,我将修改我的代码示例在等待一个Mutex时死锁,看看是否导致“等待链”列出现在“线程”窗格中. 好.最后找到时间进行下一次测试.创建一个应用程序,在启动时占用一个互斥体.创建一个使用OpenMutex获取该Mutex的句柄,然后调用WaitForsingleObject(handle,INFINITE)的工作线程. “线程”窗格中仍然没有“等待链”列. 解决方法
>是的.写一个程序与两个线程死锁.您将看到,在“线程”窗格中,右侧列(标记为“等待链”)会显示“正在等待线程12345持有的锁”.我没有完全确定确切的短语,因为我现在的程序中没有任何僵局:)如果没有看到它,请向右滚动.如果列不存在,那很奇怪 – 请在评论中说出来.
>否.等待链遍历是在Vista(和Server 2008)中引入的,并且也可以在Windows 7上使用.您可以通过阅读 MSDN documentation on wait chain traversal并查看 core functions中的一些内容,其中的所有内容都在表中的评论中. “最低支持客户端:Windows Vista”.它会说如果它是在Windows的更高版本中被删除的 – 它的措辞方式就是对任何数量的其他功能的方式.另外,一个使用Windows 7(我有Vista)的同事也有这个功能. 编辑:我在Vista机器上尝试过Delphi 2010中的示例代码(我没有2009我害怕).我点击了Button1和Button2,然后切换到Threads视图,而不会暂停程序. “线程”窗格中有一个“等待链”列,其中包含两个非主线程“被阻止等待线程11968”或“14416”的文本. 这是一个截图: 如果这个功能正常工作,那么这就是您应该在IDE中获得的东西. 当你尝试这个,你得到其他列,但不是等待链列?如果是这样,我承认我有点困惑,我认为下一步可能是联系Embarcadero或者至少在one of their forums发布,大概是this one.我查找了等待链功能需要哪些权限,但是它如果您正在使用当前用户拥有的进程,您似乎是don’t even need 我不认为你的IDE是以奇特权还是以XP兼容模式运行?编辑4:这是答案.请参阅下面的Cary的评论,该快捷方式以XP兼容模式运行. 编辑2:我以为我会做一个健全检查,并查找他们的帮助文件说什么.我在线找不到2009年的帮助,但2010年的帮助说“Wait Chain: (For Windows Vista and 7 only)”.凉.然后我看了一下XE的帮助,并说’Wait Chain: (For Windows Vista only)‘.很奇怪. 我怀疑这是一个文档错误,它应该在Windows 7上工作,因为我也碰到了这个Delphi 2009 hotfix for wait chain traversal running on Windows 7.听起来非常支持我!没有办法肯定会在最新版本的Windows的最新版本中删除这样的功能…是吗?即使他们使用的是版本,它被记录在案. 编辑3:我问我的Windows 7使用同事也试用你的示例代码.这是他得到的: 在这一点上,我承认我有点困惑.我可以说的是,在Windows 7上使用RAD Studio 2010的作品,据我所知,应该适合你.如果不是我没有更多的建议超出上面几个模糊的想法! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |