Log in

View Full Version : Problem with Scar form



Bad Boy JH
08-23-2010, 11:20 AM
program New;
var
VoteNoForm: TForm;
VoteNumber: TComboBox;
OkBtn, CancelBtn: TButton;

procedure InitVoteNoForm;
begin
VoteNoForm := CreateForm;
VoteNoForm.Left := 200;
VoteNoForm.Top := 160;
VoteNoForm.Width := 220;
VoteNoForm.Height := 110;
VoteNoForm.Caption := 'Rum Rebellion';
VoteNoForm.Color := ClWhite;

VoteNumber := TComboBox.Create(VoteNoForm);
VoteNumber.Parent := VoteNoForm;
VoteNumber.Top := 10;
VoteNumber.Left := 10;
VoteNumber.Width := 180;
VoteNumber.Height := 20;
VoteNumber.TEXT := '1';

OkBtn := TButton.Create(VoteNoForm);
OkBtn.Parent := VoteNoForm;
OkBtn.Caption := 'OK';
OkBtn.Top := 40;
OkBtn.Left := 10;
OkBtn.Height := 30;
OkBtn.Width := 80;

CancelBtn := TButton.Create(VoteNoForm);
CancelBtn.Parent := VoteNoForm;
CancelBtn.Caption := 'Cancel';
CancelBtn.Top := 40;
CancelBtn.Left := 110;
CancelBtn.Height := 30;
CancelBtn.Width := 80;
end;

procedure SafeInitVoteNoForm;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('InitVoteNoForm', v);
end;

procedure ShowVoteNoFormModal;
begin
VoteNoForm.ShowModal;
end;

procedure SafeShowVoteNoFormModal;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('ShowVoteNoFormModal', v);
end;

Function BuyVoteMultiForm(HowMany: Integer): Integer;
var
I: Integer;
begin
SafeInitVoteNoForm;
For I := 1 to HowMany do
VoteNumber.Items.Add(IntToStr(I));
SafeShowVoteNoFormModal;
end;

begin
BuyVoteMultiForm(4);
end.

It runs OK, and crashes during ShafeShowVoteNoFormModal. however if i remove the
For I := 1 to HowMany do
VoteNumber.Items.Add(IntToStr(I)); lines it runs without a problem

Edit: Right, sick of this happening, I post, 5 seconds later, I think of something and it fixes it...

I added
VoteNumber.Items.Add('1');
in InitForm, it solves the problem (obviously I have to change other parts) but scar doesn't crash...

Freddy1990
08-24-2010, 03:10 PM
You need to synchronize all access to VCL components because they run in the main thread, not just the creation of the form, this will work:
program New;
var
VoteNoForm: TForm;
VoteNumber: TComboBox;
OkBtn, CancelBtn: TButton;

procedure InitVoteNoForm;
begin
VoteNoForm := CreateForm;
VoteNoForm.Left := 200;
VoteNoForm.Top := 160;
VoteNoForm.Width := 220;
VoteNoForm.Height := 110;
VoteNoForm.Caption := 'Rum Rebellion';
VoteNoForm.Color := ClWhite;

VoteNumber := TComboBox.Create(VoteNoForm);
VoteNumber.Parent := VoteNoForm;
VoteNumber.Top := 10;
VoteNumber.Left := 10;
VoteNumber.Width := 180;
VoteNumber.Height := 20;
VoteNumber.TEXT := '1';

OkBtn := TButton.Create(VoteNoForm);
OkBtn.Parent := VoteNoForm;
OkBtn.Caption := 'OK';
OkBtn.Top := 40;
OkBtn.Left := 10;
OkBtn.Height := 30;
OkBtn.Width := 80;

CancelBtn := TButton.Create(VoteNoForm);
CancelBtn.Parent := VoteNoForm;
CancelBtn.Caption := 'Cancel';
CancelBtn.Top := 40;
CancelBtn.Left := 110;
CancelBtn.Height := 30;
CancelBtn.Width := 80;
end;

procedure SafeInitVoteNoForm;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('InitVoteNoForm', v);
end;

procedure ShowVoteNoFormModal;
begin
VoteNoForm.ShowModal;
end;

procedure SafeShowVoteNoFormModal;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('ShowVoteNoFormModal', v);
end;

procedure AddLine(s: string);
begin
if Assigned(VoteNumber) then
VoteNumber.Items.Add(s);
end;

procedure SafeAddLine(s: string);
var
v: TVariantArray;
begin
setarraylength(v, 1);
v[0] := s;
ThreadSafeCall('AddLine', v);
end;

Function BuyVoteMultiForm(HowMany: Integer): Integer;
var
I: Integer;
begin
SafeInitVoteNoForm;
for I := 1 to HowMany do
SafeAddLine(IntToStr(I));
SafeShowVoteNoFormModal;
end;

begin
BuyVoteMultiForm(4);
end.