- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下线程代码,第一次就正确执行。之后,我时不时地在线程的 Execute 方法上收到 AV,例如
Debug Output: TProcesses.Execute Access violation at address 00409C8C in module 'ListenOutputDebugString.exe'. Read of address 08070610 Process ListenOutputDebugString.exe (740)
我不知道是什么生成了这个 AV...
unit Unit3;
interface
uses
Classes,
StdCtrls,
Windows,
ExtCtrls,
SysUtils,
Variants,
JvExGrids,
JvStringGrid;
type
TProcesses = class(TThread)
private
{ Private declarations }
FTimer : TTimer;
FGrid : TJvStringGrid;
FJobFinished : Boolean;
procedure OverrideOnTerminate(Sender: TObject);
procedure DoShowData;
procedure DoShowErrors;
procedure OverrideOnTimer(Sender: TObject);
protected
procedure Execute; override;
public
constructor Create(aGrid : TJvStringGrid);overload;
end;
implementation
{TProcesses }
var SharedMessage : String;
ErrsMess : String;
lp : Integer;
constructor TProcesses.Create(aGrid : TJvStringGrid);
begin
FreeOnTerminate := True;
FTimer := TTimer.Create(nil);
FTimer.OnTimer := OverrideOnTerminate;
FTimer.OnTimer := OverrideOnTimer;
FTimer.Interval := 10000;
FGrid := aGrid;
inherited Create(false);
FTimer.Enabled := true;
FJobFinished := true;
end;
procedure TProcesses.DoShowData;
var wStrList : TStringList;
wi,wj : Integer;
begin
// FMemo.Lines.Clear;
for wi := 1 to FGrid.RowCount-1 do
for wj := 0 to FGrid.ColCount-1 do
FGrid.Cells[wj,wi] := '';
try
try
wStrList := TStringList.Create;
wStrList.Delimiter := ';';
wStrList.StrictDelimiter := true;
wStrList.DelimitedText := SharedMessage;
// outputdebugstring(PChar('Processes list '+SharedMessage));
FGrid.RowCount := wStrList.Count div 4;
for wi := 0 to wStrList.Count-1 do
FGrid.Cells[(wi mod 4), (wi div 4)+1] := wStrList[wi];
Except on e:Exception do
OutputDebugString(Pchar('TProcesses.DoShowData '+e.Message));
end;
finally
FreeAndNil(wStrList);
end;
end;
procedure TProcesses.DoShowErrors;
begin
// FMemo.Lines.Add('Error '+ ErrsMess);
FGrid.Cells[1,1] := 'Error '+ ErrsMess;
ErrsMess := '';
end;
procedure TProcesses.Execute;
function EnumProcess(hHwnd: HWND; lParam : integer): boolean; stdcall;
var
pPid : DWORD;
title, ClassName : string;
begin
//if the returned value in null the
//callback has failed, so set to false and exit.
if (hHwnd=NULL) then
begin
result := false;
end
else
begin
//additional functions to get more
//information about a process.
//get the Process Identification number.
GetWindowThreadProcessId(hHwnd,pPid);
//set a memory area to receive
//the process class name
SetLength(ClassName, 255);
//get the class name and reset the
//memory area to the size of the name
SetLength(ClassName,
GetClassName(hHwnd,
PChar(className),
Length(className)));
SetLength(title, 255);
//get the process title; usually displayed
//on the top bar in visible process
SetLength(title, GetWindowText(hHwnd, PChar(title), Length(title)));
//Display the process information
//by adding it to a list box
SharedMessage := SharedMessage +
(className +' ;'+//'Class Name = ' +
title +' ;'+//'; Title = ' +
IntToStr(hHwnd) +' ;'+ //'; HWND = ' +
IntToStr(pPid))+' ;'//'; Pid = ' +
;// +#13#10;
Result := true;
end;
end;
begin
if FJobFinished then
begin
try
FJobFinished := false;
//define the tag flag
lp := 0; //globally declared integer
//call the windows function with the address
//of handling function and show an error message if it fails
SharedMessage := '';
if EnumWindows(@EnumProcess,lp) = false then
begin
ErrsMess := SysErrorMessage(GetLastError);
Synchronize(DoShowErrors);
end
else
Synchronize(DoShowData);
FJobFinished := true;
Except on e:Exception do
OutputDebugString(Pchar('TProcesses.Execute '+e.Message));
end;
end
end;
procedure TProcesses.OverrideOnTerminate(Sender: TObject);
begin
FTimer.Enabled := false;
FreeAndNil(FTimer);
end;
procedure TProcesses.OverrideOnTimer(Sender: TObject);
begin
Self.Execute;
end;
end.
最佳答案
我永远不会在线程中使用计时器。相反,我会创建一个系统事件,并使用 WaitForSingleObject
在线程的执行循环中等待指定的时间。功能。此函数将等待,直到指定对象(在本例中为事件)处于有信号状态或超时间隔已过。
原理很简单,您将在无信号状态下创建事件并将其保持在该状态直到线程将要终止。这将导致 WaitForSingleObject
每次在函数调用中指定的时间内阻塞线程执行循环时,函数都会超时。一旦您决定终止线程,您只需设置线程的终止标志(您应该尽可能多地询问该标志)并将该事件设置为有信号状态,这会导致 WaitForSingleObject
函数立即返回。
下面是一个模拟线程计时器的示例(2 秒间隔 = 2000 毫秒用作 WaitForSingleObject
函数调用中的第二个参数):
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TTimerThread = class(TThread)
private
FTickEvent: THandle;
protected
procedure Execute; override;
public
constructor Create(CreateSuspended: Boolean);
destructor Destroy; override;
procedure FinishThreadExecution;
end;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FTimerThread: TTimerThread;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
ReportMemoryLeaksOnShutdown := True;
FTimerThread := TTimerThread.Create(False);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FTimerThread.FinishThreadExecution;
end;
{ TTimerThread }
constructor TTimerThread.Create(CreateSuspended: Boolean);
begin
inherited;
FreeOnTerminate := True;
FTickEvent := CreateEvent(nil, True, False, nil);
end;
destructor TTimerThread.Destroy;
begin
CloseHandle(FTickEvent);
inherited;
end;
procedure TTimerThread.FinishThreadExecution;
begin
Terminate;
SetEvent(FTickEvent);
end;
procedure TTimerThread.Execute;
begin
while not Terminated do
begin
if WaitForSingleObject(FTickEvent, 2000) = WAIT_TIMEOUT then
begin
Synchronize(procedure
begin
Form1.Tag := Form1.Tag + 1;
Form1.Caption := IntToStr(Form1.Tag);
end
);
end;
end;
end;
end.
关于multithreading - Delphi - 线程内定时器生成AV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11244848/
哪种定时器在性能方面更适合使用? Jquery 计时器或 Javascript 计时器。 具有计时器的页面没有任何 Jquery 代码。 谢谢 最佳答案 在仅使用计时器的页面上引用完整的 JQuery
R 语言有没有简单的方法来设置定时器功能?计时器函数是指位于 session 后台并每隔一段时间执行一次的函数。 干杯! 最佳答案 tcltk2 包中有 tclTaskSchedule 函数(和其
我想在点击发生后调用 setTimeout()。 如果用户在 300ms 过去之前再次点击,我想停止那个计时器,触发另一个函数并重新启动原来的计时器。 我知道 setTimeout() 但我不确定如何
请参阅下面的代码...它会在页面加载 + 8 秒后更改图像,然后继续每 1 秒更改一次。 setInterval(function(){ setTimeout(function(){
我正在尝试使用计时器来安排应用程序中的重复事件。但是,我希望能够实时调整事件触发的时间段(根据用户输入)。 例如: public class HelperTimer extends TimerTask
setTimeout()--用于指定在一段特定的时间后执行某段程序。 格式: [定时
setTimeout 和 clearTimeout 复制代码 代码如下: var obj = setTimeout(cb, ms); setTim
if(e.getSource()==continuous) { TimerTask task = new TimerTask() { public void run()
请谁能告诉我如何在 iPhone 的 cocos2d 中实现启动游戏的倒计时器。 我的意思是,按下“播放”时,一个新场景会出现,显示数字“3”、“2”、“1”,然后显示“GO!”一词。 最佳答案 来自
我正在制作一个计时器,而且效果很好。唯一的问题是,每过一秒,它就会在新行中打印剩余的时间(以秒为单位)。我该如何做到这一点,而不是打印一个新行,而只是改变当前行中显示的内容? 这就是我所拥有的...
这个问题在这里已经有了答案: Lua Program Delay (2 个答案) 关闭 7 年前。 我目前使用 Corona SDK,Lua 作为我的主要语言。我在使用此代码时遇到问题 - 当我运行
我正在制作一个计时器,而且效果很好。唯一的问题是,每过一秒,它就会在新行中打印剩余的时间(以秒为单位)。我该如何做到这一点,而不是打印一个新行,而只是改变当前行中显示的内容? 这就是我所拥有的...
到目前为止,我使用的每种方法都只是暂时卡住我的程序,但我希望游戏继续运行,我只希望盾牌 boolean 值在 X 时间内为 true,然后在时间到期后返回 false,有吗有办法做到这一点吗?谢谢。
我需要创建一个异步线程,它运行一次,延迟 2 分钟,并且可以随时终止。我看到了几种可能的解决方案: ScheduledExecutorService 和 FutureTask 允许我中断正在运行的任务
我开发了一个简单的应用程序并使用了计时器,但如果我多次运行计时器,计时器会丢弃此异常:线程“AWT-EventQueue-0”java.lang.IllegalStateException 中的异常:
我正在实现一个计时器: timer = new Timer(); timer.schedule(new TimerTask() { @Overr
我有一个有点复杂的 iOS 用户界面,我需要每秒重新加载 UICollectionView 的特定单元格以显示时间(有点像复杂的秒表),我还需要每秒做一些其他事情在这次通话中。 问题的第 1 部分 我
我一直在研究可用于 QueryPerformanceCounter()/QueryPerformanceFrequency() 的不同类型的计时器,在进一步研究之后,我发现了一个使用计时器类的例子..
我正在尝试以微秒为单位做一个计时器,但它不太管用。 #include #include #include using namespace std; int main () { struc
假设我有一个整数数组 int timeouts [] = {1000 , 2000 , 3000 , 3500}; 我想创建一个计时器,最多计时 3.5 秒,并在毫秒计数等于数组元素之一时调用相同的函
我是一名优秀的程序员,十分优秀!