Link to home
Start Free TrialLog in
Avatar of www_SMR_co_uk_was_too_offensive
www_SMR_co_uk_was_too_offensive

asked on

bizarre VB windowing race condition?

ok, I've run up against an irritating problem that seems to be some kind of bug in VB's windowing code.

I have a form (form1) .Show()ing a modal form (form2) which does some stuff. form1 has a control with a LostFocus() event. This event seems to be fired at a peculiar time. When debugging the code (or when using messageboxes in code to indicate order of execution) the lostfocus event fires when the form2.Show() command is given just before form2 is actually shown. Otherwise it seems to fire just after form2 returns. That's the first bug.

The second problem is that I actually don't know what the hell is going on in that latter case (just executing the code without any debugging measures). But for whatever reason it seems to be behaving illogically.

some pseudocode:
form1_textbox_doubleclick()
  noupdate = true         'noupdate is a private signalling variable inside form1
  form2.Show 1, Me        'modal, Me is owner
  set form1.textbox to form2.returnvar         'returnvar is a public variable set by operations on form2
  noupdate = false
end sub

form1_textbox_lostfocus()
  if noupdate then exit sub
  set form1.textbox to "0"
end sub

anyone know anything about this or come across similar problems? Other points that might be of interest are that the prog uses functions in a DLL written in C to do some stuff, but none of that happens anywhere near the above code. There are no MDI forms. No windows API code is being used. My DLL doesn't mess about with window procedures or touch the windowing code in any way whatsoever.

I would be especially grateful if anyone can point me to some accurate information about the order of execution of code in these specialised sorts of windowing scenarios in VB.

Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of JCinDE
JCinDE

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of www_SMR_co_uk_was_too_offensive
www_SMR_co_uk_was_too_offensive

ASKER

The main form (form1) is itself simply a dialog for the retrieval of a value from the user. form2 is an optional pop-up that allows you to view/enter/change the value in scientific notation (Ax10^B). The lostfocus event is necessary for input checking - when the textbox loses focus the program assumes that the user is done typing, takes a value from the box and refreshes the box so it displays the value as it has been stored (ie removes any illegal letters, symbols etc).

behavioural flow diagrams:
  [form1_show] -> (set textbox to VALUE)
  [textbox_keypress_vbCR] -> (if len(textbox) then VALUE = val(textbox)),(textbox = str(VALUE))
  [textbox_lostfocus] -> Same as above
  [textbox_dblclick] -> (show form2 modally)
*[form2_show] -> (set TEMPVALUE to VALUE), (set form2 textboxes to values based on TEMPVALUE)
  [etc]
  [form2_textboxes_lostfocus or vbCR] -> (TEMPVALUE = textbox values etc, like above)
  [form2_ok_button] -> (VALUE = TEMPVALUE), (hide), (refresh form1)
  [form2_cancel] -> (just hide form)

*it's around here that things get wierd. If debugging, lostfocus is called, and then execution carries on as normal. If running normally, then the lostfocus event is somehow queued here, but doesn't fire until form 2 returns (these are modal forms remember), so after the changes etc, form1_textbox_lostfocus fires and resets the value we got back from form2. This is probably all a result of the way in which VB's runtimes create these 'modal' forms, which AFAIK is not a builtin feature of the windowing engine.

I'm presently trying to figure out other ways of doing what I want to do (it's currently looking like a timer will be necessary to 'schedule' things properly) so the problem isn't of the 'please help me do XYZ' variety, more a 'what the hell is going on, anyone know?'.

Thanks for the suggestion JCinDE, but no cigar just yet! I'll keep digging to see if I can find anything, I'll report back if I make any progress. Any time you can spend on this is greatly appreciated.

=me=
dammit, why can no-one ever answer any of my questions? give the points to JCinDE. I guess he tried.

=me=
Dan,

Alas, it was rhetorical, but I thank you for your suggestions. I apologise if I seem ungrateful for the services provided by you and your cohorts, I do not mean to cause offence.

The reason for the comment is my frustration at seeming to have developed an ability to come up against really obscure problems such as the one above. With regard to your point 3 above, I generally do not especially want people to test my problems out. I will usually bang my head against a problem for a day or two before bringing it here, so I'm usually fairly certain of the parameters, and I'm more in search of advice from someone who has experienced the problem themselves and as such has expertise. It is advice and insight I seek, not man hours. However I cannot complain if that is what people have to offer here. I guess the motivation of points (and thus the selectiveness of attention) is to be expected as well, after all it is the currency of this exchange. However this brings me on to your point 4 - professionalism. My response to that would be that I am *glad* I sound unprofessional. I do this for love not money! I feel that keeping one eye on the money is not information technology - it's business, and I don't feel that business is always particularly good for IT. Personally I will make suggestions on any question I feel competent in answering, regardless of points, but again, it's your ball so I guess you get to choose the game :)

With regard to point 2, yes I agree the name is pretty offensive. Originally it was just a shameless website plug, and the account's deactivation prompted me to start a new account with a slightly moronic 'retort'. I keep forgetting that the way to get things changed around here is to make requests in CS - I had just assumed that the lack of 'change username' meant it couldn't be done. I'll probably tap something out to CS and try to get it changed. Failing that I'll just shut down the account and start again. Incidentally, if long names are so offensive why does EE allow people such things?

It's a shame my 'voice' comes across as expressing a (bad) attitude, it certainly isn't meant as such. I fear that people may take things more seriously than I, and perhaps I should steel myself for more misunderstandings in the future. I hope however that you're able to see that I'm keen to learn and share my knowledge and I trust that keeps me well within the template of a welcome user of this exchange.

=me=