Determine if custom plugin is loaded in another rhino process?

Hello,

When a plug-in loads or when the process that loaded it shuts down, is there a way to determine whether or not the plug-in is loaded by another Rhino process?

Ideally, I wish to avoid doing this with files because when a crash occurs, the files may not get cleaned up.

Hey @jakemurphy0118,

Here is one simple way. I’m sure there are others…

using Rhino.PlugIns;
using System;
using System.Threading;

namespace TestJake
{
  public class TestJakePlugIn : PlugIn
  {
    private Mutex m_mutex;

    public TestJakePlugIn()
    {
      Instance = this;
    }

    public static TestJakePlugIn Instance
    {
      get; private set;
    }

    protected override LoadReturnCode OnLoad(ref string errorMessage)
    {
      m_mutex = new Mutex(true, Id.ToString(), out var result);
      if (!result)
      {
        errorMessage = string.Format("{0} plug-in already in use in another running instance of Rhino.", Name);
        return LoadReturnCode.ErrorShowDialog;
      }
      GC.KeepAlive(m_mutex);

      return LoadReturnCode.Success;
    }

    protected override void OnShutdown()
    {
      if (null != m_mutex)
        m_mutex.Dispose();
    }
  }
}

– Dale

Thanks Dale.