I assume you are in fact doing this within a WPF application. So I wrote some sample code you can put into a Rhino Common Command that will demonstrate how to invoke onto a WPF UI Thread from a Background Worker Thread.
Create a Rhino Command in your Project.
Add the following Code / Methods to the Command.
private static BackgroundWorker _worker;
private Application _app;
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
// Start WPF UI Dispatcher if not running.
if (_app == null)
{
_app = new Application();
}
RhinoApp.WriteLine("Creating Background Worker Test.");
_worker = new BackgroundWorker();
_worker.DoWork += _worker_DoWork;
_worker.RunWorkerCompleted += _worker_RunWorkerCompleted;
_worker.RunWorkerAsync();
RhinoApp.WriteLine("Exiting Run Command.", EnglishName);
return Result.Success;
}
void _worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
RhinoApp.WriteLine("DoWork Completed Back on UI thread.", EnglishName);
}
void _worker_DoWork(object sender, DoWorkEventArgs e)
{
const string command = "_-Circle p 0,0 3,3";
RhinoApp.WriteLine("Background Worker DoWork Starting on Background Thread.");
var result = RhinoApp.RunScript(command, true);
RhinoApp.WriteLine("Circle Attempted: " + result);
Application.Current.Dispatcher.Invoke(() =>
{
RhinoApp.WriteLine("Background Worker DoWork Invoking on UI thread.");
result = RhinoApp.RunScript(command, true);
RhinoApp.WriteLine("Circle Attempted: " + result);
});
}
Start your project and Execute the Command.
You should see the following Output in the Command Prompt window.
Creating Background Worker Test.
Exiting Run Command.
Background Worker DoWork Starting on Background Thread.
Circle Attempted: False
Background Worker DoWork Invoking on UI thread.
Command: _-Circle
Center of circle ( Deformable Vertical 2Point 3Point Tangent AroundCurve FitPoints ): p
Start of diameter ( Vertical ): 0,0
End of diameter ( Vertical ): 3,3
Circle Attempted: True
DoWork Completed Back on UI thread.
This demonstrates that the RunScript() executed on the Background Thread fails, but by using the Dispatcher and Invoking your RunScript the second time it succeeds. You will have a Circle near Origin Axis.
This should fix you all up. If you have any questions don’t hesitate to email me.