c# - how to force Show() to execute now, not after others methods -


in baseform of winform project code connecting database moved load event show event.
in show event there call update() before fetching data, makes form appear faster more pleasant users.

but found code on places example :

formritdetail ritdetail = new formritdetail(); ritdetail.primarykeyvalue = ritid; ritdetail.show(); ritdetail.sendsaleemail(cancelsale); ritdetail.close(); 

this worked perfect while code fetching data in load event, gives error have tracked down. in sendsaleemail method data not fetched yet.

the fetching happens in shown() event, seems c# call sendsaleemail first, , call show().

how can force c# methods in order write them ?
can call ritdetail.update() after ritdetail.show() know that, general solution not involves writing additional code everywhere show() method called.

is possible ?

in baseform of winform project code connecting database moved load event show event.

there real problem. depend on event executed valid object state. that's called temporal coupling. it's makes experience current problem.

a general guideline never execute business logic within events. instead create separate methods that. methods can in turn executed event handlers.

the other problem need load , show form send send email? @ least interpret question form open, execute , close. move code new class responsibility.

so answer question is:

  1. do not depend on ui events ensure business data loaded. can loaded directly not yet populated form until it's ready.
  2. forms have ui responsibility. should not responsible of business logic. create separate classes.

update

regarding actual problem, checked reference source form class. show() method changes internal state (using setwindowlongptr winapi function). nothing done until message pump processes message.

there no guarantee it's done before next method call (i.e. sendsaleemail).


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -