MATLAB Answers

Abort external code process called by Matlab

2 ビュー (過去 30 日間)
Yonatan Nethanel
Yonatan Nethanel 2020 年 1 月 7 日
コメント済み: Guillaume 2020 年 2 月 3 日
Hi!
I have a c# function called by matlab, which takes long time to process. Also, I need the ability to raise a c# flag (used for aborting the process) from a UI .
So I need to do something like the follow:
%%
% on main file:
% launching the ui with the abort button
UI_guide();
% calling the long c# function
long_c_sharp_function();
%%
% on the guide .m file:
function button_pressed_callback()
c_sharp_function_to_raise_flag();
end
The problem is, the "abort" button callback is executed only after the long function finished - which mean I can't abort the process.
what can i do to solve this problem? should i consider another method?

  0 件のコメント

サインイン to comment.

回答 (1 件)

Andrew Janke
Andrew Janke 2020 年 1 月 31 日
There's no general mechanism for aborting a C# function call. The C# function has to be written specifically to support aborting, and you'll need to call it on a separate worker thread to allow your main thread to do the timeout and then call the abort.

  3 件のコメント

Yonatan Nethanel
Yonatan Nethanel 2020 年 2 月 3 日
I'm not looking for a matlab mechanism to abort c# function. I'm looking for a way to be able to raise matlab button_callback function while the c# method is still working...
Andrew Janke
Andrew Janke 2020 年 2 月 3 日
Ah. In that case, you need to run your C# code on another thread. Run your C# code using the C# Task mechanism - https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-based-asynchronous-programming - and that will leave the Matlab execution thread free for your GUI callbacks to happen on.
Guillaume
Guillaume 2020 年 2 月 3 日
I'm confused by the question, " I need the ability to raise a c# flag (used for aborting the process) from a UI" really sounds like you want matlab to be the one that send the abort signal to a long running C# code. In that case, as Andrew wrote there is no mechanism for that. Matlab is single threaded and if it has handed control to C#, there's nothing it can do in the meantime.
However, indeed if your C# code spawn a new thread doing the actual processing and returns immediately (using tasks for example), then yes matlab can then continue to do processing and then sets a flag. All the async logical needs to be implemented on the C# side, matlab can't help you with it.

サインイン to comment.

サインイン してこの質問に回答します。


Translated by