开发者

C# WinForms: A function that executes more than expected

开发者 https://www.devze.com 2023-04-03 07:46 出处:网络
It sounds stupid, but this happens to me. I activate a function, and it does its work, and after it finishes it enables itself again. With other words, the function activates twice, not once. I tried

It sounds stupid, but this happens to me.

I activate a function, and it does its work, and after it finishes it enables itself again. With other words, the function activates twice, not once. I tried to find out why by debugging, but I didn't find the reason.

When I click on a panel this function activates:

private void Play(object sender, EventArgs e)
{
   开发者_JS百科 Play0(MousePositionX, MousePositionY);
    if (swich_player == true && AI_enabled == true)
    {
        AI_playing();
        swich_player = false;
    }
}

The whole code is really long.

Is it me not searching correctly? Are there other reasons? In order for this function to start again, something has to activate it. I can't find what is activating the function again.

Any suggestions, where to search for the problem? Or what is the problem?

EDIT:

I finally found the problem. There is a button (button1) that changes the panel's properties, and a second button (button2) that makes the panel 0 pixels wide and 0 pixels high (in order to make it invisible). The first button also adds an EventHandler that activates the function. But button2 does not remove the EventHandler. This way the function gets called as much times as the times I have pressed button1.


Looks like what you're looking for (via your comments/edit) is the source of multiple calls to this function. With the current snippet of code, it's not possible for us to tell. But what you should do is put a breakpoint on the entry of the function and then when it is hit, look at the "Call Stack" window in visual studio.

This can be accessed via the Debug -> Windows -> Call Stack menu item (when running the program. It will not show up in Windows if you're not running)

You can then see what is calling your function through this window. It is an extremely useful tool.

Pre Edit: Your question isn't very clear, but I think (from your variable names) that you're looking for a game loop that will continually run. If so, take a look at this blog post which has some very good information on different styles of game loops. It ranges from simple to more complex (and scalable) loops.

Your question states that you activate something once and it "enables again", and also that "In order this function to start again, something has to activate it. I can't find what". These seem to be contradictory statements. Can you edit your question to be a little clearer? If my answer is not what you were looking for after your edit, I will do my best to add whatever is needful.


check against which event you've registered this function. if you registered into to something no "OnClick" of that button, it might be the root cause

0

精彩评论

暂无评论...
验证码 换一张
取 消