In the below script, an AfterInstall procedure (SetupOperation) executes after the installation of the lone file installed. If it fails, I want to abort setup, which I do by calling WizardForm.Close. The problem is, if the CurPageChanged procedure is merely declared, the the exception is never properly rethrown in SetupOperation. If I comment it out, everything works properly. There's got to be something simple I'm missing.
[Setup]
DisableDirPage=yes
DisableProgramGroupPage=yes
AppID=someuniqueid
DefaultDirName={pf}\MyCompany\ExampleApp
SetupLogging=yes
AppName=ExampleApp
AppVersion=1.2.3.4
AppVerName=ExampleApp 1.2.3.4
AppPublisher=MyCompany
OutputBaseFilename=MyCompany.ExampleApp.1234
[Files]
Source: "d:\temp\test.txt"; DestDir: "{commonappdata}\MyCompany\ExampleApp"; AfterInstall: SetupOperation
[Code]
var SharedProgressPage: TOutputProgressWizardPage;
// Called by Inno to initialize the user interface
procedure InitializeWizard();
begin
Log('InitializeWizard called');
SharedProgressPage := CreateOutputProgressPage('Installing', 'Please wait while Setup installs the ExampleApp on your computer.');
SharedProgressPage.SetProgress(0,1);
SharedProgressPage.ProgressBar.Style := npbstMarquee;
end;
// Called when the current wizard page changes
procedure CurPageChanged(CurPageID: Integer);
begin
end;
procedure SetupOperation();
begin
Log('SetupOperation called');
try
SharedProgressPage.SetText('', '');
SharedProgressPage.Show;
try
SharedProgressPage.SetText('Performing operation...', '');
RaiseException('Some weird error');
except
Log('Caught an exception; re-raising');
RaiseException(GetExceptionMessage());
finally
Log('Closing progress page');
SharedProgressPage.Hide;
end;
except
Log('An error occurred setting performing the operation: ' + GetExceptionMessage());
WizardForm.Close();
end;
end;
This might have something to do with the usage of a TOutputProgressWizardPage, but I don't see how. It seems to get shown/hidden properly.
Related
I tried to run a SELECT SQL to retrieve some data from MySql database. For this, I had created a TZQuery inside a TThreadn to avoid freezing the GUI of the application.
But, after the end of each consult (tested with 100,000+ rows, just to "force" a freeze) the memory was increasing instead of returning to the initial values (even after FreeAndNil(ZQuery) and FreeAndNil(TThread)) increasing ~2mb to each query executed (verified using Windows taskman.)
The test:
1 - Click button1 to start thread and query
2 - Wait callback
3 - Click button2 to finish and free query
{ TForm }
procedure TForm1.Button1Click(Sender: TObject);
begin
//DBQuery: TSubThreadTest;
DBQuery:= TSubThreadTest.Create(SQLStatement, true, ZCon, #onQueryCallBack);
DBQuery.Start;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
FreeAndNil(DBQuery);
end;
procedure TForm1.onQueryCallBack(fail: boolean; query: TZQuery);
begin
lLastExec.Caption:= FormatDateTime('hh:mm:ss', Now);
end;
{ TSubThreadTest }
constructor TSubThreadTest.Create(SQLStatement: string; toExec: boolean;
connector: TZConnection; EOnConclude: TSubThreadTestCallBack);
begin
inherited Create(true);
//main: TZQuery;
main:= TZQuery.Create(nil);
main.Connection:= connector;
main.SQL.Text:= SQLStatement;
isToExec:= toExec;
OnConclude:= EOnConclude;
FreeOnTerminate:= False;
end;
procedure TSubThreadTest.callback;
begin
if assigned(OnConclude) then
OnConclude(true, main);
end;
procedure TSubThreadTest.Execute;
begin
if isToExec then
main.ExecSQL
else
main.Open;
Synchronize(#callback);
FreeAndNil(main);
end;
First thread create and call: 24mb memory usage
Second thread create and call: 26,5mb memory usage
Etc...
I'll say how I reproduce the problem on lazarus.
I have a form and a datamodule using zeos to enstablish a connection with a local oracle db.
The problem born when I put some code to interlocute with the db.
Here is an example:
OracleMng.ZQuery1.SQL.Clear;
That is exactly the line going in error.
Here is the full code of the form:
unit form1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, DBGrids, StdCtrls,
datamodule2;
type
{ TLogin }
TLogin = class(TForm)
Button1: TButton;
DBGrid1: TDBGrid;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Login: TLogin;
implementation
{$R *.lfm}
{ TLogin }
procedure TLogin.Button1Click(Sender: TObject);
begin
OracleMng.ZQuery1.SQL.Clear;
end;
end.
Here is the code of the datamodule:
unit datamodule2;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, DB, ZConnection, ZDataset, ZSqlMonitor;
type
{ TOracleMng }
TOracleMng = class(TDataModule)
DataSource1: TDataSource;
ZConnection1: TZConnection;
ZQuery1: TZQuery;
private
public
end;
var
OracleMng: TOracleMng;
implementation
{$R *.lfm}
{ TOracleMng }
end.
I'm trying
if (OracleMng <> Nil) and (OracleMng.Zquery1 <> Nil) then OracleMng.ZQuery1.SQL.add('select * from help');
if (OracleMng <> Nil) and (OracleMng.Zquery1 <> Nil) then OracleMng.ZQuery1.ExecSQL;
dbgrid1.refresh;
I have no more errors but the DBGrid1 is not filled.
This is my project lpr file:
program project1;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, zcomponent, datamodule2, form1
{ you can add units after this };
{$R *.res}
begin
RequireDerivedFormResource:=True;
Application.Scaled:=True;
Application.Initialize;
Application.CreateForm(TLogin, Login);
Application.Run;
end.
The fact that the change I suggested in my comment, namely
if (OracleMng <> Nil) and (OracleMng.Zquery1 <> Nil) then
OracleMng.ZQuery1.SQL.Clear
evidently stopped you getting the SIGSEGV error suggests that your DataModule and
form are being created in the wrong order, i.e. form first. Check this out by going to
Project | View Source in the IDE. If you see something like
program MyProgram;
[...]
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TDataModule1, DataModule1);
Application.Run;
end.
they are in the wrong order, so swap the two CreateForm lines
Application.CreateForm(TDataModule1, DataModule1);
Application.CreateForm(TForm1, Form1);
With that change, you should no longer need the
if (OracleMng <> Nil) and (OracleMng.Zquery1 <> Nil) then`
Next thing: You seem to be confused about when to use
ZQuery1.ExecSQL
and
ZQuery1.Open
Open is intended for when the SQL statement you are using produces a result set, that is
a collection of records which can be viewed in a TDBGrid. The most usual way to do this
is to use a SELECT statement as in
ZQuery1.SQL.Text := 'select * from MyTable';
ZQuery1.Open;
ExecQuery is intended for use where your SQL statement performs some operation on the database
which does not involve SELECTing records. The most common SQL statements which need ExecSQL are
UPDATE
INSERT
DELETE
though there are others, for example statements which execute stored procedures on the SQL Server
(note that some stored procedures return result sets and so need Open, rather than ExecSQL).
Note that ExecSQL will clear out any records which are in the dataset (ZQuery1) so after
you need to do Open again using a suitable SQL statement
var
S : String;
begin
S := 'update MyTable set number = number +1 where id = 5';
ZQuery.SQL.Text := S;
ZQuery1.ExecSQL; // no records shown in DBGrid1 from here
S := 'select * from MyTable';
ZQuery.SQL.Text := S;
ZQuery1.Open; // records shown in DBGrid1 again
end;
Note that I do
S := 'select * from MyTable';
ZQuery.SQL.Text := S;
instead of
ZQuery1.SQL.Clear;
ZQuery1.SQL.Add('select * from myTable');
The reason for this is that it's much easier to see the whole SQL statement in the debugger by
inspecting the variable S than inspecting the ZQuery1.SQL.Text property and much easier to
see any syntax errors.
You should always Close a dataset that you've Opened once you have finished working with it as it ensures what the data on disk is up to date. if the last SQL operation was ExecSQL, you don't need to close the dataset.
If you set the query's Text property the way I do, with ZQuery1.SQL.Text, you don't need to uses Clear. In any case, it is only equivalent to doing ZQuery1.SQL.Text := '' and it does not affect the state of the dataset - it only does anything when you call ExecSQL or Open.
I'm using xampp with phpMyAdmin to manage a MySql database.
When creating a function I got a "No Return Found" error, so I stripped down the function to narrow the origin of the error I got to the simplest case where it still doesn't work.
And the error message is this:
Apparently if the RETURN statement isn't the only thing on the code I get an error.
Assuming the actual code being executed against MySQL is:
CREATE FUNCTION `Contagem`() RETURNS INT(11)
SET #c = 0;
RETURN #c;
This will fail, because you have not wrapped the function in BEGIN...END.
Try instead to write this as your function declaration:
BEGIN
SET #c = 0;
RETURN #c;
END;
Alternatively, declare the function yourself directly in MySQL:
DELIMITER $$
CREATE FUNCTION `Contagem`() RETURNS INT(11)
BEGIN
SET #c = 0;
RETURN #c;
END $$
DELIMITER ;
When reviewing this post I was reading more carefully the error message I realized the MySql code to define the function didn't have BEGIN and END in it.
I assumed when typing function code with several lines that phpMyAdmin would know how to handle it, since the code text box has support to multiple lines of code.
So, putting BEGIN at the beginning and END at the end of the code solved my problem.
I`m using Delphi XE6 and UniDAC and MySQL
I have some TUniQuery components in my DM and I want to Refresh theme repeatedly, so I put some Timers in my main form and in each timer I create a thread and pass a query to it for refreshing data :
for Example :
TUpdateThread = class(TThread)
private
FQuery : TUniQuery;
FResultHandle : THandle;
public
constructor Create(var Query : TUniQuery; ResultHandle : THandle);
protected
procedure Execute; override;
end;
constructor TUpdateThread.Create(var Query: TUniQuery; ResultHandle : THandle);
begin
inherited Create;
Suspend;
FQuery := Query;
FResultHandle := ResultHandle;
FreeOnTerminate := True;
Resume;
end;
procedure TUpdateThread.Execute;
var
Msg : String;
B : Boolean;
begin
try
B := True;
try
FQuery.Refresh;
except
on E:Exception do
begin
B := False;
Msg := 'Error : ' + #13 + E.Message;
SendMessage(FResultHandle, MSG_UPDATERESULT, 2, Integer(Msg));
end;
end;
finally
if B = True then
SendMessage(FResultHandle, MSG_UPDATERESULT, 1, 1);
Terminate;
end;
end;
Sometimes it`s done successfully but many times I got some errors such as AVs or "Net Pack Header ... " error
or sometimes I have problem in my Grids ( Ehlib DBGrid ) such as error in drawing rows or ... ( specially when I use DisableControls and EnableControls )
All of Queries have same connection , I think each Thread should have his own connection, because of all timers intervals are same , I suggest sometimes refreshing queries interrupts each others
In fact, my database is in a VPS server and there is some client applications , I want to have Live-Tables in Clients and update theme repeatedly
What is the best way to achieve that ?
how I should update my Tables without application hangs !
there is some components as TThreadTimer ( or ... ) , is theme useful for this situation ?!
thanks ...
The first issue is here :
constructor TUpdateThread.Create(var Query: TUniQuery; ResultHandle : THandle);
begin
inherited Create; // Create with no arguments
Suspend; // means CreateSuspended = false
FQuery := Query;
FResultHandle := ResultHandle;
FreeOnTerminate := True;
Resume;
end;
Here you create the thread with the default constructor (CreateSuspended = false) where the thread begins running immediately. You call suspend (which is deprecated and should not be used) immediately, but this is still a race condition since your thread may or may not start trying to Refresh your query before you've assigned it. To create the thread in a suspended state use the overload constructor of
inherited Create(true);
Resume is also deprecated. Instead you should use Start;.
Further, you're passing in a TUniQuery to this thread's constructor. We can assume, I imagine, that this query has affinity to the main thread - this is to say that it is (perhaps) a visual component on a form, has databindings to visual components, or is otherwise interacted with by the user or user interface.
The answer, if so, is that you simply cannot do this - a thread cannot modify an object with affinity to another thread. Your interface may be in the middle of retrieving records from the query when the background thread, for example, is simultaneously destroying them in preparation to refresh the query contents. Naturally this will cause all sorts of problems.
The simple solution is to use a regular timer and refresh synchronously on the main thread. If this takes too long then you need to consider a different strategy altogether. We don't really have sufficient information to suggest much further. If network access and I/O is the bottleneck then you might consider asynchronously refreshing to a separate query object owned by the thread, then synchronously assign it to your view components.
I have a database that I need to query over and over as fast as possible. My queries execute pretty quickly, but there seems to be some additional lag.
I have a feeling that this lag is due to the fact that I am initiating and de-initiating a connection the connection each time. Is there a way to avoid this?
I am not using libmysql (at least, not directly). I am using the "mysql50" package in Lazarus/FreePascal (similar to delphi), which in turn uses libmysql ( I think ).
I would really appreciate if someone took a look at my code and pointed out (or maybe even fixed ) some inefficiencies.
The purpose of this library is to pass along a query sent from MQL4 (a propitiatory C-like language for the financial exchange market), and return a single row from my MYSQL database (to which it connects through a pipe).
{$CALLING STDCALL}
library D1Query;
{$mode objfpc}{$H+}
uses
cmem,
Windows,
SysUtils,
profs_win32exceptiontrap,
mysql50;
var
sock: PMYSQL;
qmysql: st_mysql;
type
VArray = array[0..100] of Double;
PArray = ^VArray;
procedure InitSQL; stdcall;
begin
mysql_init(PMySQL(#qmysql));
sock :=
mysql_real_connect(PMysql(#qmysql), '.', 'root', 'password', 'data', 3306, 'mysql', CLIENT_MULTI_STATEMENTS);
if sock = nil then
begin
OutputDebugString(PChar(' Couldn''t connect to MySQL.'));
OutputDebugString(PChar(mysql_error(#qmysql)));
halt(1);
end;
end;
procedure DeInitSQL; stdcall;
begin
mysql_close(sock);
end;
function SQL_Query(QRY: PChar; output: PArray): integer; stdcall;
var
rowbuf: MYSQL_ROW;
recbuf: PMYSQL_RES;
i: integer;
nfields: LongWord;
begin
InitSQL();
if (mysql_query(sock, QRY) < 0) then
begin
OutputDebugString(PChar(' Query failed '));
OutputDebugString(PChar(' ' + mysql_error(sock)));
end;
recbuf := mysql_store_result(sock);
nfields := mysql_num_fields(recbuf);
rowbuf := mysql_fetch_row(recbuf);
if (rowbuf <> nil) then
begin
for i:=0 to nfields-1 do
output^[i] := StrToFloatDef(rowbuf[i], -666);
end;
mysql_free_result(recbuf);
DeInitSQL();
Result := i;
end;
exports
SQL_Query,
InitSQL,
DeInitSQL;
begin
end.
You could use Initialization and Finalization blocks to handle setting up and tearing down the SQL connection. That way you remove the overhead of connection setup from each query that you execute. You can find more info on Initialization and Finalization here.
From the link:
The initialization block is used to initialize certain variables or execute code that is necessary for the correct functioning of the unit. The initialization parts of the units are executed in the order that the compiler loaded the units when compiling a program. They are executed before the first statement of the program is executed.
The finalization part of the units are executed in the reverse order of the initialization execution. They are used for instance to clean up any resources allocated in the initialization part of the unit, or during the lifetime of the program. The finalization part is always executed in the case of a normal program termination: whether it is because the final end is reached in the program code or because a Halt instruction was executed somewhere.