Hy@all,
ich habe ein komisches Problem. Mein Programm an dem ich grad schreibe, darf nur einmal geöffnet werden. Um dies zu realisieren hab ichs einfach so gemacht.
public class SingleInstanceApplication : WindowsFormsApplicationBase
{
private SingleInstanceApplication()
{
base.IsSingleInstance = true;
}
public static void Run(Form f, StartupNextInstanceEventHandler startupHandler)
{
SingleInstanceApplication app = new SingleInstanceApplication();
app.MainForm = f;
app.StartupNextInstance += startupHandler;
app.Run(Environment.GetCommandLineArgs());
}
}
static class Program
{
private static Form1 Frm = null;
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main(String[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Frm = new Form1();
Frm.StartWithArguments(args);
SingleInstanceApplication.Run(Frm, StartupNextInstanceHandler);
}
static void StartupNextInstanceHandler(object sender, StartupNextInstanceEventArgs e)
{
if (Frm == null)
{
return;
}
List<String> ArgsList = new List<String>();
for (Int32 i = 1; i < e.CommandLine.Count; i++)
{
ArgsList.Add(e.CommandLine[i]);
}
Frm.StartWithArguments(ArgsList.ToArray());
}
}
Wie man in dem Oben gezeigten Codeausschnitt sehen kann, kann mein Programm sowohl beim start der exe als auch bei wiederholten aufruf mit argumenten (Frm.StartWithArguments(args); Frm.StartWithArguments(ArgsList.ToArray()); ) von außen "gefüttert" werden. Klappt auch alles soweit Problemlos.
In der Methode "StartWithArguments" des Form1 möchte ich bei einem bestimmt Argument (z.B. "/?") eine MessageBox ausgeben. Jetzt habe ich das Phänomen, das diese MessageBox immer zweimal angezeigt wird. Wenn man im Programm Breakpoints setzte, so wird der Breakpoint erst nach(!) der ersten MessageBox aktiviert, bloß warum?
Kann mir hier jemand helfen?
MfG
Tonka
Hallo tonka,
schwer zu sagen. Du solltest mal versuchen, das Problem mit Logging zu lokalisieren und dabei jeweils auch die Prozess-Id mit ausgeben.
herbivore
Hy,
mir sagt "logging" leider nichts, könntest du's mal kurz beschreiben.
Danke
MfG
Tonka
Hallo tonka,
siehe [Hinweis] Wie poste ich richtig? Punkt 1.1: "Bitte schlagt unbekannte Begriffe selbst nach."
herbivore
vielleicht hilft dir auch das hier weiter:
[FAQ] mehrere Programminstanzen verhindern (inkl. Parameterübergabe)
nils
Danke für eure Antwort, hab die Lösung selber gefunden, aber ehrlich gesagt weiß ich nicht, warum das Programm so reagiert.
Man muss einfach alles aus der methode "static void StartupNextInstanceHandler(....)" rauslöschen. Scheinbar weil der Argumentenaufruf schon im "static void main(...)" gemacht wird, wird automatisch die nachher gesendeten Parameter zur gleichen Methode gesenet - Warum weiß ich nicht.
public class SingleInstanceApplication : WindowsFormsApplicationBase
{
private SingleInstanceApplication()
{
base.IsSingleInstance = true;
}
public static void Run(Form f, StartupNextInstanceEventHandler startupHandler)
{
SingleInstanceApplication app = new SingleInstanceApplication();
app.MainForm = f;
app.StartupNextInstance += startupHandler;
app.Run(Environment.GetCommandLineArgs());
}
}
static class Program
{
private static Form1 Frm = null;
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main(String[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Frm = new Form1();
Frm.StartWithArguments(args);
SingleInstanceApplication.Run(Frm, StartupNextInstanceHandler);
}
static void StartupNextInstanceHandler(object sender, StartupNextInstanceEventArgs e)
{
}
}
MfG
Tonka