views:

221

answers:

2

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

+2  A: 

For my future-self...I needed to CoInitialize and CoUnInitialize in my Execute method:

procedure TPollingThread.Execute;
begin

  CoInitialize(nil);

  while not Terminated  do
  begin
  // ...
  end;


   CoUnInitialize;
end;
Duncan
And may I also suggest reading the error messages. They might contain hidden hints :)
Lars Truijens
+3  A: 

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.

Marcus Adams
+1 for calling `CoUninitialize()` only when `CoInitializeEx()` succeeded, and for having a top-level exception handler in the thread.
mghie
mghie, I know that I have a mis-matched Co/CoUn in my app, as I threw CoInitialize in there because that's what the error said to do.... I will fix at the first opportunity, but that'll usually be a change request or escalated bug. In the meantime, what sort of problems will I be looking at? A memory leak, or will it crash somehow, or will it just be benign?
Chris Thornton
@Chris: I don't know for sure, I expect it to be benign, maybe some windows and handles not being properly released. Still, I like to vote up answers that promote good practices, and the documentation (http://msdn.microsoft.com/en-us/library/ms678543%28VS.85%29.aspx) clearly states that calls need to be balanced to gracefully close the COM library.
mghie
Thanks mghie, I'll fix at the earliest convenience, and it's good to know that it probably isn't too serious.
Chris Thornton