c# - AccessViolationException occured using P/Invoke with Media Foundation Interface in Multithread application -
i'm using p/invoke in c# call native function c++ dll below:
c++ dll:
extern "c" { // function: create wmv video sequences image. codec: wmv3 (vc-1) __declspec(dllexport) bool __stdcall createwmv(...) { ... } }
c# wrapper class. create c# wrapper class function map native c++ code:
[dllimport("aviencoder.dll", entrypoint = "createwmv", charset = charset.unicode, callingconvention = callingconvention.stdcall)] [return: marshalas(unmanagedtype.bool)] public static extern bool createwmv(...);
i'm sure parameters marshaling correctly in c#, because run when called directly in client c# code. issue occurred when put function in background thread.
private void test() { .... createwmv(...); // call processed without issue thread backgroundthread = new thread( new threadstart(()=> { createwmv(...); // call causes accessviolationexception } }
the function createwmv() uses media foundation interface generate wmv video. tried debug , found out when commented out function imfsinkwriter::writesample() in native code, program run without causing exception.
thus, wonder whether microsoft has weird in sinkwriter implementation. have same issue using media foundation way?
follow oleksii's comment, set below:
backgroundthread.trysetapartmentstate(apartmentstate.sta); // add fix createwmv() in multithreading backgroundthread.name = "createvideothead"; backgroundthread.start();
now, program run without exception. concept of apartmentstate in c# threading.
Comments
Post a Comment