开发者

C# Method Statements Not executing in correct order

开发者 https://www.devze.com 2023-03-18 07:31 出处:网络
I seem to havea problem executing commands in the correct order, I have a method in my Program: private voidGenerateButton_Click(object sender, EventArgs e)

I seem to have a problem executing commands in the correct order, I have a method in my Program:

private void  GenerateButton_Click(object sender, EventArgs e)
{
    Statuslabel.Text = "Working...";

    LongMethod();
    //Call to another Method of another class which takes 15-20 seconds to execute

    Statuslabel.Text = "Done";
}

the problem seems to be that instead of assigning "Working" to the status label and THEN calling the LongMethod, the Program seems to execute LongMethod() first, and then it changes Status Label's text to "Working" for a split second and 开发者_如何转开发then immediately changes it to "Done". Oh, and the UI is locked up during the LongMethod() execution, because the Program is SingleThreaded.

I tried threads earlier, but for the life of me I couldn't get the syntax right, I tried:

Thread MyThread = new Thread(LongClass.LongFunction);

Thread MyThread = new Thread(new ThreadStart(LongClass.LongFunction));

Where LongClass is the class which contains LongFunction as a static method. I will check out the background worker now.


You should execute LongMethod on another thread so that the UI thread doesn't block while it's running.


Remember, updating the UI is running code just like anything else. While your long-running method is running, that thread is not doing any of the tasks necessary to redraw the user interface. Changing a UI element does not stop everything and re-draw it because suppose you changed a thousand UI elements; you wouldn't expect a redraw after each one; you'd expect them all to happen at once, after you'd made all the changes.

Long story short, if you want to refresh the UI after the update but before the long-running code -- that is, you don't care about hanging the UI but you at least want it to update -- then insert a call that explicitly refreshes the UI.

Some have suggested "DoEvents" as a workaround. This can work, but it is super dangerous. For two reasons. First, suppose the user clicks a button twice. During the processing of the first click, you do a DoEvents, and so you then recurse and hey, now you have suspended the processing of the first button click so that you can process the second button click... and that can't be good.

Second, suppose you're processing an event, and you do a DoEvents, which causes you to start processing another event, and then while you're doing that, you do a DoEvents, and that causes you to start processing a third event... and this keeps going forever. When do you finish processing the first event? Potentially never. Remember "DoEvents" basically means "concentrate on what just happened at the expense of what you were already working on".


While I think Jason's answer to use another thread is the way to go, there is another "evil" option.

Statuslabel.Text = "Working...";

Application.DoEvents();

LongMethod();

Statuslabel.Text = "Done";
0

精彩评论

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