Skip to content

Action Managers (code)

No I Wi ll Not Tell

Asked Apr 5, 2007, 4:28 PM EDT2 answers
Share
LinkedInX

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);
}
if (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!

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0
    this looks like it may be interesting.  why don't you place it in a blog on C# Corner?
    0
  • 0
    I didn't think about it, but i tried posting things like code snippets but it kept asking me to log in.
    0