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
[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
- performance is a feature -
Microsoft MVP - @Website - @AzureStuttgart - github.com/BenjaminAbt - Sustainable Code
WindowsFormsApplicationBase-Klasse
Mach das viel einfacher.
Beispiel dazu in mehrere Programminstanzen verhindern?
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.