【読書ノート】イベントイベントイベント
2755 ワード
, 。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Event
{
class Program
{
static void Main(string[] args)
{
LongTask It = new LongTask();
It.OnNotifyProgress += new LongTask.NotifyProgressDelegate(It_OnNotifyProgress);
It.PerformTask();
Console.ReadLine();
}
static void It_OnNotifyProgress(ProgressArgs pa)
{
Console.WriteLine("Progress on Long Task Complete. {0}% Complete.", pa.PercentComplete);
}
}
class LongTask
{
public delegate void NotifyProgressDelegate(ProgressArgs pa); // Declare a delegate
public event NotifyProgressDelegate OnNotifyProgress; // Create an event
public void PerformTask()
{
for (int i = 0; i < 10000; i++)
{
//Perform some processing
//...
//notify subscribers that progress was made
if (i % 100 == 0)
{
OnNotifyProgress(new ProgressArgs((int)i / 100));
}
}
}
}
class ProgressArgs
{
public int PercentComplete;
public ProgressArgs(int pctComplete)
{
PercentComplete = pctComplete;
}
}
}