Description





Description

Compilers are required to not preserve this type in metadata as a custom attribute. Instead, compilers are required to emit it directly in the file format, as described in Partition II of the CLI Specification. Metadata consumers, such as the Reflection API, are required to retrieve this data from the file format and return it as if it were a custom attribute.

[Note: This class uses the MethodImplOptions enumeration to describe the implementation details of methods. For most languages, it is recommended that the notions of "forward" and "synchronized" be attached to methods using language syntax instead of custom attributes.]

Example

using System;

using System.Runtime.CompilerServices;

using System.Threading;



/// <summary>

/// Sample demonstrating the use of the MethodImplAttribute class.

/// Use this attribute to provide information about a method implementation.

/// </summary>

internal class MethodImplAttributeSample

{



    private static void Main()

    {

        DemoClass demo = new DemoClass();



        // Use a couple of threads from the thread pool to call the 

        // synchronized an unsynchronized methods on DemoClass.

        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc1), demo);

        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc2), demo);

        Thread.Sleep(8000);



        Console.WriteLine();

        Console.WriteLine();

        Console.WriteLine("Press Enter to continue");

        Console.ReadLine();

    }



    // ThreadProc1 will call GetStatus first but, because of a shorter delay, 

    // ThreadProc2 will call GetStatusSynchronized first. The result is that 

    // ThreadProc1 will be blocked from calling GetStatusSynchronized until 

    // the call finishes on ThreadProc2.

    private static void ThreadProc1(object stateInfo)

    {

        DemoClass demo = (DemoClass)stateInfo;



        demo.GetStatus(1, 1500);

        demo.GetStatusSynchronized(1, 2000);

    }



    private static void ThreadProc2(object stateInfo)

    {

        DemoClass demo = (DemoClass)stateInfo;



        demo.GetStatus(2, 1000);

        demo.GetStatusSynchronized(2, 3000);

    }



    private class DemoClass

    {

        static int syncCount = 0;

        static int count     = 0;



        [MethodImpl(MethodImplOptions.Synchronized)]

        public void GetStatusSynchronized(int threadIndex, int delay)

        {

            Console.WriteLine(

                "Thread {0} entering GetStatusSynchronized", threadIndex);

            syncCount++;

            Console.WriteLine("  {0} thread(s) in critical section", syncCount);

            Thread.Sleep(delay);

            syncCount--;

            Console.WriteLine(

                "Thread {0} leaving GetStatusSynchronized", threadIndex);

        }



        public void GetStatus(int threadIndex, int delay)

        {

            Console.WriteLine("Thread {0} entering GetStatus", threadIndex);

            count++;

            Console.WriteLine("  {0} thread(s) in critical section", count);

            Thread.Sleep(delay);

            count--;

            Console.WriteLine("Thread {0} leaving GetStatus", threadIndex);

        }



    }



}


The output is


Thread 1 entering GetStatus

  1 thread(s) in critical section

Thread 2 entering GetStatus

  2 thread(s) in critical section

Thread 2 leaving GetStatus

Thread 2 entering GetStatusSynchronized

  1 thread(s) in critical section

Thread 1 leaving GetStatus

Thread 2 leaving GetStatusSynchronized

Thread 1 entering GetStatusSynchronized

  1 thread(s) in critical section

Thread 1 leaving GetStatusSynchronized





Press Enter to continue



     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows