简体   繁体   中英

c#: order of operations in event-driven code doesn't match flow

The flow of code doesn't follow my logic, and my lists don't get the required output.

public static List<String> genericList;
public static List<String> List1;

in frmLoadCurrentForm():

List1 = new List<String>();
genericList = new List<String>();

Then, in various places (? buttons)

1 private void btn1_Click(object sender, EventArgs e)
2 {
3     genericList = List1.ToList();
4     call(); 
5     List1 = genericList.ToList();
6 }
private void call()
{
    frmForm form = new frmForm();            
    for (int i = 0; i < genericList.Count(); i++)
        form.lst.Items.Add(genericList.ElementAt(i));
    form.Show();
    //form updates genericList on exit with lst contents, tested
}

While stepping through my code, I put a breakpoint on line 5, and code goes to line 5 then 4 (and into the form), then doesn't go back to 5 - so my List1 doesn't get updated with what happens inside the call().

I don't understand this logic, or what to do about it.

I am using multiple lists and a single genericList so that I can use a complicated interface for multiple situations, and theoretically my logic seems valid...

At the exit from the form, genericList has the right info, List1 doesn't. Why ?

Edit: added declaration of lists

Edit: As I step through, the form will start rendering but will not finish until the code at the end of the button that called the method that created the form got executed. Which is even more odd, since the form instance is created in a method so it should be closed and disposed of at the end of the method...

Well, by putting a breakpoint at Line 5, you're not going to be running line 5; but the call on line 4 will still go through, and the program will proceed normally. Line 4, you called a function, which effectively ran, then stopped when it came back.

Breakpoints are only triggered when the program executes that specific line. C# doesn't execute code linearly, it can, but it is not held to that rule.

Take a look at http://www.ndepend.com/ It's a mapping software, and it might be able to give you a better idea, visually.

Please inform me if my answer is wrong, as well, therefore I do not spread incorrect information. Thanks!

Although I am confused what you are doing with the above code you should be aware that Form.Show() only returns if the opened Form has been closed - maybe that's a source of your confusion.

Further I don't know what you want to do with the call to the method ToList() because the purpose of this method is to generate a list object from an IEnumerable object but you seem to generate a List from the List which doesn't make sense to me.

I have not tested your code but I think you code should execute as it's written above without any problems that means when you click the button that has the event handling method btn1_Click been called execution should be as follows:

  1. execution of line 3
  2. control goes in to the code within the call() block and proceeds sequentially until the Form.Show() call. This call lets the form be displayed and the execution is branched again to the internal Form code.
  3. When the form has been closed execution returns to the first line after the Form.Show() call which is the closing braces of the method.
  4. The next statement to be executed is line 5 of your event handling method.

When you put a breakpoint at the declaration line of the btn1_Click method and step through the code in a step by step manner you should experience exactly this behavior.

It was definitely a threading problem.

To solve it the easiest way possible (instead of creating a separate thread), I replaced form.Show() with form.ShowDialog();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM