algorithmic modeling for Rhino
Hi everyone,
within a custom component (code below) I am creating a BackgroundWorker object to run an application (open Notepad) on a different thread to avoid freezing Grasshopper's UI. Everything seems to work fine but I'm currently encountering two bugs:
1. the output - DA.SetData(0, "Doing stuff"); - is not automatically refreshed when I assign it the new value from within worker_DoWork
2. if I inspect the output message with a panel it displays two branches, as if it had created a tree structure (see picture)
Can anyone provide me the missing piece of the puzzle? Thanks!
Code:
protected override void SolveInstance(IGH_DataAccess DA)
{
bool run = false;
DA.GetData(0, ref run);
DA.SetData(0, "Idle");
if (run)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (obj, e) => worker_DoWork(obj, e, ref DA);
worker.RunWorkerCompleted += (obj, e) => worker_RunWorkerCompleted(obj, e, ref DA);
worker.RunWorkerAsync();
}
}
private static void worker_DoWork(object sender, DoWorkEventArgs e, ref IGH_DataAccess DA)
{
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("notepad");
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = psi;
p.EnableRaisingEvents = true;
p.Start();
DA.SetData(0, "Doing stuff");
while (!p.HasExited)
{
System.Threading.Thread.Sleep(100);
}
}
private static void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e, ref IGH_DataAccess DA)
{
DA.SetData(0, "Done!");
}
Tags:
After a couple of days of searching, tinkering and sweating I've managed to find a decent solution. I'm attaching the code (implemented in a C# script) below, should anyone have similar issues.
This is currently doing the job for me, but I wouldn't mind some feedback from others (David, Giulio, to]?) in case they can suggest a better or cleaner approach.
Cheers,
r
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson.
Powered by