So we are working on new app for Windows Store and we're utilizing DirectX on C++/CX part of app and C# on other. As we needed some sort of communication, we created interface in shared library (C#) and class that implements it on C# side of the project. We then pass instance of that class to the C++. When C++ need to communicate with c#, it calls methods from shared interface and c# executes what was implemented in the class.
The problem arises when C++ calls methods from another thread than then object was passed on to it. Error:
The application called an interface that was marshalled for a different thread.
Here's how it works:
C# instance of shared interface (some class)
C# -> C++ \/ passed in parameter of public C++ method
C++ saves to variable
C++ creates new thread
C++ calls instance->SayHello();
C# exception
Shared interface: (C#)
public interface IUserMessageService
{
void WriteMessage(string message, MessageTypes type);
}
Implementation: (C#)
public sealed class UserMessageService : IUserMessageService
{ ... }
Assign method (C++)
void AssignUserMessageService(...::IUserMessageService ^service) { m_service = service; }
I then call AssignUserMessageService
with instance of UserMessageService
from UI thread. Error occurs when I try to access data passed by C++ when calling WriteMessage
in another thread.
Is there any other way to pass instance or call method?