这有什么错? |
尚未結案
|
andy_qin
一般會員 發表:41 回覆:51 積分:18 註冊:2003-03-06 發送簡訊給我 |
Procedure runselect(P:pointer);stdcall;
begin
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('****');
Form2:=TForm2.Create(Application);
try
adoquery1.Open;
except
showmessage('异常处理');
end;
end; procedure TForm1.SpeedButton1Click(Sender: TObject);
var
thid:dword;
begin
createthread(nil,0,@runselect,nil,0,thid); end; 能编译,但不能执行,帮我看看吧。
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
|
andy_qin
一般會員 發表:41 回覆:51 積分:18 註冊:2003-03-06 發送簡訊給我 |
|
andy_qin
一般會員 發表:41 回覆:51 積分:18 註冊:2003-03-06 發送簡訊給我 |
|
solnone
中階會員 發表:2 回覆:97 積分:69 註冊:2003-05-06 發送簡訊給我 |
'access villoation' 是因為你用的Object不存在所造成的
在Thread 中使用 COM Object 要先 CoInitialize Procedure runselect; stdcall;
var
adoquery1: TADOQuery;
begin
CoInitialize(nil);
adoquery1 := TADOQuery.Create(nil);
try
adoquery1.ConnectionString := 'Provider=....';
adoquery1.SQL.Text := 'select * from table';
adoquery1.Open;
finally
adoquery1.Free;
CoUninitialize();
end;
end; procedure TForm1.SpeedButton1Click(Sender: TObject);
var
thid: DWord;
begin
createthread(nil,0,@runselect,nil,0,thid);
end;
|
andy_qin
一般會員 發表:41 回覆:51 積分:18 註冊:2003-03-06 發送簡訊給我 |
|
sos_admin
版主 發表:121 回覆:697 積分:768 註冊:2003-07-23 發送簡訊給我 |
unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls;
type
ado_exec = class(TThread)
private
Procedure runselect;
{ Private declarations }
protected
procedure Execute; override;
end;
type
TForm1 = class(TForm)
Button1: TButton;
ADOQuery1: TADOQuery;
procedure Button1Click(Sender: TObject);
private { Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation uses Unit2; {$R *.dfm} //#####################
procedure ado_exec.Execute;
begin
Synchronize(runselect);
end; Procedure ado_exec.runselect;
var
adoquery1: TADOQuery;
begin
adoquery1:=TADOQuery.Create(nil);
try
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('****');
Form2:=TForm2.Create(Application);
try
adoquery1.Open;
except
showmessage('异常处理');
end;
finally
adoquery1.Free;
end;
end;
//##################### procedure TForm1.Button1Click(Sender: TObject); begin
ado_exec.Create(false);
end; end.
|
andy_qin
一般會員 發表:41 回覆:51 積分:18 註冊:2003-03-06 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
I have tested... both 1) & 2) works
1)
function RunSelect: dword; stdcall; begin Result := S_OK; end;2) procedure RunSelect(Param: integer); stdcall; However, you should be careful that VCL is not thread safe (and same to some Windows APIs), this is an example: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; const WM_TEST = WM_USER 1; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } procedure WMText(var Msg: TMessage); message WM_TEST; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} function RunSelect: dword; stdcall; begin Result := S_OK; SendMessage(Form1.Handle,WM_TEST,0,0); end; procedure TForm1.Button1Click(Sender: TObject); var thid: dword; begin CreateThread(nil,0,@RunSelect,nil,0,thid); end; procedure TForm1.WMText(var Msg: TMessage); begin ShowMessage('Test'); end; end. |
solnone
中階會員 發表:2 回覆:97 積分:69 註冊:2003-05-06 發送簡訊給我 |
|
andy_qin
一般會員 發表:41 回覆:51 積分:18 註冊:2003-03-06 發送簡訊給我 |
unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB, ADODB, Activex; type
TForm1 = class(TForm)
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} function RunSelect: dword; stdcall;
var
adoquery1:tadoquery;
begin
CoInitialize(nil);
adoquery1.close;
adoquery1.open;
CoUninitialize();
end; procedure TForm1.Button1Click(Sender: TObject);
var
thid: dword;
begin
CreateThread(nil,0,@RunSelect,nil,0,thid);
end; end. error message:error message:'access villoation at 0x00487c2a:read of address 0x0000000' 活见鬼。
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
|
andy_qin
一般會員 發表:41 回覆:51 積分:18 註冊:2003-03-06 發送簡訊給我 |
unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, DBGrids, DB, ADODB, Activex; type
TForm1 = class(TForm)
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} function RunSelect: dword; stdcall;
var
adoquery1:tadoquery;
begin
CoInitialize(nil);
adoquery1:=tadoquery.Create(nil);
try
adoquery1.close;
adoquery1.ConnectionString:='Provider=***';
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select * from table');
adoquery1.open;
finally
adoquery1.free;
CoUninitialize();
end; end; procedure TForm1.Button1Click(Sender: TObject);
var
thid: dword;
begin
CreateThread(nil,0,@RunSelect,nil,0,thid);
end; end. 这个写table并没有open,为什么?
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
|
andy_qin
一般會員 發表:41 回覆:51 積分:18 註冊:2003-03-06 發送簡訊給我 |
|
solnone
中階會員 發表:2 回覆:97 積分:69 註冊:2003-05-06 發送簡訊給我 |
第一種方法,直接使用 Form1.ADOQuery1
function RunSelect: dword; stdcall;
begin
CoInitialize(nil);
Form1.ADOQuery1.close;
Form1.ADOQuery1.open;
CoUninitialize();
end; 第二種方法,參考到 Form1.ADOQuery1
function RunSelect: dword; stdcall;
var
adoquery1:tadoquery;
begin
adoquery1 := Form1.ADOQuery1;
CoInitialize(nil);
adoquery1.close;
adoquery1.open;
CoUninitialize();
end; 第三種方法,自已建 ADOQuery1
Procedure runselect; stdcall;
var
adoquery1: TADOQuery;
begin
CoInitialize(nil);
adoquery1 := TADOQuery.Create(nil);
try
adoquery1.ConnectionString:='Provider=MSDAORA.1;Password=123;User ID=123;Data Source=ho;Persist Security Info=True';
adoquery1.SQL.Add('select * from table1');
adoquery1.Open;
finally
adoquery1.Free;
CoUninitialize();
end;
end;
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |