Multi-Threaded MATLAB???

by Joseph on Jan 3rd in Uncategorized

So the other day I had to do something pretty wild. I had to use multi-threaded MATLAB. Fortunately, there’s a way to do this. But it’s not easy. Basically, I wanted to allow my user to start a long-running process, from within a WPF application. We wanted to display a Busy Indicator to the user. The kicker is that MATLAB is not multi-threaded, so if we display the Busy Indicator, then the process stops.

So I ended up creating the busy indicator as a window in C#, and running it in a background thread – a second UI thread.

It’s nasty, and I don’t like it – but it WORKS!

It ends up looking something like this:

[code lang=csharp]
private Task StartTask(CommandType commandType = CommandType.Cancel,
string title = "Processing",
bool waitsBeforeShowingWindow = true)
{
_taskFinished = false;

Task backgroundTask = Task.Run(()=>
{
using (var iWait = new WaitProcedure(commandType: commandType,
command: new RelayCommand(() => { CancelExecuted?.Invoke(this, new CommandExecutedEventArgs(true)); }),
commandParameter: string.Empty,
title: title,
waitsBeforeShowingWindow: waitsBeforeShowingWindow,
startWaitProcedure: true,
isModal:false))
{
while (!_taskFinished) Task.Delay(1);
if(_taskFinished && !_taskCancelled) TaskFinished?.Invoke(this, new EventArgs());
}
});
return backgroundTask;
}
[/code]

Leave a Reply

Powered By Wordpress Designed By Ridgey