I read the mutex code, but it gives me errors like:
public override void dispose (bool disposing); no suitable method found
This happens when the component is initialized, so I commented on this part, but the main error that I encountered was part of the design:
The designer cannot process the code on line 26: throw new NotImplementedException (); The code inside the 'InitializeComponent' method is generated by the designer and should not be changed manually. Delete all changes and try opening the constructor again.
I can not view form.cs [design]. How can i fix this?
Program.cs:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using PU;
namespace WindowsApplication1
{
static class Program
{
[STAThread]
static void Main()
{
if (ProcessUtils.ThisProcessIsAlreadyRunning())
{
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
{
public static class ProcessUtils
{
private static Mutex mutex = null;
public static bool ThisProcessIsAlreadyRunning()
{
Debug.Assert(mutex == null);
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);
public static void SetFocusToPreviousInstance(string windowCaption)
{
IntPtr hWnd = FindWindow(null, windowCaption);
if (hWnd != null)
{
IntPtr hPopupWnd = GetLastActivePopup(hWnd);
if (hPopupWnd != null && IsWindowEnabled(hPopupWnd))
{
hWnd = hPopupWnd;
}
SetForegroundWindow(hWnd);
if (IsIconic(hWnd))
{
ShowWindow(hWnd, SW_RESTORE);
}
}
}
}
}
source
share