异常从线程调用远程SOAP调用 [英] Exception calling remote SOAP call from thread

查看:133
本文介绍了异常从线程调用远程SOAP调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是这个问题我几分钟前问过。



我有一个主窗体和线程的Delphi应用程序。线程每X秒发出一个远程对象的Web服务请求。然后,它会发回到使用新信息更新UI的主窗体。



我以前在我的线程中使用了TTimer对象,当TTimer回调函数跑了,它在主线程的上下文中运行(但是远程Web服务请求确实有效)。这相当于打破了单独的线程的目的,所以我现在在我的线程的Execute函数中有一个简单的循环和休眠例程。问题是,从GetIMySOAPService()返回时抛出异常。

  procedure TPollingThread.Execute; 
var
SystemStatus:TCWRSystemStatus;
开始
而不是终止do
begin
sleep(5000);
try
SystemStatus:= GetIMySOAPService()。GetSystemStatus;
PostMessage(ParentHandle,Integer(apiSystemStatus),Integer(SystemStatus),0);
SystemStatus.DataContext:= nil;
LParam(SystemStatus):= 0;

结束;
结束
结束

有人建议为什么在从线程调用此函数时抛出此异常?我确定我忽略了一些基本和简单的东西。



谢谢Duncan

解决方案

Execute()方法中,您必须调用 CoInitialize 您可以在 Application.Initialize()中自动执行此操作,以便设置和拆除COM库。



程序,但其他线程需要在调用COM函数之前调用 CoInitialize



确保在 Execute()方法中调用 CoInitialize ,而不是在构造函数中,因为构造函数在父线程(通常是主线程)。这不是你需要的地方。我们建议您使用以下格式:



pre> try
//如果调用失败,OleCheck会引发错误
OleCheck(CoInitializeEx(NIL,COINIT_MULTITHREADED或COINIT_SPEED_OVER_MEMORY));
try
//在这里工作
finally
CoUninitialize;
结束
除了
//我们失败了,做某事
结束;

这允许您在发生无法初始化时捕获错误,并确保您调用 CoUninitialize


This is an extension / next step of this question I asked a few minutes ago.

I've a Delphi application with a main form and a thread. Every X seconds the thread makes a web services request for a remote object. It then posts back to the main form which handles updating the UI with the new information.

I was previously using a TTimer object in my thread, and when the TTimer callback function ran, it ran in the context of the main thread (but the remote web services request did work). This rather defeated the purpose of the separate thread, and so I now have a simple loop and sleep routine in my thread's Execute function. The problem is, an exception is thrown when returning from GetIMySOAPService().

procedure TPollingThread.Execute;
var
SystemStatus : TCWRSystemStatus;
begin
while not Terminated  do
begin
  sleep(5000);
  try
    SystemStatus := GetIMySOAPService().GetSystemStatus;
    PostMessage( ParentHandle, Integer(apiSystemStatus), Integer(SystemStatus), 0 );
    SystemStatus.DataContext := nil;
    LParam(SystemStatus) := 0;
  except
  end;
end;
end;

Can anyone advise as to why this exception is being thrown when calling this function from the thread? I'm sure I'm overlooking something fundamental and simple.

Thanks, Duncan

解决方案

In your Execute() method, you must call CoInitialize and CoUnitialize to setup and tear down the COM library.

Your main thread automatically does this in the Application.Initialize() procedure, however, other threads require the call to CoInitialize before calling COM functions.

Ensure you call CoInitialize in the Execute() method and not in the constructor because the constructor is executed in the parent thread (usually the main thread). That's not where you need it. It must be called from the thread that you plan on making COM calls from.

I recommend using this format:

try
  // OleCheck will raise an error if the call fails
  OleCheck(CoInitializeEx(NIL, COINIT_MULTITHREADED or COINIT_SPEED_OVER_MEMORY));
  try
    // Do work here
  finally
    CoUninitialize;
  end;
except
  // We failed, do something
end;

This allows you to trap the error if it fails to initialize and ensures that you call CoUninitialize.

这篇关于异常从线程调用远程SOAP调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆