1

I don't have .NET 4.5 installed (and I can't install it).

Where can I find the source code of the new System.Progress<T> class ?

(Or someone kind enough, with the .NET 4.5 beta installed, to copy/paste the code)

bbaia
  • 1,193
  • 7
  • 8
  • 1
    install 4.5, install demo of reflector or ilspy and et voila ... –  May 25 '12 at 07:53
  • I know, but I can't install it (I've edited my question) – bbaia May 25 '12 at 08:03
  • 1
    super requirement ... lol ... no chance for any other computer you can (ab)use for this? –  May 25 '12 at 08:03
  • you **probabbly** are seacrhing for job scheduler. In this case may be [Quartz.NET](http://quartznet.sourceforge.net/) can help you. – Tigran May 25 '12 at 08:05
  • btw - you can link your visual studio to the symbol-servers and step through the implementation .. –  May 25 '12 at 08:05
  • here for 4.6.2 https://referencesource.microsoft.com/#mscorlib/system/progress.cs,d23df0450d3fd0d6 – tym32167 Dec 16 '16 at 12:40

4 Answers4

6

Here is the output from ILSpy:

using System;
using System.Runtime;
using System.Threading;
namespace System
{
    [__DynamicallyInvokable]
    public class Progress<T> : IProgress<T>
    {
        private readonly SynchronizationContext m_synchronizationContext;
        private readonly Action<T> m_handler;
        private readonly SendOrPostCallback m_invokeHandlers;
        [__DynamicallyInvokable]
        public event EventHandler<T> ProgressChanged
        {
            [__DynamicallyInvokable]
            add
            {
                EventHandler<T> eventHandler = this.ProgressChanged;
                EventHandler<T> eventHandler2;
                do
                {
                    eventHandler2 = eventHandler;
                    EventHandler<T> value2 = (EventHandler<T>)Delegate.Combine(eventHandler2, value);
                    eventHandler = Interlocked.CompareExchange<EventHandler<T>>(ref this.ProgressChanged, value2, eventHandler2);
                }
                while (eventHandler != eventHandler2);
            }
            [__DynamicallyInvokable]
            remove
            {
                EventHandler<T> eventHandler = this.ProgressChanged;
                EventHandler<T> eventHandler2;
                do
                {
                    eventHandler2 = eventHandler;
                    EventHandler<T> value2 = (EventHandler<T>)Delegate.Remove(eventHandler2, value);
                    eventHandler = Interlocked.CompareExchange<EventHandler<T>>(ref this.ProgressChanged, value2, eventHandler2);
                }
                while (eventHandler != eventHandler2);
            }
        }
        [__DynamicallyInvokable]
        public Progress()
        {
            this.m_synchronizationContext = (SynchronizationContext.CurrentNoFlow ?? ProgressStatics.DefaultContext);
            this.m_invokeHandlers = new SendOrPostCallback(this.InvokeHandlers);
        }
        [__DynamicallyInvokable]
        public Progress(Action<T> handler) : this()
        {
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }
            this.m_handler = handler;
        }
        [__DynamicallyInvokable]
        protected virtual void OnReport(T value)
        {
            Action<T> handler = this.m_handler;
            EventHandler<T> progressChanged = this.ProgressChanged;
            if (handler != null || progressChanged != null)
            {
                this.m_synchronizationContext.Post(this.m_invokeHandlers, value);
            }
        }
        [__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        void IProgress<T>.Report(T value)
        {
            this.OnReport(value);
        }
        private void InvokeHandlers(object state)
        {
            T t = (T)((object)state);
            Action<T> handler = this.m_handler;
            EventHandler<T> progressChanged = this.ProgressChanged;
            if (handler != null)
            {
                handler(t);
            }
            if (progressChanged != null)
            {
                progressChanged(this, t);
            }
        }
    }
}
Palo Mraz
  • 61
  • 1
  • 1
4

Download the .NET Framework version 4.5 and install it on your PC. Then get a copy of something like dotPeek and use that to look at the source code for Progress.

http://www.microsoft.com/en-us/download/details.aspx?id=8483

http://www.jetbrains.com/decompiler/

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
  • 1
    Ahhh OK. Only option is use another PC where you can install it. Although the .NET source code is available, you cannot simply browse it like a public repository. You need to use something like .NET Reflector, or dotPeek, or even Visual Studio itself when configured to do so, to download the symbols for the .NET framework. That will allow you to view the source code. – Jason Evans May 25 '12 at 08:07
  • I was expecting someone with .NET 4.5 installed to do this for me as I don't have .NET 4.5. I can't install it because it overrides .NET 4 – bbaia May 25 '12 at 08:11
  • You can target .NET 4.0 even with .NET 4.5 installed, @bbaia. – Cody Gray - on strike May 25 '12 at 08:54
1

If you are interested on the original source code, Microsoft has made it avaliable on the internet. The source for the Progress classe can be found here:

https://referencesource.microsoft.com/#mscorlib/system/progress.cs

Teus
  • 11
  • 1
0

Source code taken from ReSharper Decompiler:

// Decompiled with JetBrains decompiler
// Type: System.Progress`1
// Assembly: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 85AB3664-E4CA-41A0-86E3-96342ED95AAA
// Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll

using System.Threading;

namespace System
{
  /// <summary>Provides an <see cref="T:System.IProgress`1" /> that invokes callbacks for each reported progress value.</summary>
  /// <typeparam name="T">Specifies the type of the progress report value.</typeparam>
  [__DynamicallyInvokable]
  public class Progress<T> : IProgress<T>
  {
    private readonly SynchronizationContext m_synchronizationContext;
    private readonly Action<T> m_handler;
    private readonly SendOrPostCallback m_invokeHandlers;

    /// <summary>Raised for each reported progress value.</summary>
    [__DynamicallyInvokable]
    public event EventHandler<T> ProgressChanged;

    /// <summary>Initializes the <see cref="T:System.Progress`1" /> object.</summary>
    [__DynamicallyInvokable]
    public Progress()
    {
      this.m_synchronizationContext = SynchronizationContext.CurrentNoFlow ?? ProgressStatics.DefaultContext;
      this.m_invokeHandlers = new SendOrPostCallback(this.InvokeHandlers);
    }

    /// <summary>Initializes the <see cref="T:System.Progress`1" /> object with the specified callback.</summary>
    /// <param name="handler">A handler to invoke for each reported progress value. This handler will be invoked in addition to any delegates registered with the <see cref="E:System.Progress`1.ProgressChanged" /> event. Depending on the <see cref="T:System.Threading.SynchronizationContext" /> instance captured by the <see cref="T:System.Progress`1" /> at construction, it is possible that this handler instance could be invoked concurrently with itself.</param>
    [__DynamicallyInvokable]
    public Progress(Action<T> handler)
      : this()
    {
      if (handler == null)
        throw new ArgumentNullException("handler");
      this.m_handler = handler;
    }

    /// <summary>Reports a progress change.</summary>
    /// <param name="value">The value of the updated progress.</param>
    [__DynamicallyInvokable]
    protected virtual void OnReport(T value)
    {
      // ISSUE: reference to a compiler-generated field
      if (this.m_handler == null && this.ProgressChanged == null)
        return;
      this.m_synchronizationContext.Post(this.m_invokeHandlers, (object) value);
    }

    [__DynamicallyInvokable]
    void IProgress<T>.Report(T value)
    {
      this.OnReport(value);
    }

    private void InvokeHandlers(object state)
    {
      T e = (T) state;
      Action<T> handler = this.m_handler;
      // ISSUE: reference to a compiler-generated field
      EventHandler<T> progressChanged = this.ProgressChanged;
      if (handler != null)
        handler(e);
      if (progressChanged == null)
        return;
      progressChanged((object) this, e);
    }
  }
}
Teus
  • 11
  • 1