c# - Function associated wth a property does not fire? -
enter code herei have propert in .cs file. when ever set property event associated gets fired.
public event action responsereceived; private string response; public string response { { return response; } set { response = value; if (responsereceived != null) { responsereceived(); } } } now problem in file when do
responsereceived += new action(function_responsereceived); void function_responsereceived() { //change gui thread if (invokerequired) { this.begininvoke(new action(function_responsereceived), new object[] { }); return; } textbox1.text = response; } response = "yes"; . . . (after lines) . . .
response = "no"; but yes not fire funcion associated event response = "no"; fires (the response field updated faster time takes fire event over-written, guess) . there way both times when set property function asscoiated event fires properly
initial answer:
the code show works fine.
there must in code mentioned '. . . (after lines) . . .'.
testcode:
using system; using system.collections.generic; using system.linq; using system.text; namespace functionassociatedpropertyfire { class program { static void main(string[] args) { new testclass().test(); console.readline(); } } class testclass { public event action responsereceived; private string response; public string response { { return response; } set { response = value; if (responsereceived != null) { responsereceived(); } } } void function_responsereceived() { console.writeline("function_responsereceived"); } public void test() { responsereceived += new action(function_responsereceived); console.writeline("beforeyes"); response = "yes"; console.writeline("afteryes"); response = "no"; console.writeline("afterno"); } } } testresult:
beforeyes function_responsereceived afteryes function_responsereceived afterno update:
you invoking asynchronously, invoking code doesn't wait invoked method finish. can try use invoke-method instead of begininvoke-method.
void function_responsereceived() { //change gui thread if (invokerequired) { this.invoke(new action(function_responsereceived), new object[] { }); return; } textbox1.text = response; }
Comments
Post a Comment