2
Answers

Action Managers (code)

Ask a question

The codes here allow you to manage actions, like not normally done.

'Until' until <CONDITION> is equal to true do <ACTIONS>

delegate void Actiondelegate();
delegate bool ConditionalDelegate
();
private void Until(ConditionalDelegate mybool, Actiondelegate
actdel)
{
     while (Convert.ToBoolean(mybool.DynamicInvoke(null)) != true
)
     {
        actdel.DynamicInvoke(
null
);
     }
}

example
...
Until(delegate {return mybool == true;}, delegate {myactions});
...

'Whenever' (always==true)Whenever <CONDITION> is equal to true, do <ACTIONS> (always==false)The First Time <CONDITION> is equal to true do <ACTIONS>. sorry for the weird colors, it cnped weridly

delegate void Actiondelegate();
delegate bool ConditionalDelegate
();
bool exiting = false;/*setting this to true ends all Whenever loops, set it back to false to use more loops. You MUST set this to true when exiting your app or the thread will remain.*/
private void Whenever(ConditionalDelegate mybool, Actiondelegate actdel, bool always)
{
while (always == true && exiting == false)
//if it is always
{
while (Convert.ToBoolean(mybool.DynamicInvoke(null)) == true)
//while it is true
{
Application.DoEvents();
//always ensure to not interfere
actdel.DynamicInvoke(null
);
}
Application.DoEvents();
//same, don't interfere
System.Threading.Thread
.Sleep(1);
}
while (Convert.ToBoolean(mybool.DynamicInvoke(null)) != true && exiting == false)
//incase always=false
{
Application
.DoEvents();
System.Threading.
Thread
.Sleep(1);
}
i
f (Convert.ToBoolean(mybool.DynamicInvoke(null)) == true
)
{
actdel.DynamicInvoke(
null
);
}
}

Example
...
Whenever(delegate {return TheTextBox.Text == "a";}, delegate {TheTextBox.Text += "b";}, true);
...
We have all had those times when we want to have something happen automatically when times like these come, you need Whenever. Whenever(delegate { return MYCONDITION; }, delegate {MYACTIONS}, true); That code will do MYACTIONS everytime MYCONDITION is true. Perhaps you're waiting on something? Until(delegate { return MYCONDITION;}, delegate { MYACTIONS }); that code will do MYACTIONS until MYCONDITION == true, this does stop the progression of a method, a productive 'wait' function (try to include System.Threading.Thread.Sleep(1) inside to reduce CPU usage and Application.DoEvents() to update the user interface)

WARNING: PLACING A 'Whenever' CALL WITHIN STARTUP FUNCTIONS WILL CAUSE AN ERROR ON CLOSING THE APPLICATION (eg. just after the InitializeComponent call) CALL WHENEVER FROM SOMEWHERE ELSE (any ideas on fixing this would be appreciated)

i couldn't post anywhere else so here this is, enjoy.

ideas, questions, comments? Post them now!


Answers (2)