1

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?

Aleksandar Toplek
  • 2,792
  • 29
  • 44
  • this might help:http://stackoverflow.com/questions/19341591/the-application-called-an-interface-that-was-marshalled-for-a-different-thread – Matt Feb 25 '14 at 16:54
  • @Matt that actually worked for me. The error was that I was using `CoreApplication.MainWindow.CoreWindow.Dispatcher` which look like isn't UI thread dispatcher... – Aleksandar Toplek Feb 25 '14 at 20:24

1 Answers1

1

The problem was that I was using CoreApplication.MainWindow.CoreWindow.Dispatcher instead of Window.Current.Dispatcher

Aleksandar Toplek
  • 2,792
  • 29
  • 44