나는 뮤텍스, 의 코드를 통해 갔다 그러나 그것은처럼 나에게 오류를주고있다 : (폐기 BOOL)C# 프로그램에서 뮤텍스 코드를 사용하는 방법은 무엇입니까?
공공 재정의 무효 처분; 더 적합한 방법이 초기화 구성 요소에 발생하는
처분 찾을 수 없습니다, 그래서 그 부분, 를 댓글을 달았지만 내가 직면하고있어 주요 오류가 디자인 뷰 부분에 :
디자이너 26 행에서 코드를 처리 할 수 없습니다 : throw 새 NotImplementedException(); 'InitializeComponent'메서드 내의 코드는 디자이너가 생성하므로 수동으로 수정해서는 안됩니다. 변경 사항을 제거하고 디자이너를 다시 열어보십시오.
form.cs [디자인]을 볼 수 없습니다. 이 문제를 어떻게 해결할 수 있습니까?
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);
}
}
}
}
}
form.cs의 디자인보기를 열 수없는 경우 해당 클래스의 코드를 붙여 넣으면 쿼리 해결에 도움이 될 수 있습니다. – shahjapan