I've just "earned" the privilege to maintain a legacy library coded in C# at my current work.

This dll:

  • Exposes methods for a big legacy system made with Uniface, that has no choice but calling COM objects.
  • Serves as a link between this legacy system, and another system's API.
  • Uses WinForm for its UI in some cases.

More visually, as I understand the components :

*[Big legacy system in Uniface]* ==[COM]==> [C# Library] ==[Managed API]==> *[Big EDM Management System]*

The question is: One of the methods in this C# Library takes too long to run and I "should" make it asynchronous!

I'm used to C#, but not to COM at all. I've already done concurrent programming, but COM seems to add a lot of complexity to it and all my trials so far end in either:

  • A crash with no error message at all
  • My Dll only partially working (displaying only part of its UI, and then closing), and still not giving me any error at all

I'm out of ideas and resources about how to handle threads within a COM dll, and I would appreciate any hint or help.

So far, the biggest part of the code I've changed to make my method asynchronous :

// my public method called by the external system
public int ComparedSearch(string application, out string errMsg) {
  errMsg = "";
  try {
    Action<string> asyncOp = AsyncComparedSearch;
    asyncOp.BeginInvoke(application, null, null);
  } catch (ex) {
    // ...
  }
  return 0;
}

private int AsyncComparedSearch(string application) {
  // my actual method doing the work, that was the called method before
}

Any hint or useful resource would be appreciated. Thank you.

UPDATE 1:

Following answers and clues below (especially about the SynchronizationContext, and with the help of

Edit
Report