2010-02-03 2 views
1

Ceci est mon code que j'avais essayé de lancer dans mon programme C# .net 3.5 mais j'ai des erreurs. Qu'est-ce que je fais mal?Comment exécuter le code mutex dans C# .net lors d'un clic sur un événement?

error CS0115: 'Form1.Dispose(bool)': no suitable method found to override

C'est le code où je suis l'erreur:

protected override void Dispose(bool disposing) 
{ 
    if (disposing) 
    { 
     this._userConnectionOption = null; 
     this._poolGroup = null; 
     this.close(); 
    } 
    this.DisposeMe(disposing); 
    base.Dispose(disposing); 
} 

Le codage réel commence ici:

Program.cs:

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:

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); 
       } 
      } 
     } 
    } 
+2

« Je reçois des erreurs » ne sont pas précis. Dites exactement * quelles * erreurs vous obtenez. –

+0

Trop de code, affichez le code SNIPPET (S) qui génère des erreurs et les erreurs réelles, s'il vous plaît. –

+0

Je suis passé par le code de mutex .. mais il me donne des erreurs comme: -public override void disposer (bool disposing); aucune méthode appropriée trouvée pour disposer .. ceci est ocuring sur initialiser le composant .. ainsi j'ai commenté cette partie .. mais l'erreur principale im faisant face est sur la partie de conception: - Le concepteur ne peut pas traiter le code à la ligne 26: throw new NotImplementedException() Le code dans la méthode 'InitializeComponent' est généré par le concepteur et ne doit pas être modifié manuellement. Veuillez supprimer les modifications et réessayer d'ouvrir le concepteur. je ne peux pas voir form.cs [la conception] plz me aider – zoya

Répondre

0

Créez votre mutex avec true comme le premier paramètre.

et insérez

if(!createdNew) 
    mutex.Close(); 

avant

return !createdNew; 
+0

merci monsieur mais mon erreur n'est toujours pas résolue je dois savoir sur l'erreur que j'ai eu sur le: - erreur CS0115: 'Form1.Dispose (bool)': aucune méthode appropriée trouvée pour remplacer – zoya

+0

Je suppose que vous avez dérivé votre formulaire de IDisposable et n'avez pas mis en œuvre Dispose() – dwo

+0

comment implémenter Plz élaborer? – zoya

0

Par défaut, une fenêtre de formulaire "(dans l'espace de noms System.Windows.Forms) implémente IDisposable (dans l'espace de noms System)

Mais à partir du message d'erreur, il semble que votre "Form1" ne prolonge pas la classe "Form". Par conséquent, dans votre code, le compilateur se plaint de l'absence de méthode "over-riddable".

Pourriez-vous s'il vous plaît votre code aussi poster 'Form1'?

Questions connexes