internal delegate void ProgressHandler(object sender, ProgressEventArgs e);
internal delegate void ProcessStatusHandler(object sender, ProcessStatusEventArgs e);
public partial class MyWindowsForm : System.Windows.Forms.Form
{
internal ProgressHandler myProgressHandler;
internal ProcessStatusHandler myProcessStatusHandler;
public MyWindowsForm()
{
InitializeComponent();
myProgressHandler = new ProgressHandler(UpdateProgress);
myProcessStatusHandler = new ProcessStatusHandler(SetStatus);
}
internal void UpdateProgress(object sender, ProgressEventArgs e)
{
myProgressBar.Increment(e.IncrementValue);
}
internal void SetStatus(object sender, ProcessStatusEventArgs e)
{
myStatusLabel.Text = e.ProcessStatus;
}
private void button1_Click(object sender, System.EventArgs e)
{
System.Threading.Thread myThread = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadFunction));
myThread.Start();
}
private void ThreadFunction()
{
MyProcessor myThreadClassObject = new MyProcessor(this);
myThreadClassObject.Run();
}
}
internal class ProgressEventArgs : System.EventArgs
{
private int _incrementValue;
internal int IncrementValue { get { return _incrementValue; } }
internal ProgressEventArgs(int incrementValue) { _incrementValue = incrementValue; }
}
internal class ProcessStatusEventArgs : System.EventArgs
{
private string _processStatus;
internal string ProcessStatus { get { return _processStatus; } }
internal ProcessStatusEventArgs(string processStatus) { _processStatus = processStatus; }
}
internal class MyProcessor
{
MyWindowsForm myWindowsForm;
internal MyProcessor(MyWindowsForm myForm)
{
myWindowsForm = myForm;
}
internal void Run()
{
for (int i = 0; i < 10; i++)
{
myWindowsForm.Invoke(myWindowsForm.myProgressHandler, new System.Object[] { this, new ProgressEventArgs(10) });
System.Threading.Thread.Sleep(500);
}
myWindowsForm.Invoke(myWindowsForm.myProcessStatusHandler, new System.Object[] { this, new ProcessStatusEventArgs("Process completed successfully") });
}
}