ADD: Show progress form when connect network drive

This commit is contained in:
Alexander Koblov 2022-12-13 21:55:39 +03:00
commit cd52866a71

View file

@ -5,7 +5,8 @@ unit uNetworkThread;
interface
uses
Classes, SysUtils, SyncObjs, JwaWinNetWk, Windows, Forms, Graphics, uDrive;
Classes, SysUtils, SyncObjs, JwaWinNetWk, Windows, Forms, Graphics, Dialogs,
StdCtrls, ComCtrls, Buttons, uDrive, uOSForms;
type
@ -18,6 +19,19 @@ type
destructor Destroy; override;
end;
{ TNetworkForm }
TNetworkForm = class(TModalDialog)
lblPrompt: TLabel;
btnAbort: TBitBtn;
pbConnect: TProgressBar;
private
FThread: TThread;
public
constructor Create(TheOwner: TComponent; AThread: TThread; APath: PWideChar); reintroduce;
procedure ExecuteModal; override;
end;
{ TNetworkThread }
TNetworkThread = class(TThread)
@ -31,7 +45,8 @@ type
constructor Create(lpLocalName, lpRemoteName: LPWSTR; dwType: DWORD); reintroduce;
destructor Destroy; override;
public
class function Connect(lpLocalName, lpRemoteName: LPWSTR; dwType: DWORD; CheckOperationState: TThreadMethod = nil): Integer;
class function Connect(lpLocalName, lpRemoteName: LPWSTR; dwType: DWORD): Integer; overload;
class function Connect(lpLocalName, lpRemoteName: LPWSTR; dwType: DWORD; CheckOperationState: TThreadMethod): Integer; overload;
end;
{ TNetworkDriveLoader }
@ -51,7 +66,7 @@ type
implementation
uses
uMyWindows, uPixMapManager;
Math, InterfaceBase, Controls, fMain, uMyWindows, uPixMapManager, uLng;
{ TDriveIcon }
@ -86,6 +101,78 @@ begin
FreeOnTerminate:= True;
end;
{ TNetworkForm }
constructor TNetworkForm.Create(TheOwner: TComponent; AThread: TThread;
APath: PWideChar);
begin
FThread:= AThread;
inherited CreateNew(TheOwner);
AutoSize:= True;
BorderStyle:= bsDialog;
Caption:= Application.Title;
Position:= poOwnerFormCenter;
ChildSizing.TopBottomSpacing:= 6;
ChildSizing.LeftRightSpacing:= 6;
lblPrompt := TLabel.Create(Self);
with lblPrompt do
begin
Parent:= Self;
AutoSize:= True;
Caption:= rsOperWaitingForConnection + ' ' + UTF8Encode(UnicodeString(APath));
end;
pbConnect:= TProgressBar.Create(Self);
with pbConnect do
begin
Parent:= Self;
Style:= pbstMarquee;
AnchorToNeighbour(akTop, 6, lblPrompt);
Constraints.MinWidth:= Math.Max(280, Screen.Width div 4);
end;
btnAbort:= TBitBtn.Create(Self);
with btnAbort do
begin
Parent:= Self;
Kind:= bkAbort;
Default:= True;
Cancel:= True;
AutoSize:= True;
AnchorHorizontalCenterTo(Self);
AnchorToNeighbour(akTop, 12, pbConnect);
end;
end;
procedure TNetworkForm.ExecuteModal;
begin
repeat
WidgetSet.AppProcessMessages;
if Application.Terminated then
begin
ModalResult:= mrCancel;
end;
if ModalResult <> 0 then
begin
CloseModal;
if ModalResult <> 0 then Break;
end;
with TNetworkThread(FThread) do
begin
if (FWaitConnect.WaitFor(1) <> wrTimeout) then
begin
ModalResult:= mrOK;
Break;
end;
end;
Application.Idle(True);
until False;
end;
{ TNetworkThread }
procedure TNetworkThread.Execute;
@ -122,6 +209,42 @@ begin
inherited Destroy;
end;
class function TNetworkThread.Connect(lpLocalName, lpRemoteName: LPWSTR;
dwType: DWORD): Integer;
var
AStartTime: QWord;
AThread: TNetworkThread;
begin
AThread:= TNetworkThread.Create(lpLocalName, lpRemoteName, dwType);
with AThread do
begin
Start;
AStartTime:= GetTickCount64;
try
while True do
begin
if (GetTickCount64 - AStartTime > 3000) then
begin
with TNetworkForm.Create(frmMain, AThread, lpRemoteName) do
try
if (ShowModal = mrOK) then
Exit(ReturnValue)
else begin
Exit(ERROR_CANCELLED);
end;
finally
Free;
end;
end;
if (GetAsyncKeyStateEx(VK_ESCAPE)) then Exit(ERROR_CANCELLED);
if (FWaitConnect.WaitFor(1) <> wrTimeout) then Exit(ReturnValue);
end;
finally
FWaitFinish.SetEvent;
end;
end;
end;
class function TNetworkThread.Connect(lpLocalName, lpRemoteName: LPWSTR;
dwType: DWORD; CheckOperationState: TThreadMethod): Integer;
begin