Toasts – Rev 23
?pathlinks?
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using System.Windows.Forms;
namespace Toasts
{
public class Toasts : IDisposable
{
private readonly CancellationToken _cancellationToken;
private readonly BufferBlock<ToastForm> _toastFormBufferBlock;
private IDisposable _toastFromActionBlockLink;
private Toasts()
{
}
public Toasts(CancellationToken cancellationToken) : this()
{
_cancellationToken = cancellationToken;
_toastFormBufferBlock = new BufferBlock<ToastForm>(new DataflowBlockOptions { CancellationToken = _cancellationToken});
var toastFormActionBlock = new ActionBlock<ToastForm>(DisplayForm, new ExecutionDataflowBlockOptions { CancellationToken = _cancellationToken });
_toastFromActionBlockLink = _toastFormBufferBlock.LinkTo(toastFormActionBlock);
}
public void Dispose()
{
if (_toastFromActionBlockLink != null)
{
_toastFromActionBlockLink.Dispose();
_toastFromActionBlockLink = null;
}
}
private static void DisplayForm(ToastForm toastForm)
{
var thread = new Thread(() =>
{
Application.Run(toastForm);
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
public async Task Queue(ToastForm toastForm)
{
await _toastFormBufferBlock.SendAsync(toastForm, _cancellationToken);
}
}
}
Generated by GNU Enscript 1.6.5.90.