Force Matlab to execute .NET callback on main/single thread

4 views (last 30 days)
Hi
I wrote a simple matlab filewatcher object than uses .NET's FileSystemWatcher. It's a great little tool, works straight off the bat and you can add a Matlab callback directly. So far so good.
This worked perfectly in R2015a. I compile this in a larger stand-along application that my colleagues use and the .exe application works perfectly.
Enter R2015b (and R2016a): Works fine when running natively in Matlab but when run as a compiled .exe (even on my own machine) it crashes whenever the callback for the file watcher is asked to execute.
The reason for this is apparently that the callback tries to execute on a different thread and this crashes the application. This has been reported as bug 560506. It's been around for a long time (since R2009a) yet this doesn't happen in R2015a. In the bug report it says the workaround is to not invoke the callback MATLAB code delegate in a separate thread. Does anybody know how to do that?
I realise I could implement this in java but that requires java dev, it requires dragging a jar file along and requires "indirect" callback attachment. I also realize I could stay with R2015a but recently I have developed code with findgroups that obviously does not exist in R2015a.
So if anyone can point me in the right direction for forcing the callback execution on the main/single thread that would be absolutely great.
Regards, Phil

Answers (1)

Rahul Goel
Rahul Goel on 12 Apr 2016
Hi Philip
There is no full proof work around for this issue. You can minimize the chances of running into this issue however by making sure you do not create multiple objects like the System.IO.FileSystemWatcher object which would raise an .NET event on the same external event (i.e. a file changing).
To confirm that you are running into this exact issue, load the matlab_crash_dump.NNN.dmp file into Visual Studio, debug the managed part of the code and confirm that the actual Exception which occurred is:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
  3 Comments
Mike
Mike on 4 Jun 2016
Edited: Mike on 4 Jun 2016
I think I am having this same issue. I can demonstrate it by compiling a simple guide app with a single "Start" button. The start button calls a .NET function "startEventThread" that creates a new thread that fires off 100 sequential events with 10ms delay. The events are caught in Matlab with a simple disp('Event Triggered'). This works in Matlab 2013b and 2016a. It also works when compiled in 2013b but it crashes when compiled in 2016a (it crashes from the very first event that is fired). Interestingly if I update my Start button callback to include pause(5) immediately after the .NET function startEventThread is called, the application surprisingly works when compiled in 2016a.
It seems to me that this is an issue introduced in 2015b. I frequently use .NET events fired from a separate .NET thread using compiled 2013b with no issues.
I have attached source code.
Mike
Mike on 7 Jun 2016
I just tested 2016b prerelease and the issue appears to be resolved. I guess we will have to wait until the official release to confirm.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!