Announcement

Collapse
No announcement yet.

Run only one instance of MDX

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Run only one instance of MDX

    I feel like I'm asking a lot of questions today

    Is there a way to run only one instance of MDX? I did a quick search and couldn't find anything. The reason is that I want MDX is startup during boot but in centrafuse, I want to be able to 'open' it as an external app. In centrafuse, it just opens up a new instance of MDX which conflict.

    I'm sorry if there is a FAQ somewhere on this but I couldn't find anything.

    Thanks,

    Tim
    Love

  • #2
    Originally posted by rEegLer View Post
    I feel like I'm asking a lot of questions today

    Is there a way to run only one instance of MDX? I did a quick search and couldn't find anything. The reason is that I want MDX is startup during boot but in centrafuse, I want to be able to 'open' it as an external app. In centrafuse, it just opens up a new instance of MDX which conflict.

    I'm sorry if there is a FAQ somewhere on this but I couldn't find anything.

    Thanks,

    Tim
    This is a windows related issue, meaning if there is a solution to any windows program, it will work with MDX too. The normal method of a program opening is to allow as many simultaneous instances as possible. So when you launch it essentially from start and CF, then it spawns two instances (which I dont think will work because of driver conflicts over the same device...)

    Is there a way in CF to maximize a program? The one that launches in the system tray is a full version of the program, it just hides itself. So if you could get CF to maximize the program, it will popout of there. Then if you minimize it (and have minimize to system tray enabled which I think you do) it will go back to the system tray when you "exit" it from CF which would be minimization...

    I am not familiar enough with Centrafuse itself to point you in the right direction, hopefully some CF guru around here will know.

    -- Nick
    Fusion Brain Version 6 Released!
    1.9in x 2.9in -- 47mm x 73mm
    30 Digital Outputs -- Directly drive a relay
    15 Analogue Inputs -- Read sensors like temperature, light, distance, acceleration, and more
    Buy now in the MP3Car.com Store

    Comment


    • #3
      Originally posted by 2k1Toaster View Post
      This is a windows related issue, meaning if there is a solution to any windows program, it will work with MDX too...
      Not exactly...it depends on how the program was designed. Using C# and .Net, you can limit any program to a single instance by checking to see whether it is already running.

      In the program's Main method, call ProcessUtils.ThisProcessIsAlreadyRunning() to determine if another instance of the program is already running. ThisProcessIsAlreadyRunning attempts to create a Mutex object with the same name as the program (Application.ProductName). If it fails, the Mutex object already exists (and was created by another instance of the same program).

      If another instance of the program is already running, that instance is made visible and input focus is set to it. Otherwise a new instance of the program is launched.

      Example:

      Code:
      using System;
      using System.Collections.Generic;
      using System.Windows.Forms;
      using PU;
      
      namespace WindowsApplication1
      {
          static class Program
          {
              [STAThread]
              static void Main()
              {
                  // If this program is already running, set focus
                  // to that instance and quit.
                  if (ProcessUtils.ThisProcessIsAlreadyRunning())
                  {
                      // "Form1" is the caption (Text property) of the main form.
                      ProcessUtils.SetFocusToPreviousInstance("Form1");
                  }
                  else
                  {
                      Application.EnableVisualStyles();
                      Application.SetCompatibleTextRenderingDefault(false);
                      Application.Run(new Form1());
                  }
              }
          }
      }

      ProcessUtils.cs:

      Code:
      using System;
      using System.Diagnostics;
      using System.Threading;
      using System.Windows.Forms;
      using System.Runtime.InteropServices;
      
      namespace PU
      {
          /// Summary description for ProcessUtils.
          public static class ProcessUtils
          {
              private static Mutex mutex = null;
      
              /// Determine if the current process is already running
              public static bool ThisProcessIsAlreadyRunning()
              {
                  // Only want to call this method once, at startup.
                  Debug.Assert(mutex == null);
      
                  // createdNew needs to be false in .Net 2.0, otherwise, if another instance of
                  // this program is running, the Mutex constructor will block, and then throw 
                  // an exception if the other instance is shut down.
                  bool createdNew = false;
      
                  mutex = new Mutex(false, Application.ProductName, out createdNew);
      
                  Debug.Assert(mutex != null);
      
                  return !createdNew;
              }
      
              [DllImport("user32.dll", SetLastError = true)]
              static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
      
              [DllImport("user32.dll")]
              [return: MarshalAs(UnmanagedType.Bool)]
              static extern bool SetForegroundWindow(IntPtr hWnd);
      
              [DllImport("user32.dll")]
              static extern bool IsIconic(IntPtr hWnd);
      
              [DllImport("user32.dll")]
              static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
      
              const int SW_RESTORE = 9;
      
              [DllImport("user32.dll")]
              static extern IntPtr GetLastActivePopup(IntPtr hWnd);
      
              [DllImport("user32.dll")]
              static extern bool IsWindowEnabled(IntPtr hWnd);
      
              /// Set focus to the previous instance of the specified program.
              public static void SetFocusToPreviousInstance(string windowCaption)
              {
                  // Look for previous instance of this program.
                  IntPtr hWnd = FindWindow(null, windowCaption);
      
                  // If a previous instance of this program was found...
                  if (hWnd != null)
                  {
                      // Is it displaying a popup window?
                      IntPtr hPopupWnd = GetLastActivePopup(hWnd);
      
                      // If so, set focus to the popup window. Otherwise set focus
                      // to the program's main window.
                      if (hPopupWnd != null && IsWindowEnabled(hPopupWnd))
                      {
                          hWnd = hPopupWnd;
                      }
      
                      SetForegroundWindow(hWnd);
      
                      // If program is minimized, restore it.
                      if (IsIconic(hWnd))
                      {
                          ShowWindow(hWnd, SW_RESTORE);
                      }
                  }
              }
          }
      }
      Thanks,

      Dan

      Comment


      • #4
        I said "normal". I know it is easy and allowable to make just one instance. However that is not normal programming. You shouldnt inhibit multiple instance programmatically unless you have a very good reason for doing so.
        Fusion Brain Version 6 Released!
        1.9in x 2.9in -- 47mm x 73mm
        30 Digital Outputs -- Directly drive a relay
        15 Analogue Inputs -- Read sensors like temperature, light, distance, acceleration, and more
        Buy now in the MP3Car.com Store

        Comment


        • #5
          Agreed.

          If you have more than one FB, do you run an instance of MDX for each or can a single instance manage multiple brains? If a single instance can manage multiple brains and if multiple instances causes a conflict in the drivers, wouldn't that be a good reason to limit it to a single instance?

          ...So when you launch it essentially from start and CF, then it spawns two instances (which I dont think will work because of driver conflicts over the same device...)
          Thanks,

          Dan

          Comment


          • #6
            Originally posted by Dan2008 View Post
            Agreed.

            If you have more than one FB, do you run an instance of MDX for each or can a single instance manage multiple brains? If a single instance can manage multiple brains and if multiple instances causes a conflict in the drivers, wouldn't that be a good reason to limit it to a single instance?
            Right... under what circumstances would you want more than one instance running?
            Love

            Comment


            • #7
              Originally posted by Dan2008 View Post
              If you have more than one FB, do you run an instance of MDX for each or can a single instance manage multiple brains?
              That might have been a rhetorical question, but you don't run multiple MDX instances for multiple FB. Just one.

              Comment


              • #8
                Originally posted by Dan2008 View Post
                Not exactly...it depends on how the program was designed. Using C# and .Net, you can limit any program to a single instance by checking to see whether it is already running.

                In the program's Main method, call ProcessUtils.ThisProcessIsAlreadyRunning() to determine if another instance of the program is already running. ThisProcessIsAlreadyRunning attempts to create a Mutex object with the same name as the program (Application.ProductName). If it fails, the Mutex object already exists (and was created by another instance of the same program).

                If another instance of the program is already running, that instance is made visible and input focus is set to it. Otherwise a new instance of the program is launched.

                Example:

                Code:
                using System;
                using System.Collections.Generic;
                using System.Windows.Forms;
                using PU;
                
                namespace WindowsApplication1
                {
                    static class Program
                    {
                        [STAThread]
                        static void Main()
                        {
                            // If this program is already running, set focus
                            // to that instance and quit.
                            if (ProcessUtils.ThisProcessIsAlreadyRunning())
                            {
                                // "Form1" is the caption (Text property) of the main form.
                                ProcessUtils.SetFocusToPreviousInstance("Form1");
                            }
                            else
                            {
                                Application.EnableVisualStyles();
                                Application.SetCompatibleTextRenderingDefault(false);
                                Application.Run(new Form1());
                            }
                        }
                    }
                }

                ProcessUtils.cs:

                Code:
                using System;
                using System.Diagnostics;
                using System.Threading;
                using System.Windows.Forms;
                using System.Runtime.InteropServices;
                
                namespace PU
                {
                    /// Summary description for ProcessUtils.
                    public static class ProcessUtils
                    {
                        private static Mutex mutex = null;
                
                        /// Determine if the current process is already running
                        public static bool ThisProcessIsAlreadyRunning()
                        {
                            // Only want to call this method once, at startup.
                            Debug.Assert(mutex == null);
                
                            // createdNew needs to be false in .Net 2.0, otherwise, if another instance of
                            // this program is running, the Mutex constructor will block, and then throw 
                            // an exception if the other instance is shut down.
                            bool createdNew = false;
                
                            mutex = new Mutex(false, Application.ProductName, out createdNew);
                
                            Debug.Assert(mutex != null);
                
                            return !createdNew;
                        }
                
                        [DllImport("user32.dll", SetLastError = true)]
                        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
                
                        [DllImport("user32.dll")]
                        [return: MarshalAs(UnmanagedType.Bool)]
                        static extern bool SetForegroundWindow(IntPtr hWnd);
                
                        [DllImport("user32.dll")]
                        static extern bool IsIconic(IntPtr hWnd);
                
                        [DllImport("user32.dll")]
                        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
                
                        const int SW_RESTORE = 9;
                
                        [DllImport("user32.dll")]
                        static extern IntPtr GetLastActivePopup(IntPtr hWnd);
                
                        [DllImport("user32.dll")]
                        static extern bool IsWindowEnabled(IntPtr hWnd);
                
                        /// Set focus to the previous instance of the specified program.
                        public static void SetFocusToPreviousInstance(string windowCaption)
                        {
                            // Look for previous instance of this program.
                            IntPtr hWnd = FindWindow(null, windowCaption);
                
                            // If a previous instance of this program was found...
                            if (hWnd != null)
                            {
                                // Is it displaying a popup window?
                                IntPtr hPopupWnd = GetLastActivePopup(hWnd);
                
                                // If so, set focus to the popup window. Otherwise set focus
                                // to the program's main window.
                                if (hPopupWnd != null && IsWindowEnabled(hPopupWnd))
                                {
                                    hWnd = hPopupWnd;
                                }
                
                                SetForegroundWindow(hWnd);
                
                                // If program is minimized, restore it.
                                if (IsIconic(hWnd))
                                {
                                    ShowWindow(hWnd, SW_RESTORE);
                                }
                            }
                        }
                    }
                }
                Talk about a complicated solution to an easy problem....that whole thing can be replaced with a single line of managed code.

                Theres no reason that the cf plugin couldn't check for a single instance though->it doesn't have to be mdx.
                openMobile - An open source C# Front End (why choose openMobile?)
                - Always Recruiting Developers -
                Like what you see? Donations are always welcome

                Comment


                • #9
                  Is there a CF plugin for mdx? I'd whip one up but I'm illiterate in most forms of programming.
                  Love

                  Comment


                  • #10
                    I use FCC with RR and would also like to be able to restrict it to 1 instance.

                    Comment

                    Working...
                    X