User Tools

Site Tools


processing_form_template

Processing Form Template

using System;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
 
public partial class ProcessingFormTemplate : Form
{
	private const string StartButtonText = "Start";
	private const string StopButtonText = "Stop";
 
	private Processor _processor;
	private Processor Processor
	{
		get
		{
			if (_processor == null)
			{
				_processor = new Processor();
				_processor.LogMessageEvent += new EventHandler<LogMessageEventArgs>(Processor_LogMessage);
				_processor.ProcessingCompleteEvent += new EventHandler(Processor_ProcessingComplete);
			}
 
			return _processor;
		}
	}
 
	public ProcessingFormTemplate()
	{
		InitializeComponent();
	}
 
	private void buttonStartStopProcessing_Click(object sender, EventArgs e)
	{
		Debug.Assert(sender == buttonStartStopProcessing, "Incorrect object triggered a click event.");
 
		switch (buttonStartStopProcessing.Text)
		{
			case StartButtonText:
				UpdateUIForStartStopProcessing(true);
				StartProcessing();
				break;
 
			case StopButtonText:
				UpdateUIForStartStopProcessing(false);
				StopProcessing();
				break;
 
			default:
				Debug.Assert(true, "The object buttonStart has invalid Text.");
 
				// Ignore the click.
				break;
		}
	}
 
	private void StopProcessing()
	{
		Processor.StopProcessing();
	}
 
	private void StartProcessing()
	{
		ProcessorOptions processorOptions = GetCurrentOptions();
 
		Processor.StartProcessing(processorOptions);
	}
 
	private ProcessorOptions GetCurrentOptions()
	{
		ProcessorOptions processorOptions = new ProcessorOptions();
 
		processorOptions.SomeOption = checkBoxSomeOption.Checked;
 
		return processorOptions;
	}
 
	private delegate void UpdateUIForStartStopProcessingCallback(bool IsProcessing);
	private void UpdateUIForStartStopProcessing(bool IsProcessing)
	{
		if (this.buttonStartStopProcessing.InvokeRequired || this.checkBoxSomeOption.InvokeRequired)
		{
			UpdateUIForStartStopProcessingCallback callback = new UpdateUIForStartStopProcessingCallback(UpdateUIForStartStopProcessing);
			this.Invoke(callback, new object[] { IsProcessing });
		}
		else
		{
			foreach (Control control in this.Controls)
			{
				if (control != buttonStartStopProcessing)
					control.Enabled = !IsProcessing;
			}
 
			buttonStartStopProcessing.Text = IsProcessing ? StopButtonText : StartButtonText;
		}
	}
 
	private void Processor_ProcessingComplete(object sender, EventArgs e)
	{
		UpdateUIForStartStopProcessing(false);
	}
 
	private void Processor_LogMessage(object sender, LogMessageEventArgs e)
	{
		LogMessage(e.Message);
	}
 
	private delegate void LogMessageCallback(string message);
	private void LogMessage(string message)
	{
#if DEBUG
		string timeFormat = "hh:mm:ss.fff ";
#else
		string timeFormat = "hh:mm:ss ";
#endif
 
		if (this.textBoxMessageLog.InvokeRequired)
		{
			LogMessageCallback callback = new LogMessageCallback(LogMessage);
			this.Invoke(callback, new object[] { message });
		}
		else
		{
			StringBuilder fullMessage = new StringBuilder();
			fullMessage.Append(DateTime.Now.ToShortDateString());
			fullMessage.Append(" ");
			fullMessage.Append(DateTime.Now.ToString(timeFormat, CultureInfo.CurrentCulture));
			fullMessage.Append(message);
			fullMessage.Append(Environment.NewLine);
 
			this.textBoxMessageLog.AppendText(fullMessage.ToString());
		}
	}
}
 
public class LogMessageEventArgs : EventArgs
{
	public string Message { get; set; }
}
 
public class ProcessorOptions
{
	public bool SomeOption { get; set; }
}
 
public class Processor
{
	public event EventHandler<LogMessageEventArgs> LogMessageEvent;
	public event EventHandler ProcessingCompleteEvent;
 
	private bool PleaseStopProcessing;
 
	private ProcessorOptions Options;
 
	public Processor()
	{
	}
 
	public void StartProcessing(ProcessorOptions processorOptions)
	{
		this.Options = processorOptions;
 
		PleaseStopProcessing = false;
 
		new Thread(new ThreadStart(StartProcessingAsync)).Start();
	}
 
	public void StopProcessing()
	{
		PleaseStopProcessing = true;
	}
 
	private void StartProcessingAsync()
	{
		OnLogMessageEvent("Processing starting.");
 
		Thread.Sleep(1000);
		if (this.PleaseStopProcessing)
		{
			StopProcessing(true);
			return;
		}
 
		OnLogMessageEvent(this.Options.SomeOption ? "Some option was selected." : "Some option was not selected.");
 
		Thread.Sleep(1000);
		if (this.PleaseStopProcessing)
		{
			StopProcessing(true);
			return;
		}
 
		OnLogMessageEvent("Still going...");
 
		Thread.Sleep(1000);
		if (this.PleaseStopProcessing)
		{
			StopProcessing(true);
			return;
		}
 
		OnLogMessageEvent("Nearly there...");
 
		Thread.Sleep(1000);
		if (this.PleaseStopProcessing)
		{
			StopProcessing(true);
			return;
		}
 
		StopProcessing(false);
	}
 
	private void StopProcessing(bool WasProcessingInterupted)
	{
		OnLogMessageEvent(WasProcessingInterupted ? "Processing interrupted." : "Processing completed.");
		OnProcessingCompleteEvent();			
	}
 
	protected virtual void OnLogMessageEvent(string message)
	{
		LogMessageEvent(this, new LogMessageEventArgs() { Message = message });
	}
 
	protected virtual void OnProcessingCompleteEvent()
	{
		ProcessingCompleteEvent(this, new EventArgs());
	}
}
processing_form_template.txt · Last modified: 2017/01/01 20:05 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki