Results 1 to 3 of 3

Thread: Lock Statement

  1. #1
    Garcíarobine is offline Senior Member
    Join Date
    Dec 2009
    Posts
    334
    Rep Power
    3

    Default Lock Statement

    Lock Statement of C#

    I am new in programming word. I am learning # programming language. I am aware about the basic statements of C#. But "lock statement" seems very difficult to understand as I have not at all used this statement. I think this statement is may be used in multithreading concept but I am not sure on this. Please help me.

  2. #2
    RodríguezBrown is offline Senior Member
    Join Date
    Dec 2009
    Posts
    322
    Rep Power
    3

    Default

    In C# the 'lock Statement' is defined using 'lock' keyword, this type of C# statement to make important section. This can be getting by applying mutual exclusion lock over class object. Once execution of statement block finally you want to release the applied lock. Following is the syntax for lock statement in C#:

    lock(expression) block_of_statement
    Last edited by nitesh14; 02-06-2010 at 03:25 PM.

  3. #3
    WilsonMartin is offline Senior Member
    Join Date
    Dec 2009
    Posts
    319
    Rep Power
    3

    Default

    Follow the below code which is help you to understand Lock Statement of C#:

    namespace Lock
    {
    class Program
    {
    static void Main(string[] args)
    {
    Thread[] threads = new Thread[10];
    Account acc = new Account(1000);
    for (int i = 0; i < 10; i++)
    {
    Thread t = new Thread(new ThreadStart(acc.MyTransactions));
    threads[i] = t;
    }
    for (int i = 0; i < 10; i++)
    {
    threads[i].Start();
    }
    Console.Read();
    }
    }
    class Account
    {
    private Object myLock = new Object();
    int Bal;
    Random rnd = new Random();
    public Account(int iniAmount)
    {
    Bal = iniAmount;
    }
    int Withdraw(int amt)
    {
    // This condition will never be true unless the lock statement
    // is commented out:
    if (Bal < 0)
    {
    throw new Exception("Negative Balance");
    }
    // Comment out the next line to see the effect of leaving out
    // the lock keyword:
    lock (myLock)
    {
    if (Bal >= amt)
    {
    Console.WriteLine("Balance before Withdrawal : " + Bal);
    Console.WriteLine("Amount to Withdraw : -" + amt);
    Bal = Bal - amt;
    Console.WriteLine("Balance after Withdrawal : " + Bal);
    return amt;
    }
    else
    {
    return 0; // transaction rejected
    }
    }
    }
    public void MyTransactions()
    {
    for (int i = 0; i < 100; i++)
    {
    Withdraw(rnd.Next(1, 100));
    }
    }
    }
    }
    Last edited by nitesh14; 02-06-2010 at 03:25 PM.

Similar Threads

  1. Statement in visual basic
    By PowellParker in forum Programming
    Replies: 1
    Last Post: 05-27-2010, 02:29 PM
  2. Print a Dynamic SQL Statement
    By BakerJones in forum Programming
    Replies: 0
    Last Post: 03-11-2010, 05:15 PM
  3. fixed statement
    By AndersonDiaz in forum Programming
    Replies: 2
    Last Post: 02-03-2010, 01:06 PM
  4. statement in java program
    By PetersonClark in forum Programming
    Replies: 1
    Last Post: 01-27-2010, 02:50 PM
  5. Flash Action Script - Add Statement
    By interworld in forum Programming
    Replies: 0
    Last Post: 10-26-2008, 02:08 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO by SubmitEdge

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48