I do something like this to run a python script in a .net environment (works fine):
public partial class MainWindow : Window
{
private readonly ScriptEngine m_engine;
private readonly ScriptScope m_scope;
public MainWindow()
{
InitializeComponent();
m_engine = Python.CreateEngine();
dynamic scope = m_scope = m_engine.CreateScope();
}
public void ExecuteScript()
{
try
{
m_engine.Execute(textEditor.Text, m_scope);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
It would be nice, if there is a chance to execute the instruction
m_engine.Execute(textEditor.Text, m_scope);
in a background task to not completly block the GUI of the application.
What I have tried:
However, the easy way to put this in a Task like this:
await Task.Run(() => m_engine.Execute(textEditor.Text, m_scope));
does not work. Any ideas on how to get this run?
Thanks Joe
update:
I get the following error message: The calling thread cannot access this object because a different thread owns it.
Update 2:
I understood that I need to put the Execute inbetween a dispatcher invoke:
await Task.Run(() => this.Dispatcher.Invoke(() => m_engine.Execute(textEditor.Text, m_scope)));;
This works so far but the main thread with the GUI is still blocked as long as the script is being executed. What has to be done, that the thread runs in background and the GUI is still accessible?
Update 3:
These are my methodes:
private void Run(object sender, RoutedEventArgs e)
{
ExecuteScriptAsync(textEditor.Text);
}
private async void ExecuteScriptAsync(string script)
{
await Task.Run(() => m_engine.Execute(script, m_scope));
}
When I just using
await Task.Run(() => m_engine.Execute(script, m_scope));
without dispatcher I still get "The calling thread cannot access this object because a different thread owns it" (even when using a variable instead of the Textbox.Text)
private async void ExecuteScriptAsync(string script)
{
await Task.Run(() => this.Dispatcher.Invoke(() => m_engine.Execute(script, m_scope).ConfigureAwait(true))); ;
}
works without "The calling thread cannot access this object because a different thread owns it". However the UI is blocked as long as the script operates. Question is, how to run the background task without blocking the calling UI task.