스레딩 here과 관련하여이 기사를 읽었습니다. Mutex 섹션에 다음 코드가 있습니다. 나는이 코드 내가 EXE (위의 생성 된 EXE)를 실행하면 두 번이 를 인쇄해야합니다, 예상대로 작동하지 않는 실행할 때블록을 사용하여 뮤텍스가 다르게 동작합니다.
class OneAtATimePlease
{
static void Main()
{
// Naming a Mutex makes it available computer-wide. Use a name that's
// unique to your company and application (e.g., include your URL).
using (var mutex = new Mutex (false, "oreilly.com OneAtATimeDemo"))
{
// Wait a few seconds if contended, in case another instance
// of the program is still in the process of shutting down.
if (!mutex.WaitOne (TimeSpan.FromSeconds (3), false))
{
Console.WriteLine ("Another app instance is running. Bye!");
return;
}
RunProgram();
}
}
static void RunProgram()
{
Console.WriteLine ("Running. Press Enter to exit");
Console.ReadLine();
}
}
는 그러나 "다른 응용 프로그램 인스턴스는 안녕. 실행 중입니다!". 그러나 그 블록으로가는 것은 아닙니다.
그러나 사용하는 블록을 제거하면 코드가 예상대로 올바르게 동작합니다.
도와주세요. 나는 스레딩 개념에 익숙하지 않다.
UPDATE 오해의 소지에 대한
죄송합니다. 나는 실제로 그 기사에 정확하게 쓰여진 것처럼하지 않았다.
내 코드로되고,
internal class Program
{
/// <summary>
/// Entry point
/// </summary>
/// <param name="args">The arguments.</param>
internal static void Main(string[] args)
{
//Misc.RunAssemblies();
var threadingDemo = new ThreadingDemo();
threadingDemo.MutexDemo();
Console.ReadLine();
}
}
namespace LearnNet.Console
{
using System.Threading;
public class ThreadingDemo
{
public void MutexDemo()
{
using (var mutex = new Mutex(false, "Global\\oreilly.com OneAtATimeDemo"))
{
// Wait a few seconds if contended, in case another instance
// of the program is still in the process of shutting down.
if (!mutex.WaitOne(0, false))
{
System.Console.WriteLine("Another app instance is running. Bye!");
return;
}
RunProgram();
}
}
static void RunProgram()
{
System.Console.WriteLine("Running. Press Enter to exit");
}
}
}
이 제가 위에서 설명 된 문제를 재생한다. 그리고 내가 사용하는 블록을 제거하면 올바르게 동작합니다.
첫 번째 코드가 제대로 작동하고 두 번째 인스턴스를 실행할 때 "Bye"가 인쇄됩니다. – Evk
안녕하세요, 답장을 보내 주셔서 감사합니다. 문제가 생겼어. 나는 그 소식을 업데이트 할 것이다. – shanmugharaj
@Evk 업데이트 된 질문을 확인하십시오. 문제가 재현됩니다. – shanmugharaj