jwcolby
jwcolby at colbyconsulting.com
Wed Apr 4 20:09:14 CDT 2012
If anybody actually tries to understand this please respond on this message thread and I will
explain my reasoning (or lack thereof).
jwc
using System;
using System.Drawing;
using System.Threading;
using System.Timers;
using System.Windows.Forms;
namespace projAccuzip2
{
/// <summary>RunState
/// RunState enum.
/// These are the 4 different states that are used by our runState class.
/// </summary>
public enum RunState
{
Stopping, Stopped, Starting, Started
}
/// <summary>clsRunState
/// The parent is the object that has an instance of this class
/// The grandparent if the object that owns the parent
///
/// clsRunState is instantiated once by the parent object (the parent of clsRunState)
/// The grandparent typically owns instances of the parent class
///
/// The grandparent calls mStart and mStop to tell the parent that it needs to start or stop
/// Each of these methods sets the associated flag and raises an event evStart or evStop
///
/// The grandparent calls pRunState to get the current runstate of the Parent object
/// The grandparent should never directly set the runstate. Only the parent object knows
/// when it transitions from one state to another
///
/// The parent can sink the evStart and / or evStop so that it can be immediately notified that
/// the grandparent is telling it to start or stop
///
/// The parent sets the runstate by calling mSetRunState passing in the run state that it
is currently in
/// </summary>
public class clsRunState
{
#region Header
private string rsName = ""; //variable to store the run state name(this name is
also used for the thread if there is one)
private Thread oThread; //thread object for use by this class if you tell it use it
private bool threaded; //variable to track if this class should be threaded
private bool threadBusy = false;
private System.Timers.Timer timer;
private bool timed = false;
private bool timerBusy = false;
private RunState runState = RunState.Stopped; //A start variable telling everyone the
current state of the parent object(defaults to stopped)
private CheckBox chkBox; //checkBox control pointer(used to start/stop the
runState)
private Button btnStartStop; //button control pointer(used to start/stop the
runstate)
private bool BtnOn = false; //bool variable to track if the button is in the
started state or not
private string startText = "Start", startingText = "Starting", startedText =
"Started...", stopText = "Stop", stoppingText = "Stopping...", stoppedText = "Stopped"; //the
different text to display on the checkBox for the different states
private Color startingCol = Color.LightGreen, startedCol = Color.DarkGreen, stoppingCol
= Color.DarkOrange, stoppedCol = Color.Red; //the different colors to use for the text for the
different states
public int pTimerInterval { get; set; }
/// <summary>clsRunState
/// Constructor for clsRunState.
/// This constructor is used if there is not need to start/stop the class from a form
with a checkbox or button.
/// </summary>
/// <param name="RSName">Name given to this run state class.</param>
/// <param name="Threaded">Threaded or not.</param>
public clsRunState(string RSName = "", bool Threaded = false, bool Timed = false, int
TimerInterval = 10000)
{
rsName = RSName; //set the name
threaded = Threaded; //set the thread variable
timed = Timed;
pTimerInterval = TimerInterval;
InitTimerOrThread();
}
/// <summary>clsRunState
/// Constructor for clsRunState.
/// This constructor is used if you want a checkbox on the form for starting and
stopping the run state.
/// </summary>
/// <param name="RSName">Name given to this run state class.</param>
/// <param name="checkBox">Checkbox used to start/stopp the run state.</param>
/// <param name="Threaded">Threaded or not.</param>
public clsRunState(string RSName, CheckBox checkBox, bool Threaded = false, bool Timed
= false, int TimerInterval = 10000)
{
rsName = RSName; //set the name
chkBox = checkBox; //set the checkbox pointer
chkBox.CheckedChanged += new EventHandler(chkBox_CheckedChanged); //sink the
checked event of the checkbox
threaded = Threaded; //set the threaded variable
timed = Timed;
pTimerInterval = TimerInterval;
InitTimerOrThread();
}
/// <summary>clsRunState
/// Constructor for clsRunState.
/// This constructor is used if you want a button on the form for starting and stopping
the run state.
/// </summary>
/// <param name="RSName">Name given to the run state class.</param>
/// <param name="BtnStartStop">Button used to start/stop the run state.</param>
/// <param name="Threaded">Threaded or not.</param>
public clsRunState(string RSName, Button BtnStartStop, bool Threaded = false, bool
Timed = false, int TimerInterval = 10000)
{
rsName = RSName; //set the name
btnStartStop = BtnStartStop; //set the button pointer
btnStartStop.Click += new EventHandler(btnStartStop_Click); //sink the click
event of the button
threaded = Threaded; //set the thread variable
timed = Timed;
pTimerInterval = TimerInterval;
InitTimerOrThread();
}
#endregion
#region control Delegates
//A delegate used to update the check box textColor and text
delegate void delUpdateChk (CheckBox chk, Color textColor, string text, bool enabledState);
/// <summary>
/// Performs the invoke to set the checkbox label
/// </summary>
/// <param name="color"></param>
/// <param name="text"></param>
/// <param name="enabledState"></param>
private void invokeChkBox(Color color, string text, bool enabledState)
{
if (chkBox != null)
{
lock (chkBox)
{
chkBox.Invoke(new delUpdateChk(invUpdateChk), new object[]
{ chkBox, color, text, enabledState });
}
}
}
/// <summary>invUpdateLabel
/// Delegate method for updating the passed in label color and text.
/// </summary>
/// <param name="label"></param>
/// <param name="textColor"></param>
/// <param name="text"></param>
private void invUpdateChk(CheckBox chk, Color textColor, string text, bool enabledState)
{
chk.ForeColor = textColor; //set the checkbox text color
chk.Text = text; //set the checkbox text
chk.Enabled = enabledState; //set the checkbox enabled property
}
// A delegate used to update the button enabled state
delegate void delUpdateBtn(Button btn, Color textColor, string text, bool enabledState);
private void invokeBtn(Color color, string text, bool enabledState)
{
if (btnStartStop != null)
{
lock (btnStartStop)
{
btnStartStop.Invoke(new delUpdateBtn(invUpdateBtn), new object[]
{ btnStartStop, color, text, enabledState });
}
}
}
/// <summary>invUpdateLabel
/// Delegate method for updating the button.
/// </summary>
/// <param name="btn"></param>
/// <param name="enabledState"></param>
private void invUpdateBtn(Button btn, Color textColor, string text, bool enabledState)
{
btn.ForeColor = textColor; //set the button text color
btn.Text = text; //set the button text
btn.Enabled = enabledState; //set the button enabled property
}
#endregion
#region Timer Delegate
/// <summary>
/// The timer start
/// </summary>
private void timer_Elapsed(object sender, EventArgs e)
{
if (!timerBusy) //Do not re-enter the code if the timer thread is still busy
{
timerBusy = true; //Set a flag saying that we are starting the timer thread
evTimer(this); //Start the timer thread business.
timerBusy = false; //Clear the flag saying the timer thread is busy
}
}
#endregion
#region Thread Delegate
/// <summary>
/// The thread start
///
/// When running a thread, when we return from the start
/// the thread is finished with it's business and will never run again
/// If the thread is to run periodically, use the timer
/// </summary>
private void mtStart()
{
//
//Do not allow re-entrant processing.
//This should never happen but if it does,
//make sure that the code does not re-enter.
if (!threadBusy)
{
threadBusy = true;
evStart(this);
threadBusy = false;
runState = RunState.Stopped; //Set the RunState to stopped
}
}
#endregion
#region Events
public delegate void delEvStartStop(object sender);
public event delEvStartStop evStart;
public event delEvStartStop evStop;
public delegate void delEvTimer(object sender);
public event delEvTimer evTimer;
//check box check changed event
void chkBox_CheckedChanged(object sender, EventArgs e)
{
if (chkBox.Checked) //if the checkbox is checked
{
mStart(); //start the run state
}
else //else
{
mStop(); //stop the run state
}
}
//button click event
void btnStartStop_Click(object sender, EventArgs e)
{
if (BtnOn) //if button is on(already started)
{
mStop(); //stop the run state
}
else //else
{
mStart(); //start the run state
}
}
#endregion
#region Properties
/// <summary>pRunState
/// Returns the runState.
/// </summary>
public RunState pRunState
{
get { return runState; }
}
#endregion
John W. Colby
Colby Consulting
Reality is what refuses to go away
when you do not believe in it
On 4/4/2012 6:16 PM, David McAfee wrote:
> Did my message ever come through?
>>
>> Care to share the code that worked for you?
>>
>> Thanks,
>> David