// Form1.cs, aus MyComponentDemo
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
namespace MyComponent
{
public class Notifier: Component, INotifier
{
private string notifyText = "unbekannt";
public void Notify(string text)
{
this.notifyText = text;
SimuliereBerechnung(4);
}
private void SimuliereBerechnung(int seconds)
{
Form.ActiveForm.Cursor = Cursors.WaitCursor;
DateTime end = DateTime.Now.AddSeconds((double)seconds);
while (DateTime.Now < end)
{
Thread.Sleep(100); Application.DoEvents();
}
Form.ActiveForm.Cursor = Cursors.Default;
BerechnungFertig(this, new NotifyEventArgs(notifyText));
}
public event NotifyEventHandler BerechnungFertig;
public delegate void NotifyEventHandler(object sender, NotifyEventArgs e);
public class NotifyEventArgs : EventArgs
{
public NotifyEventArgs(string notifyText)
{
this.notifyMessage = notifyText;
}
string notifyMessage;
public string NotifyMessage
{
get { return notifyMessage; }
set { notifyMessage = value; }
}
}
}
}
// INotifier.cs, aus Projekt MyComponent using System;
namespace MyComponent
{
public interface INotifier
{
event Notifier.NotifyEventHandler BerechnungFertig;
void Notify(string text);
}
}
// Notifier.cs, aus Projekt MyComponent
using System;
using System.Windows.Forms;
using MyComponent;
namespace MyComponentDemo
{
public partial class Form1 : Form
{
INotifier iNotifier;
public Form1()
{
InitializeComponent();
iNotifier = (INotifier)notifier1;
iNotifier.BerechnungFertig += new Notifier.NotifyEventHandler(iNotifier_BerechnungFertig);
}
void iNotifier_BerechnungFertig(object sender, Notifier.NotifyEventArgs e)
{
MessageBox.Show("Die Berechnung in Komponente notifier1 ist fertig.\r\n"+
"Der Benachrichtigungstext war:" + e.NotifyMessage);
}
private void btnStart_Click(object sender, EventArgs e)
{
iNotifier.Notify("Hallo Welt!");
}
}
}