gpt4 book ai didi

multithreading - 在TThread执行中引发异常?

转载 作者:行者123 更新时间:2023-12-03 14:44:41 25 4
gpt4 key购买 nike

我只是意识到我的异常没有在我的线程中显示给用户!

最初,我在线程中使用了此方法来引发异常,该异常不起作用:

except on E:Exception do
begin
raise Exception.Create('Error: ' + E.Message);
end;

IDE向我显示了异常,但我的应用程序却没有!

我到处寻找解决方案,这是我发现的结果:

Delphi thread exception mechanism

http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_22039681.html

这些都不对我有用。

这是我的线程单元:
unit uCheckForUpdateThread;

interface

uses
Windows, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP, GlobalFuncs, Classes, HtmlExtractor, SysUtils, Forms;

type
TUpdaterThread = class(TThread)
private
FileGrabber : THtmlExtractor;
HTTP : TIdHttp;
AppMajor,
AppMinor,
AppRelease : Integer;
UpdateText : string;
VersionStr : string;
ExceptionText : string;
FException: Exception;
procedure DoHandleException;
procedure SyncUpdateLbl;
procedure SyncFinalize;
public
constructor Create;

protected
procedure HandleException; virtual;

procedure Execute; override;
end;

implementation

uses
uMain;

{ TUpdaterThread }

constructor TUpdaterThread.Create;
begin
inherited Create(False);
end;

procedure TUpdaterThread.Execute;
begin
inherited;
FreeOnTerminate := True;

if Terminated then
Exit;

FileGrabber := THtmlExtractor.Create;
HTTP := TIdHTTP.Create(nil);
try
try
FileGrabber.Grab('http://jeffijoe.com/xSky/Updates/CheckForUpdates.php');
except on E: Exception do
begin
UpdateText := 'Error while updating xSky!';
ExceptionText := 'Error: Cannot find remote file! Please restart xSky and try again! Also, make sure you are connected to the Internet, and that your Firewall is not blocking xSky!';
HandleException;
end;
end;

try
AppMajor := StrToInt(FileGrabber.ExtractValue('AppMajor[', ']'));
AppMinor := StrToInt(FileGrabber.ExtractValue('AppMinor[', ']'));
AppRelease := StrToInt(FileGrabber.ExtractValue('AppRelease[[', ']'));
except on E:Exception do
begin
HandleException;
end;
end;

if (APP_VER_MAJOR < AppMajor) or (APP_VER_MINOR < AppMinor) or (APP_VER_RELEASE < AppRelease) then
begin
VersionStr := Format('%d.%d.%d', [AppMajor, AppMinor, AppRelease]);
UpdateText := 'Downloading Version ' + VersionStr;
Synchronize(SyncUpdateLbl);
end;

finally
FileGrabber.Free;
HTTP.Free;
end;
Synchronize(SyncFinalize);
end;

procedure TUpdaterThread.SyncFinalize;
begin
DoTransition(frmMain.TransSearcher3, frmMain.gbLogin, True, 500);
end;

procedure TUpdaterThread.SyncUpdateLbl;
begin
frmMain.lblCheckingForUpdates.Caption := UpdateText;
end;

procedure TUpdaterThread.HandleException;
begin
FException := Exception(ExceptObject);
try
Synchronize(DoHandleException);
finally
FException := nil;
end;
end;

procedure TUpdaterThread.DoHandleException;
begin
Application.ShowException(FException);
end;

end.

如果您需要更多信息,请告诉我。

再次:IDE捕获了所有异常,但是我的程序没有显示它们。

编辑:最终,这是Cosmin的解决方案起作用的-最初之所以不起作用的原因是,因为我没有添加ErrMsg变量,而是我只是将变量中包含的任何内容放入Synchronize中,这将不起作用,但是我不知道为什么。当我没有其他想法时,我就意识到了这一点,然后我就弄乱了解决方案。

和往常一样,这个笑话在我身上。 = P

最佳答案

这是我对此问题的非常简短的“总结”。它仅适用于Delphi 2010+(因为该版本引入了匿名方法)。与已经发布的更复杂的方法不同,我的仅显示错误消息,仅此而已。

procedure TErrThread.Execute;
var ErrMsg: string;
begin
try
raise Exception.Create('Demonstration purposes exception');
except on E:Exception do
begin
ErrMsg := E.ClassName + ' with message ' + E.Message;
// The following could be all written on a single line to be more copy-paste friendly
Synchronize(
procedure
begin
ShowMessage(ErrMsg);
end
);
end;
end;
end;

关于multithreading - 在TThread执行中引发异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5442978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com