c# - why do{ }while() loop doesn't work in body of a button_Click( ) method -
this code works correctly in console application, when use in windows forms application doesn't work correctly. never stops , produces no output.
i use in console aplication , works :
static void main(string[] args) { console.writeline("enter boolean query"); do{ string query = console.readline(); list<int> lst = processquery(query); count = 0; if (lst!=null) { foreach (int in lst) { if (a == 1) { console.writeline(documentcontentlist[count]); } count++; } } else { console.writeline("no search result found"); } } while(1==1); }
i try use above code on button_click
method in windows forms application, doesn't work. think wrong while(1==1)
- there equivalent?
here method wrote button:
private void button6_click(object sender, eventargs e) { if (t == null) { messagebox.show("click on loadfile button,please."); return; } if (textbox4.text == "") { messagebox.show("enter boolean query"); return; } listbox1.items.clear(); datetime dt = datetime.now; do{ list<int> lst = t.processquery(textbox4.text); count = 0; if (lst != null) { foreach (int in lst) { listbox1.items.add(t.documentcontentlist[count]); } count++; } else { listbox1.items.add("no search result found"); } label1.text = "search = " + listbox1.items.count + " items, " + datetime.now.subtract(dt).totalseconds + " s"; } while (1==1); }
i believe removing while loop button_click event handler job you.
private void button6_click(object sender, eventargs e) { if (t == null) { messagebox.show("click on loadfile button,please."); return; } if (textbox4.text == "") { messagebox.show("enter boolean query"); return; } listbox1.items.clear(); datetime dt = datetime.now; //do{ list<int> lst = t.processquery(textbox4.text); count = 0; if (lst != null) { foreach (int in lst) { listbox1.items.add(t.documentcontentlist[count]); } count++; } else { listbox1.items.add("no search result found"); } label1.text = "search = " + listbox1.items.count + " items, " + datetime.now.subtract(dt).totalseconds + " s"; // } while (1==1); }
the console application pauses execution when waits synchronously user input
string query = console.readline();
and when gets input necessary computation , prints whatever printed , waits @ same line again next input.
a winform application not work way, has event loop processes ui activity (keypressed etc.)
in short, not need loop in handler method.
Comments
Post a Comment