什么是互斥体在C#.NET中或如何将其用作类,方法或属性 [英] what is mutex In C#.NET or how to use this as a class , method or property

查看:117
本文介绍了什么是互斥体在C#.NET中或如何将其用作类,方法或属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是互斥体在C#.NET中如何将其用作类,方法或属性

what is mutex In C#.NET or how to use this as a class , method or property

推荐答案

真的,输入google太多了为你而劳累?互斥锁是多线程应用程序中使用的同步对象。



如果谷歌C#什么是互斥锁,你将获得109,000个结果返回。我打赌你会从其中一个得到更详细的答案。
Really, is typing "google" too much of a strain for you? A mutex is a synchronization object used in multithreaded apps.

If google "C# what is a mutex", you'll get 109,000 results returned. I bet you'll get a more detailed answer from one of those.


这里有一些关于如何使用它的好文章:





纯.NET单实例应用解决方案 [ ^ ]



互斥过程标识符 [ ^ ]
Here are some good articles on how to use it:


A Pure .NET Single Instance Application Solution[^]

Mutex Process Identifier[^]


Mutex代表互斥。 C#语言提供Mutex类型,以确保某些代码块一次只执行一次。 Mutex类型可以创建可由其他进程打开的命名互斥锁。确保计算机上只能运行一个实例非常有用。



示例

这个简单示例使用Mutex.OpenExisting方法。如果OpenExisting方法抛出异常,则指定的名为Mutex不存在或不可访问。 IsSingleInstance方法使用此行为。



如果互斥锁不存在,则会创建一个新的互斥锁。然后程序的其他实例可以告诉已经有一个实例已经打开。



使用Mutex和OpenExisting的程序[C#]



使用System;

使用System.Threading;



class Program

{

静态互斥_m;



静态布尔IsSingleInstance()

{

尝试

{

//尝试打开现有的互斥锁。

Mutex.OpenExisting(PERL);

}

catch

{

//如果发生异常,则没有这样的互斥锁。

程序。 _m =新的Mutex(true,PERL);



//只有一个实例。

返回true;

}

//多个实例。

返回false;

}



static void Main()

{

if(!Program.IsSingleInstance())

{

Console.WriteLi ne(不止一个实例); //退出程序。

}

else

{

Console.WriteLine(One instance); //继续使用程序。

}

//保持打开状态。

Console.ReadLine();

}

}



结果



1.首次执行将显示一个实例

2.第二次执行将显示多个实例

3.第三次执行与第二次执行相同。



表现说明。不幸的是,该程序在第一个实例运行的常见情况下引发了一个例外。例外情况比许多结构慢得多。但是,如果您的程序是计算密集型的,那么避免额外的实例启动会更有利。



请注意:

MSDN上的OpenExisting示例也使用异常处理。



MSDN参考[Mutex.OpenExisting]

方法



完成后,在Mutex实例上调用Release有时很重要。在上面的简单示例中,这不是必需的,因为当程序退出时将释放Mutex。代码在不释放Mutex的情况下运行良好。



继续,Mutex类型提供WaitOne,WaitAll和WaitAny方法。 WaitOne方法阻止当前线程,直到Mutex被释放。调用Release方法时会发布。



摘要



Mutex类型提供了一种使用方法系统范围的同步。它还可以在一个程序中同步线程。



提示:

当Mutex有一个字符串名称时,它可以从其他名称打开使用OpenExisting进行处理。此行为可用于强制进程和线程之间的同步。





希望这会帮助你们....... br />
-avb
Mutex stands for mutual exclusion. The C# language provides the Mutex type to ensure that certain blocks of code are executed only once at a time. The Mutex type can create named mutexes that can be opened by other processes. It is useful for ensuring that only one instance can be run on the computer.

Example
This simple example uses the Mutex.OpenExisting method. If the OpenExisting method throws an exception, the specified named Mutex does not exist or is inaccessible. The IsSingleInstance method uses this behavior.

If the Mutex does not exist, it creates a new one. Further instances of the program then can tell that there is an instance already open.

Program that uses Mutex and OpenExisting [C#]

using System;
using System.Threading;

class Program
{
static Mutex _m;

static bool IsSingleInstance()
{
try
{
// Try to open existing mutex.
Mutex.OpenExisting("PERL");
}
catch
{
// If exception occurred, there is no such mutex.
Program._m = new Mutex(true, "PERL");

// Only one instance.
return true;
}
// More than one instance.
return false;
}

static void Main()
{
if (!Program.IsSingleInstance())
{
Console.WriteLine("More than one instance"); // Exit program.
}
else
{
Console.WriteLine("One instance"); // Continue with program.
}
// Stay open.
Console.ReadLine();
}
}

Result

1. First execution will display "One instance"
2. Second execution will display "More than one instance"
3. Third execution is the same as second.

Performance notes. The program unfortunately incurs one exception in the common case of the first instance being run. Exceptions are measurably slower than many constructs. However, if your program is computationally-intensive, it would be more beneficial to avoid extra instances being started.

Please note:
The OpenExisting examples on MSDN also use exception handling.

MSDN Reference [Mutex.OpenExisting]
Methods

It is sometimes important to call Release on your Mutex instance once you are done with it. In the simple example above this is not necessary as the Mutex will be released when the program exits. The code works well without releasing the Mutex.

Continuing on, the Mutex type provides the WaitOne, WaitAll and WaitAny methods. The WaitOne method blocks the current thread until the Mutex has been released. Releases occur when the Release method is called.

Summary

The Mutex type provides a way to use system-wide synchronization. It can also synchronize threads within a single program.

Tip:
When a Mutex has a string name, it can be opened from other processes with OpenExisting. This behavior can be used to enforce synchronization between processes and threads.


Hope this will help you guys.....
-avb


这篇关于什么是互斥体在C#.NET中或如何将其用作类,方法或属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆