none
wpf 多线程访问一个数据容器用什么锁? RRS feed

答案

  • 您好!

    其实在WPF中与普通的.NET程序在线程一块是一样的。我们用lock关键字来保护我们的资源。请查看下面的Sample代码:

    C# Code:

    class Account
    {
    	private Object thisLock = new Object();
    	int balance;
    
    	Random r = new Random();
    
    	public Account(int initial)
    	{
    		balance = initial;
    	}
    
    	int Withdraw(int amount)
    	{
    
    		// This condition never is true unless the lock statement 
    		// is commented out. 
    		if (balance < 0)
    		{
    			throw new Exception("Negative Balance");
    		}
    
    		// Comment out the next line to see the effect of leaving out  
    		// the lock keyword. 
    		lock (thisLock)
    		{
    			if (balance >= amount)
    			{
    				Console.WriteLine("Balance before Withdrawal :  " + balance);
    				Console.WriteLine("Amount to Withdraw        : -" + amount);
    				balance = balance - amount;
    				Console.WriteLine("Balance after Withdrawal  :  " + balance);
    				return amount;
    			}
    			else
    			{
    				return 0; // transaction rejected
    			}
    		}
    	}
    
    	public void DoTransactions()
    	{
    		for (int i = 0; i < 100; i++)
    		{
    			Withdraw(r.Next(1, 100));
    		}
    	}
    }
    
    class Test
    {
    	static void Main()
    	{
    		Thread[] threads = new Thread[10];
    		Account acc = new Account(1000);
    		for (int i = 0; i < 10; i++)
    		{
    			Thread t = new Thread(new ThreadStart(acc.DoTransactions));
    			threads[i] = t;
    		}
    		for (int i = 0; i < 10; i++)
    		{
    			threads[i].Start();
    		}
    	}
    }
    

    关于C# Thread的更多内容,请参考MSDN文档,

    http://msdn.microsoft.com/en-us/library/system.threading.thread(v=vs.110).aspx

    感谢您对MSDN论坛的支持!

    祝您生活愉快!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 skczjun 2013年12月20日 6:30
    2013年12月20日 5:52
    版主

全部回复

  • lock
    2013年12月19日 10:16
  • WPF也是C#呀!多线程一样的。亲!lock之!
    2013年12月19日 12:57
  • 您好!

    其实在WPF中与普通的.NET程序在线程一块是一样的。我们用lock关键字来保护我们的资源。请查看下面的Sample代码:

    C# Code:

    class Account
    {
    	private Object thisLock = new Object();
    	int balance;
    
    	Random r = new Random();
    
    	public Account(int initial)
    	{
    		balance = initial;
    	}
    
    	int Withdraw(int amount)
    	{
    
    		// This condition never is true unless the lock statement 
    		// is commented out. 
    		if (balance < 0)
    		{
    			throw new Exception("Negative Balance");
    		}
    
    		// Comment out the next line to see the effect of leaving out  
    		// the lock keyword. 
    		lock (thisLock)
    		{
    			if (balance >= amount)
    			{
    				Console.WriteLine("Balance before Withdrawal :  " + balance);
    				Console.WriteLine("Amount to Withdraw        : -" + amount);
    				balance = balance - amount;
    				Console.WriteLine("Balance after Withdrawal  :  " + balance);
    				return amount;
    			}
    			else
    			{
    				return 0; // transaction rejected
    			}
    		}
    	}
    
    	public void DoTransactions()
    	{
    		for (int i = 0; i < 100; i++)
    		{
    			Withdraw(r.Next(1, 100));
    		}
    	}
    }
    
    class Test
    {
    	static void Main()
    	{
    		Thread[] threads = new Thread[10];
    		Account acc = new Account(1000);
    		for (int i = 0; i < 10; i++)
    		{
    			Thread t = new Thread(new ThreadStart(acc.DoTransactions));
    			threads[i] = t;
    		}
    		for (int i = 0; i < 10; i++)
    		{
    			threads[i].Start();
    		}
    	}
    }
    

    关于C# Thread的更多内容,请参考MSDN文档,

    http://msdn.microsoft.com/en-us/library/system.threading.thread(v=vs.110).aspx

    感谢您对MSDN论坛的支持!

    祝您生活愉快!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 skczjun 2013年12月20日 6:30
    2013年12月20日 5:52
    版主