FIX: The performance of a static synchronized method is slow in the .NET Framework 1.0

Article translations Article translations
Article ID: 328811 - View products that this article applies to.
This article was previously published under Q328811
Expand all | Collapse all

On This Page


When multiple threads try to access a static synchronized method at the same time in Microsoft Visual Studio .NET 2002, the performance of the synchronized method slows down.

Note A static synchronized method is a method that uses the MethodImpl(MehtodImplOptions.Synchronized) attribute.


To resolve this problem, obtain the latest service pack for the Microsoft .NET Framework 1.0.
To obtain the latest service pack, visit the following Microsoft Web site:


Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section. This problem was first corrected in Microsoft .NET Framework 1.0 Service Pack 3.


Steps to reproduce the behavior

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project. The New Project dialog box appears.
  3. Under Project Types, click Visual Basic Projects or click Visual C# Projects.
  4. Under Templates, click Console Application.
  5. In the Name box, type MyApp, and then click OK. By default, one of the following files is created:
    • If you are using Microsoft Visual Basic .NET, a file that is named Module1.vb is created.
    • If you are using Microsoft Visual C# .NET, a file that is named Class1.cs is created.
  6. In the file that was created in step 5, replace the existing code with the following code.

    Visual Basic .NET code
    Imports System
    Imports System.Threading
    Imports System.Runtime.CompilerServices
    Namespace StaticSynchronizer
       Class Test
            Shared StopThread As Thread
            Shared Sub Main()
                'Create two threads.
                Dim threads(1) As Thread
                Dim i As Integer
                Dim tstart As New ThreadStart(AddressOf (New Test()).run)
                For i = 0 To threads.Length - 1
                    threads(i) = New Thread(tstart)
                    threads(i).Name = "Thread " + i.ToString
                    'Start the threads.
                If (threads.Length > 1) Then
                    StopThread = threads(0)
                End If
            End Sub
            Public Sub run()
                    While (True)
                        Dim start As Integer = Environment.TickCount
                        Dim i As Integer
                        For i = 0 To 5000
                        Dim endno As Long = Environment.TickCount
                        Console.Out.WriteLine(Thread.CurrentThread.Name + ": " + _
                        (endno - start).ToString + " ms")
                        If (Thread.CurrentThread.Equals(StopThread)) Then
                            Exit While
                        End If
                    End While
                    Console.Out.WriteLine(Thread.CurrentThread.Name + " exiting.")
                End Try
            End Sub
            'This is the synchronized method.
            <MethodImpl(MethodImplOptions.Synchronized)> _
            Private Shared Sub SynchronizedMethod()
            End Sub
        End Class
    End Namespace
    Visual C# .NET code
    using System;
    using System.Threading;
    using System.Runtime.CompilerServices;
    namespace StaticSynchronizer
       class Test
           static Thread StopThread = null;
           static void Main(string[] args)
              //Create two threads.
              Thread[] threads = new Thread[2];		
              for (int i = 0; i < threads.Length; i++) 
                   threads[i] = new Thread(new ThreadStart(new Test().run));
                   threads[i].Name = "Thread "+i;
                   //Start the threads.
              if (threads.Length > 1) 
                  StopThread = threads[0];
           public void run()
                    while (true) 
                         int start = Environment.TickCount;
                         for (int i = 0; i < 5000; i++) 
                         long end = Environment.TickCount;
                         Console.Out.WriteLine(Thread.CurrentThread.Name+": "+(end-start)+" ms");
                         if (Thread.CurrentThread.Equals(StopThread))
                    Console.Out.WriteLine(Thread.CurrentThread.Name+" exiting.");
          //This is the synchronized method.
          private static void SynchronizedMethod()
  7. Use one of the following procedures:
    • If you are using Visual Basic .NET, follow these steps to set the startup object:
      1. In Solution Explorer, right-click MyApp, and then click Properties. The MyApp Property Pages dialog box appears.
      2. In the Startup object box, select Sub Main, and then click OK.
    • If you are using Visual C# .NET, go to step 8.
  8. On the Build menu, click Build Solution.
  9. On the Debug menu, click Start.
In the output, notice that the call to the synchronized method takes several seconds to be completed.

For additional information, click the following article number To view the article In the Microsoft Knowledge Base:
824684 Description of the standard terminology that Is used To describe Microsoft software updates


For more information about MethodImplOptions enumeration, visit the following Microsoft Developer Network (MSDN) Web site:


Article ID: 328811 - Last Review: December 30, 2006 - Revision: 1.4
  • Microsoft .NET Framework 1.0
kbhotfixserver kbqfe kbthread kbperformance kbqfe kbnetframe100presp3fix kbbug kbfix KB328811

Give Feedback


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from