http://www.oschina.net/code/snippet_4873_4882
http://www.cnblogs.com/zhangzhifeng/p/3319188.html
例子:
http://www.ysgang.com/article/html/6598.html
多个线程的例子:
http://bbs.csdn.net/topics/390486889
标准的讲解:
http://www.cnblogs.com/gzcszzx/articles/2110675.html
标准讲解补充http://www.cnblogs.com/del/archive/2009/02/13/1390295.html
有用的CreateThread
http://hi.baidu.com/delphidiary/item/8cf5309307b0f6f7291647f4
多个线程并发调用IIS上发布的webservice
--
在Delphi 多线程中出现“尚未调用CoInitialize错误”的解决方法
标准答案:
http://bbs.csdn.net/topics/60297694
function TMyThread.ExecTimer: Boolean;
begin
Result := False ;
FMyThreadExecfinish := False;
Screen.Cursor := crHourGlass;
CoInitialize(nil); // 一定要加上此行代码
try
TranspondClientBaseData_factory1(HTTPRIOLeExp, HTTPRIONC, LogStrings);
Sleep(1000);
TranspondPersonBaseData_factory1(HTTPRIOLeExp, HTTPRIONC, LogStrings);
Sleep(1000);
TranspondDeptBaseData_factory1(HTTPRIOLeExp, HTTPRIONC, LogStrings);
finally
Result := True;
FMyThreadExecfinish := True;
Screen.Cursor := crDefault;
CoUninitialize;// 一定要加上此行代码
end;
end;
即必须在使用前初始化COM环境
http://blog.yourtion.com/delphi-not-called-coinitialize-solution.html
======最终搞完了,多线程调用Webservice,两种方式,一种是windows的Api,另一种是用thread类
方法1的例子(缺点是只能起一个线程,两个的话地址错误,关闭执行保护也不行):
1、工程内,File->new->Webservice->WSDLImporter,导入webservice地址,生成一个新的pas,得到输出函数,比如叫GetSvrForPosSoap
2、该pas内切记,在initialization后面加上
InvRegistry.RegisterInvokeOptions(TypeInfo(SvrForPosSoap), ioDocument);//这行要手工加,否则传入参数会为空
3、
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, SOAPHTTPClient, StdCtrls, InvokeRegistry, Rio, ActiveX;
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
SvrForPos,XSBuiltIns;
{$R *.dfm}
4、
procedure TForm1.Button1Click(Sender: TObject);
var
HelloWorld,sMsg:widestring;
HTTPRIO:THTTPRIO;
begin
try
begin
HTTPRIO := THTTPRIO.Create(Application);
HTTPRIO.HTTPWebNode.UseUTF8InHeader:=true; //必须要加,解决中文参数的
HelloWorld := GetSvrForPosSoap(True,'', HTTPRIO).HelloWorld('Test My Brain,测试!');//调用webservice及其内部函数HelloWorld
Edit1.Text:=HelloWorld;
end
Except
On E:Exception do
begin
sMsg:=E.Message;
sMsg:='意外失败:'+sMsg;
showmessage(sMsg);
end;
end;
end;
function MyThreadFun(p: Pointer): DWORD; stdcall;
var
i: Integer;
HelloWorld,sMsg:widestring;
HTTPRIO:THTTPRIO;
begin
coinitialize(nil); //必须加,否则提示尚水调用
try
HTTPRIO := THTTPRIO.Create(Application);
HTTPRIO.HTTPWebNode.UseUTF8InHeader:=true;
for i := 0 to 10 do
begin
HelloWorld := GetSvrForPosSoap(True,'', HTTPRIO).HelloWorld('Test My Brain,测试!我要测试!');
form1.ListBox1.Items.Add(IntToStr(i)+' '+HelloWorld);
end;
finally
CoUninitialize;//必须加,否则提示尚水调用
end;
//for i := 0 to 99 do form1.ListBox1.Items.Add(IntToStr(i));
Result := 0;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
ID: DWORD;
begin
CreateThread(nil, 0, @MyThreadFun, nil, 0, ID);
end;
方法2的例子(多线程调webservice是可以的):
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, SOAPHTTPClient, StdCtrls, InvokeRegistry, Rio, ActiveX, ExtCtrls; type TConnDbThread = class(TThread) private { Private declarations } protected procedure Execute; override; end; TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; Edit2: TEdit; Button2: TButton; ListBox1: TListBox; Button3: TButton; Timer1: TTimer; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Timer1Timer(Sender: TObject); private { Private declarations } Threads, Threads1: TConnDbThread; public { Public declarations } end; var Form1: TForm1; implementation uses SvrForPos,XSBuiltIns; {$R *.dfm}
procedure TConnDbThread.Execute; var i: Integer; HelloWorld,sMsg:widestring; HTTPRIO:THTTPRIO; sProcName: WideString; sParamName: WideString; sParamType: WideString; sParamLen: WideString; sParamDirection: WideString; sParamValue: WideString; CallProcedureResult: Boolean; sOutParamValue: WideString; sResultCode: WideString; sMessage: WideString; sJson: WideString; begin coinitialize(nil); try HTTPRIO := THTTPRIO.Create(Application); HTTPRIO.HTTPWebNode.UseUTF8InHeader:=true; for i := 0 to 10 do begin sProcName:='sPro_BankCertiStatusCheck'; sParamName:='ps_CertiNo;ps_CheckCode;pf_CertiMe;ps_OrgCode;ps_SaleNo;ps_CxBillNo;ps_CertiCode;ps_ZfType;ps_BgnDate;ps_EndDate;pf_SjCertiMe;pi_Result;ps_Message'; sParamType:='S;S;F;S;S;S;S;S;S;S;F;I;S'; sParamLen:='20;4;19;10;20;20;4;4;10;10;19;-1;2000'; sParamDirection:='I;I;I;I;I;O;O;O;O;O;O;O;O'; sParamValue:='123456;1234;50;1008;2014010210080001;*;1;1;1;1;1;1;1';//输出参数随便写啥,但必须写 GetSvrForPosSoap(True,'', HTTPRIO).CallProcedure(sProcName,sParamName,sParamType,sParamLen, sParamDirection,sParamValue,CallProcedureResult,sOutParamValue,sResultCode,sMessage,sJson); HelloWorld := sOutParamValue; form1.ListBox1.Items.Add(IntToStr(i)+' '+HelloWorld); end; finally CoUninitialize; end; end; procedure TForm1.Button3Click(Sender: TObject); begin Threads:=TConnDbThread.Create(False); Threads1:=TConnDbThread.Create(False); end;