using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EventTest
{
class Program
{
static void Main(string[] args)
{
//Implementierung
Langerprozess.ProgressChanged += Langerprozess_ProgressChanged;
//Start langer Prozess..
Langerprozess.Run(1000);
}
/// <summary>
/// Ereignisbehandlung
/// </summary>
/// <param name="sender"></param>
/// <param name="e">object ProgressEventArgs</param>
private static void Langerprozess_ProgressChanged(object sender, ProgressEventArgs e)
{
Console.Clear();
switch (e.EventTyp)
{
case ProgressEventArgs.eEventTyp.Progress:
Console.WriteLine(e.Message + e.Prozent + "%");
break;
case ProgressEventArgs.eEventTyp.Start:
case ProgressEventArgs.eEventTyp.Abbruch:
case ProgressEventArgs.eEventTyp.Error:
case ProgressEventArgs.eEventTyp.Ende:
Console.WriteLine(e.Message);
break;
default:
break;
}
}
}
class Langerprozess
{
#region Events..
/// <summary>
/// Ereignis - Fortschritt
/// </summary>
public static event EventHandler<ProgressEventArgs> ProgressChanged;
/// <summary>
/// Ereignisbehandlungsmethode Prozess-Fortschritt
/// </summary>
/// <param name="e">ProgressEventArgs e</param>
public static void OnProgressChanged(ProgressEventArgs e)
{
if (ProgressChanged != null) ProgressChanged(null, e);
}
#endregion
public static void Run(int Count)
{
OnProgressChanged(new ProgressEventArgs(ProgressEventArgs.eEventTyp.Progress, "Start"));
for (int i = 0; i < Count; i++)
{
ProgressEventArgs Pea = new ProgressEventArgs(ProgressEventArgs.eEventTyp.Progress, "Verarbeitet:", (100.00f * i) / Count);
OnProgressChanged(Pea);
if (Pea.Cancel)
{
OnProgressChanged(new ProgressEventArgs(ProgressEventArgs.eEventTyp.Abbruch, "Abbruch durch Benutzer"));
return;
}
System.Threading.Thread.Sleep(10);
}
OnProgressChanged(new ProgressEventArgs(ProgressEventArgs.eEventTyp.Progress, "Start"));
}
}
/// <summary>
/// Eigenes Ereignis
/// </summary>
class ProgressEventArgs:EventArgs
{
public enum eEventTyp {Start, Progress, Abbruch, Error, Ende }
public eEventTyp EventTyp { get; set; }
public string Message { get; set; }
public float Prozent { get; set; }
public bool Cancel { get; set; }
public object Tag { get; set; }
public ProgressEventArgs() { }
public ProgressEventArgs(eEventTyp eventtyp, string message) : this(eventtyp, message, 0, false, null)
{
}
public ProgressEventArgs(eEventTyp eventtyp, string message,float prozent):this(eventtyp, message, prozent, false, null)
{
}
public ProgressEventArgs(eEventTyp eventtyp, string message, float prozent,bool cancel) : this(eventtyp, message, prozent, false, null)
{
}
public ProgressEventArgs(eEventTyp eventtyp, string message, float prozent, bool cancel, object tag)
{
this.EventTyp = eventtyp;
this.Message = message;
this.Prozent = prozent;
this.Cancel = cancel;
this.Tag = tag;
}
}
}