Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hello everyone,
How can i use a method only one time at the beginning of the code, knowing that this method is part of an event that runs always
Application.Idle += Myevent;
 
Thanks in advance,
z3ngew
Posted 18-Jan-13 13:41pm
z3ngew812
Comments
jibesh at 18-Jan-13 18:54pm
   
since you said you want restrict the execution only once for a event trigger, I would like to know the real problem so that we can suggest a better solution. I suspect you are trying to find a workaround for your problem by restricting the execution. was it true?
BillWoodruff at 18-Jan-13 21:46pm
   
~
Please tag your question to indicate whether you are using WinForms, or WPF, etc.
 
To intelligently answer this question: we need to know more.
 
1. what exactly do you mean by "this method is part of an event that runs always" ? This is confusing.
 
What, exactly is: the "event that runs always" ?
 
2. Do you mean: how do you "start an application" so that one Method is executed only once: that's a much simpler question.
 
3. are you referring by your use of the word "method" to an EventHandler assigned to some Control's Event ?
 
The answers here, so far, are premature.
 
However, David Wimbly's, and Philippe Mori's answers are, at least relevant ... and potentially useful ... based upon their assumptions about what you are asking.
 
PIEBALDconsult's answer about the idea of using a static constructor might be quite useful to you ... again, if we knew the details of your application's architecture, and your real question.
 
yrs, Bill
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You can remove the event handler when it is executed. In fact, any event handler that you add manually should typically also be removed manually (at some point).
void Myevent(object sender, EventArgs e)
{
    Application.Idle -= Myevent;
 
    // Other code here (assuming you don't want to execute the code 
    // again if an exception is thrown)...
}
The only thing that is necessary to be able to remove an handler is that it is an actual function and not a lambda expression.
  Permalink  
v4
Comments
PIEBALDconsult at 18-Jan-13 19:33pm
   
"lambda expression"You mean "Anonymous Method"?
Sergey Alexandrovich Kryukov at 18-Jan-13 23:43pm
   
Absolutely correct note. And, by the way, it's good to avoid removing events, in many cases.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Here is one way to only run a method in code once. As you can see it uses a bool flag and a for loop to simulate multiple times being ran. After the first time the OnlyOnce method executes it sets the flag to true signifying its been ran and will not run within that for loop anymore.
 
public class TestClass
{
    private bool HasAlreadyRan = false;
 
    public TestClass()
    {
        RunMethod();
    }
 
    public void RunMethod()
    {
        for(int i = 0; i < 10; i++)
        {
            if(!HasAlreadyRan)
            {
                OnlyOnce();
            }
        }
    }
 
    public void OnlyOnce()
    {
        //Run Code Here

        HasAlreadyRan = true;
    }
}
  Permalink  
v2
Comments
z3ngew at 18-Jan-13 19:07pm
   
Thank you for you support
David_Wimbley at 18-Jan-13 19:20pm
   
No problem im happy to help
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

It depends. Can you make it a static constructor?
 
A problem with the flag method is that some code could clear your flag (via Reflection if necessary) and run the method again. To make it a little more secure you could put the special method in separate class.
 
I haven't read all of Sergey's article, but it is probably pretty good.
 
Another thought is that VB (but not C# Frown | :( ) allows you to define a method-scoped (static) variable, which could work.
http://msdn.microsoft.com/en-us/library/z2cty7t8(v=vs.110).aspx[^]
  Permalink  
Comments
BillWoodruff at 18-Jan-13 22:05pm
   
Fascinating, PIEBALDconsult, I have made frequent use of static constructors: I like them. And, even though some OOP "purists" seem to have a real gut-level antipathy against "static classes:" I like those, too :)
 
Your point about the possibility of some code changing a boolean flag, used to insure single execution of a method, via reflection is very thought-evoking, even if it's hard for me to imagine a scenario in which this could actually happen.
 
Also interesting that VB.NET has this method-scoped static variable capacity: although I'll never touch that, since I'll never touch VB.NET again. Nothing against VB.NET, personally, however, just that I find everything in C# I need :)
 
Thanks for your stimulating observations !
 
best, Bill
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can do it in a very simple way, but do it over, again and again. Or, you can do it in a very universal way.
 
Nearly everyone knows the simple way, but, to best of my knowledge, the universal way is happened to be only mine. It's is very exotic but fully legitimate and reliable, as it is totally based on standard .NET library (that is, BCL). I discuss both in my article. Please see:
Wish You Were Here… Only Once[^].
 
—SA
  Permalink  
Comments
z3ngew at 18-Jan-13 19:08pm
   
This is Good Work, Thanks :)
Sergey Alexandrovich Kryukov at 18-Jan-13 19:19pm
   
My pleasure.
Good luck, call again,
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 380
1 OriginalGriff 250
2 DamithSL 210
3 Kornfeld Eliyahu Peter 200
4 Peter Leow 95
0 OriginalGriff 7,315
1 DamithSL 5,199
2 Sergey Alexandrovich Kryukov 4,917
3 Maciej Los 4,866
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100