Laden...

Befehl an laufendes Programm übergeben

Erstellt von Grimmbizkit vor 10 Jahren Letzter Beitrag vor 10 Jahren 2.087 Views
Thema geschlossen
G
Grimmbizkit Themenstarter:in
310 Beiträge seit 2006
vor 10 Jahren
Befehl an laufendes Programm übergeben

Hallo zusammen,

irgendwie stehe ich auf dem Schlauch.

Ich habe 2 Programme geschrieben.

Nun würde ich gerne in Prog1 mit einem Parameter ein Startverhalten erzeugen (z.B. Formular öffnen) Dieses habe ich mittels der "Parameterfunktion" gemacht.
Wenn ich das Programm mit einem bestimmt Parameter öffne klappt alles.


[STAThread]
static void Main(String[] args)
{
    if (args.Length == 1)
        startArg = args[0];
        .....

Nun ist es so das dieses Programm nur einmal gestartet werden soll.
Daher folgender Code:


            try
            {
                //Get the running instance. 
                Process instance = null;
                
                instance = RunningInstance();
                
                if (instance == null)
                {
                    // If no other instance of program is running, show form 
                    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new AppForm());
                }
                else
                {
                    // There is another instance of this process, do not allow second 
                    HandleRunningInstance(instance);
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.ToString());
            }

und noch:


        public static Process RunningInstance()
        {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);

            //Loop through the running processes in with the same name 
            foreach (Process process in processes)
            {
                //Ignore the current process if 
                if (process.Id != current.Id)
                {
                    //Make sure that the process is running from the exe file. 
                    if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
                        return process;
                }
            }
            return null;
        }

        public static void HandleRunningInstance(Process instance)
        {
            //Make sure the window is not minimized or maximized 
            //ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
            ShowWindowAsync(instance.MainWindowHandle, SW_SHOWMAXIMIZED);

            //Set the real intance to foreground window 
            SetForegroundWindow(instance.MainWindowHandle);
        }

        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        private const int SW_HIDE = 0;
        private const int SW_SHOWNORMAL = 1;
        private const int SW_SHOWMINIMIZED = 2;
        private const int SW_SHOWMAXIMIZED = 3;
        private const int SW_SHOWNOACTIVATE = 4;
        private const int SW_RESTORE = 9;
        private const int SW_SHOWDEFAULT = 10;

Mein großes Problem ist, das ich nun diesen Parameter nicht "rüber bekomme" wenn meine Anwendung schon läuft.

Hat jemand von euch vielleicht eine Ahnung wie das funktionieren könnte?

Gruß Simon

16.835 Beiträge seit 2008
vor 10 Jahren

[FAQ] mehrere Programminstanzen verhindern (inkl. Parameterübergabe)
Ansonsten ist "Mutex" das Suchwort, mit dem Du viele Treffer findest, die dieses Problem lösen.

Beachte [Hinweis] Wie poste ich richtig? 1.1

F
10.010 Beiträge seit 2004
vor 10 Jahren
Hinweis von herbivore vor 10 Jahren

Die Möglichkeit mit der WindowsFormsApplicationBase-Klasse wird bereits in der FAQ genannt. Insofern war der Link auf die FAQ ausreichend.

Wer weitere gute Möglichkeiten kennt, kann diese gerne per PM ans Team senden, siehe Kontakt zum Team.

Thema geschlossen