Results 1 to 10 of 10

Thread: Run only one instance of MDX

  1. #1
    Variable Bitrate rEegLer's Avatar
    Join Date
    Jul 2008
    Location
    Mechanicsburg, PA
    Posts
    266

    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. #2
    Fusion Brain Creator 2k1Toaster's Avatar
    Join Date
    Mar 2006
    Location
    Colorado, but Canadian!
    Posts
    10,045
    Quote 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

  3. #3
    Constant Bitrate
    Join Date
    Jun 2008
    Location
    Georgia, USA
    Posts
    188
    Quote 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

  4. #4
    Fusion Brain Creator 2k1Toaster's Avatar
    Join Date
    Mar 2006
    Location
    Colorado, but Canadian!
    Posts
    10,045
    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

  5. #5
    Constant Bitrate
    Join Date
    Jun 2008
    Location
    Georgia, USA
    Posts
    188
    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

  6. #6
    Variable Bitrate rEegLer's Avatar
    Join Date
    Jul 2008
    Location
    Mechanicsburg, PA
    Posts
    266
    Quote 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

  7. #7
    Constant Bitrate ACCLR8N's Avatar
    Join Date
    Apr 2009
    Location
    Davisburg, MI
    Posts
    195
    Quote 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.

  8. #8
    Raw Wave
    Auto Apps:loading...
    justchat_1's Avatar
    Join Date
    Jul 2008
    Location
    Boston, Ma or NY,NY
    Posts
    1,783
    Quote 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.

  9. #9
    Variable Bitrate rEegLer's Avatar
    Join Date
    Jul 2008
    Location
    Mechanicsburg, PA
    Posts
    266
    Is there a CF plugin for mdx? I'd whip one up but I'm illiterate in most forms of programming.
    Love

  10. #10
    Maximum Bitrate bratnetwork's Avatar
    Join Date
    Oct 2007
    Location
    Haledon, NJ
    Posts
    503
    I use FCC with RR and would also like to be able to restrict it to 1 instance.

Similar Threads

  1. Fusion Brain Latest Software MDX - Download Link
    By 2k1Toaster in forum FB Software
    Replies: 36
    Last Post: 05-12-2010, 07:52 PM
  2. Dual Instance, one winamp, cross-control
    By InfinitiPC in forum Road Runner
    Replies: 10
    Last Post: 12-09-2008, 01:04 AM
  3. MDX Auto Updater Version 4
    By 2k1Toaster in forum FB Software
    Replies: 20
    Last Post: 09-05-2008, 06:05 PM
  4. Quick overview of the Fusion Control Centre MDX Configurator
    By 2k1Toaster in forum FB Documentation and Tutorials
    Replies: 0
    Last Post: 06-06-2008, 05:17 AM
  5. UPS run from inverter?
    By FyreDaug in forum Power Supplies
    Replies: 28
    Last Post: 01-18-2007, 06:03 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •