ich mᅵchte einen Doppeltstart meiner kleinen Anwendung auf einem
WindowsCE 5.0 Gerᅵt verhindern.
Probiert hab ich :
> VS 2008 C#
> Process[] processes = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
> if (processes.Length == 1)
> {
> Application.EnableVisualStyles();
> Application.SetCompatibleTextRenderingDefault(false);
> Application.Run(new Form1());
> }
> else
> {
> MessageBox.Show("Die Anwendung darf nur einmal gestartet werden.", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
> Application.Exit();
> }
und
> //System.Threading.Mutex Mu = new System.Threading.Mutex(false, "{86E6517F-71A5-4ea6-A4E6-DA3AF2932C97}");
> //if (Mu.WaitOne(0, false))
> // Application.Run(new frmMainForm());
> //else
> // MessageBox.Show("Dieses Programm ist bereits geᅵffnet");
beides geht nicht, da das Framework von CE das so nicht unterstᅵtzt.
Kann mir jemand einen Tip geben.
Gruᅵ KT
Am Wed, 27 Jan 2010 15:45:41 +0100 schrieb Karl Twele:
> Hallo,
>
> ich m�chte einen Doppeltstart meiner kleinen Anwendung auf einem
> WindowsCE 5.0 Ger�t verhindern.
>
using System.Runtime.InteropServices;
[DllImport("coredll", EntryPoint="CreateMutexW",
SetLastError=true)]
private static extern IntPtr CreateMutex(
IntPtr lpMutexAttributes, bool InitialOwner,
string MutexName);
liefert das Gew�nschte.
HTH
R�diger
Hallo R�diger,
danke f�r die prompte Hilfe, allerdings bin ich ein Anf�nger und wei�
nicht genau wie ich dieses CreateMutex jetzt benutzen kann. Die
Parameter lpMutexAttributes und InitialOwner sagen mir nichts. Als
MutexName kann ich wahrscheinlich die Guid benutzen. Wenn du mir den
Code entsprechend �nderst, versteh ich das vielleicht.
Gru�
KT
> using System;
> using System.Diagnostics;
> using System.Collections.Generic;
> using System.Windows.Forms;
> using System.Runtime.InteropServices;
>
>
> namespace DKFDatalogicMemor
> {
> static class Program
> {
> /// <summary>
> /// Der Haupteinstiegspunkt f�r die Anwendung.
> /// </summary>
> [MTAThread]
> [DllImport("coredll", EntryPoint = "CreateMutexW", SetLastError = true)]
> private static extern IntPtr CreateMutex(IntPtr lpMutexAttributes, bool InitialOwner, string MutexName);
>
> static void Main()
> {
> if (CreateMutex(??lpMutexAttributes??, ??false??, "9e24aa2d-2b14-45f9-ad42-dd7eaf7b042d" == ??)
> Application.Run(new frmMainForm());
> else
> MessageBox.Show("Dieses Programm ist bereits ge�ffnet");
> }
> }
> }
ok, hier mal der vollst�ndige Code.
Habe ich jetzt nur so runtergeschrieben und nicht getestet. Aber selbst
wenn Fehler drin sind, sollte Dir das zumindest alle Ans�tze liefern, die
Du brauchst:
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace DKFDatalogicMemor
{
static class Program
{
private const int ERROR_ALREADY_EXISTS = 183;
/// <summary>
/// Der Haupteinstiegspunkt f�r die Anwendung.
/// </summary>
[MTAThread]
static void Main()
{
try
{
PruefeMutex();
Application.Run(new frmMainForm());
}
catch ( Exception ex )
{
MessageBox.Show(ex.Message,"Fehler");
Application.Exit();
}
}
private static void PruefeMutex()
{
IntPtr mutex = CreateMutex(IntPtr.Zero, true,
"9e24aa2d-2b14-45f9-ad42-dd7eaf7b042d");
if (mutex != null && mutex != IntPtr.Zero)
{
int lastError = GetLastError();
switch (lastError)
{
case 0: // alles in Ordnung
break;
case ERROR_ALREADY_EXISTS:
Throw new Exception("Programm l�uft schon!");
break;
default:
Throw new Exception("Fehler beim Erstellen des
Mutex\r\nFehler:" + lastError.ToString());
break;
}
}
else
{
Throw new Exception("Fehler beim Erstellen des Mutex");
}
return;
}
[DllImport("CoreDll.dll")]
public extern static Int32 GetLastError();
[DllImport("coredll", EntryPoint = "CreateMutexW", SetLastError =
true)]
private static extern IntPtr CreateMutex(IntPtr lpMutexAttributes,
bool InitialOwner, string MutexName);
}
}
Sch�ne Gr��e
R�diger
Hallo R�diger,
sieht jetzt aus wie unten und funktioniert prima. Besten Dank f�r die
M�he, wir lesen mit unseren Barcodelesescanner die BDE Daten in unserer
Produktion f�r Kronenkorken ein. Wenn du also in Zukunft einen
Kronenkorken mit der Kennzeichnung DKF �ffnest, bist du ma�geblich an
der Produktion beteiligt.
Gru� KT
>
> using System;
> using System.Diagnostics;
> using System.Collections.Generic;
> using System.Windows.Forms;
> using System.Runtime.InteropServices;
>
>
> namespace DKFDatalogicMemor
> {
> static class Program
> {
> [DllImport("CoreDll.dll")]
> public extern static Int32 GetLastError();
>
> [DllImport("coredll", EntryPoint = "CreateMutexW", SetLastError = true)]
> private static extern IntPtr CreateMutex(IntPtr lpMutexAttributes, bool InitialOwner, string MutexName);
>
> private const int ERROR_ALREADY_EXISTS = 183;
>
> /// <summary>
> /// Der Haupteinstiegspunkt f�r die Anwendung.
> /// </summary>
> [MTAThread]
> static void Main()
> {
> try
> {
> if (PruefeMutex())
> Application.Run(new frmMainForm());
> else
> Application.Exit();
>
> }
> catch (Exception ex)
> {
> MessageBox.Show(ex.Message, "Fehler");
> Application.Exit();
> }
> }
>
> private static bool PruefeMutex()
> {
> IntPtr mutex = CreateMutex(IntPtr.Zero, true, "9e24aa2d-2b14-45f9-ad42-dd7eaf7b042d");
> if (mutex != null && mutex != IntPtr.Zero)
> {
> int lastError = GetLastError();
> switch (lastError)
> {
> case 0: // alles in Ordnung
> return true;
>
> case ERROR_ALREADY_EXISTS:
> return false;
>
> default:
> return false;
> }
> }
> else
> {
> return false;
> }
> }
> }
> }
Hallo,
weshalb eigentlich doppelter Start? Soweit ich weiß, wird eine Anwendung
beim Schließen unter Windows Mobile standardmäßig in den Hintergrund gelegt
und nicht beendet. Ein erneuter Start aktiviert die Anwendung lediglich
wieder!? Ein doppelter Programmstart sollte so nicht möglich sein, oder?
Ich stehe vor dem Problem, dass ich auf irgendeine Weise abfragen möchte, ob
die Anwendung aus dem Hintergrund aktiviert wurde. Bis jetzt hab ich dafür
leider keine Lösung gefunden.
Karsten