You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.5 KiB
68 lines
1.5 KiB
#if NET20 || NET30 || NET35 || NET40 || !NET_4_6 |
|
|
|
using System.Threading; |
|
|
|
namespace System |
|
{ |
|
// This class is new in .NET 4.5 |
|
public class Progress<T> : IProgress<T> |
|
{ |
|
private readonly Action<T> _post; |
|
|
|
public Progress() |
|
{ |
|
var context = SynchronizationContext.Current; |
|
if (context == null) |
|
{ |
|
_post = value => |
|
{ |
|
ThreadPool.QueueUserWorkItem(Callback, value); |
|
}; |
|
} |
|
else |
|
{ |
|
_post = value => |
|
{ |
|
context.Post(Callback, value); |
|
}; |
|
} |
|
} |
|
|
|
public Progress(Action<T> handler) |
|
: this() |
|
{ |
|
if (handler == null) |
|
{ |
|
throw new ArgumentNullException("handler"); |
|
} |
|
ProgressChanged += (sender, args) => handler(args); |
|
} |
|
|
|
internal event LinqInternal.Core.NewEventHandler<T> ProgressChanged; |
|
|
|
public void Report(T value) |
|
{ |
|
OnReport(value); |
|
} |
|
|
|
protected virtual void OnReport(T value) |
|
{ |
|
if (ProgressChanged != null) |
|
{ |
|
_post(value); |
|
} |
|
} |
|
|
|
private void Callback(object value) |
|
{ |
|
var valueT = (T)value; |
|
var progressChanged = ProgressChanged; |
|
if (progressChanged != null) |
|
{ |
|
progressChanged.Invoke(this, valueT); |
|
} |
|
} |
|
} |
|
} |
|
|
|
#endif |