0

I have been looking through a ton of questions here to figure out what i want to achieve, but cant get it done. Here it comes:

So I have a COM object to be able to let my c# dll to communicate with a Delphi application. problem is, I have to do some status polling and want to do this from a seperate thread. The function call in the polling thread crashes at seemingly random times. I assume this is because of multiple threads access the COM Objects functions.

I tried to dispatch the call to my host c# app main thread, but this only makes things worse and blocks the app along, because for 'reasons', i.e. bad design on the delphi side, the status poll takes quite a while (up to 500ms). My c# host app is accessing this COM object as well.

The COM interface is rather simple, with all calls being channeled through a doSomething(int index, object parameter, ref object result) function. So I want to build some kind of wrapper around that function, so that the actual call of doSomething is always executed in one specific thread and the result is returned to the caller.

my (very vage) guess is, that I need some kind of STAThread which listens to some kind of Func<object>(int nr, object parameter) que, to which I add my according doSomething calls. But I cant figure out how to return the result afterwards. Most of the questions on similar issues I looked into didnt return a value or dealt with ui components, which seems to make all the difference. I dont think using Task is a valid option here, because this would only shift the problem to a different set of threads.

EDIT: What I could do is something similar to this answer, giving each call an unique index, pushing the result to a dictionary and wait for an event to retrieve the result when its ready. But this seems unneccesarily complicated and very inconvienient for such a 'simple' task

EDIT2: after reading through this related answer which acutally kind of covers what i was looking for, could it really be as simple as to mark the polling thread as STAThread, because COM is acutally kindof taking care of what i want?

Abudinka
  • 85
  • 6
  • 1
    If your implementation is correct, there's nothing specific to do, COM can handle it automatically. If you experience crashes, it's because you have bugs in your code, or your implementation is incorrect and/or is missing important points. Devil hides in details, ie: show us code or better post a reproducing project demonstrating the issue – Simon Mourier Jan 12 '22 at 10:07
  • unfortunately i cant provide some meaningful code and/or reproducable example, nevertheless your comment pushed me to look deeper into whats going on. As far as I understand the COM Interface and threading models this would point towards an issue on the delphi app side. Regardless of COM, i would be still interested how one could achieve such a redirection mechanic, but i guess this means to build or use some kind of task scheduler – Abudinka Jan 12 '22 at 10:22
  • https://stackoverflow.com/a/21684059/17034 – Hans Passant Jan 12 '22 at 11:53

0 Answers0