Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
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
C#
Application.Idle += Myevent;


Thanks in advance,
z3ngew
Posted
Comments
Jibesh 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 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

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).
C#
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.
 
Share this answer
 
v4
Comments
PIEBALDconsult 18-Jan-13 19:33pm    
"lambda expression"You mean "Anonymous Method"?
Sergey Alexandrovich Kryukov 18-Jan-13 23:43pm    
Absolutely correct note. And, by the way, it's good to avoid removing events, in many cases.
—SA
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.

C#
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;
    }
}
 
Share this answer
 
v2
Comments
z3ngew 18-Jan-13 19:07pm    
Thank you for you support
David_Wimbley 18-Jan-13 19:20pm    
No problem im happy to help
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# :( ) allows you to define a method-scoped (static) variable, which could work.
http://msdn.microsoft.com/en-us/library/z2cty7t8(v=vs.110).aspx[^]
 
Share this answer
 
Comments
BillWoodruff 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
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
 
Share this answer
 
Comments
z3ngew 18-Jan-13 19:08pm    
This is Good Work, Thanks :)
Sergey Alexandrovich Kryukov 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900