C# 挂机锁

    昨天心血来了想起做一个挂机锁,起屏幕保护的作用!
    在。net 2008 的环境下很容易实现,但是有一个关键的地方,就是屏蔽掉 Ctrl+Alt+Del ,结果想了老半天,最后用 Timer 控件解决了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中的互斥是一种同步机制,用于保护共享资源的并发访问。当一个线程拥有互斥时,其他线程无法同时拥有该,只能等待该线程释放该后才能获得。这样可以防止多个线程同时访问共享资源,避免数据竞争和不可预测的行为。 在C#中,可以使用System.Threading命名空间中的Mutex类来创建互斥。下面是一个示例代码,演示了如何使用互斥来保护共享资源的访问: ``` using System; using System.Threading; public class Example { private static Mutex mutex = new Mutex(); // 创建互斥对象 public static void Main() { for (int i = 0; i < 5; i++) { Thread myThread = new Thread(new ThreadStart(MyThreadProc)); myThread.Name = String.Format("Thread{0}", i + 1); myThread.Start(); } } private static void MyThreadProc() { Console.WriteLine("{0} is waiting for the mutex", Thread.CurrentThread.Name); mutex.WaitOne(); // 等待互斥 Console.WriteLine("{0} has entered the critical section", Thread.CurrentThread.Name); // 访问共享资源 Console.WriteLine("{0} is leaving the critical section", Thread.CurrentThread.Name); mutex.ReleaseMutex(); // 释放互斥 } } ``` 在上面的示例中,创建了一个互斥对象mutex,并在MyThreadProc方法中使用mutex.WaitOne()等待互斥,使用mutex.ReleaseMutex()释放互斥。这样可以保证只有一个线程可以进入“critical section”,避免了多个线程同时访问共享资源的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值