tags:

views:

272

answers:

6

Say I have 2 methods. One is an method triggered by the selected index changing in the listbox. The second method helps by clearing all textboxes, setting listbox index to -1, and setting the focus.

Question:

Method two executes, during the code it changes the selected index of the listbox to -1, thereby setting off the event trigger for the 1st method. Does Method 2 HALT it's own execution and transfer the process to the event, and then return back to its work after Method 1 is finished... OR does method 2 finish its entire codeblock then transfer to Method 1 since the selected index changes?

A: 

I assume the language in question is c# and you thus have a language that supports multiple threads. If you don't want to worry about threads (which would be a bad idea if you consider user experience) you can run your GUI in one thread and have the same behavior, unless the components create their own thread (which would be a bit weird though). If you want to achieve an asynchronous (parallel) execution of the event you need to have the the event triggering in its own thread.

To answer your question: if you aren't using multiple threads, the method triggered by the event will be queued. This is exactly what happens when you see GUI responding slowly in some programs.

Hope it cleared things out and welcome from another newcomer :)

Per Stilling
His question is tagged as C# though.
yodaj007
I changed my answer accordingly, thanks for making me aware of this obvious mistake of mine.
Per Stilling
Great answer... so basically the method completes, then ONCE finishes it transfers to the next method, thus "queuing" the triggered event, unless it was deliberately CALLED earlier. That really helped. Threading sounds like a good concept that I'll learn! Thanks for the welcome
Sheldon
A: 

There's a third alternative you can explore: they can also run at the same time! If I understand your question correctly, method 2 would be triggered by the index change event. In a C# Windows Forms application, this other event would occur in a separate thread of execution.

Concepts to explore: threading.

I hope this gives you a starting point in your explorations of knowledge.

yodaj007
A: 

I myself am a beginner, maybe I can help. Method2 would fire, then when the selection changes, Method1 would do his stuff, then Method2 would continue.

If you don't want Method1 to fire at that time, you might want to do is something like: (REALLY pseudo code)

Method2(object sender, System.EventArgs e)
{
  //Unsubscribe Method1 from ListboxEvent
  Listbox.OnSelectionChange -= Method1;

  ... Stuff Method2 actually does ...

  Manualy call Method1 if you want it to fire

  //Subscribe Method1 from ListboxEvent
  Listbox.OnSelectionChange += Method1;
}

It's probably not optimal (and maybe some Best Practices...) but for a lack of a better explanation, at least you have a bit of information to help you search. Hope it helps!

Tipx
+5  A: 

The first case.

Let's leave threads out of it for a moment, particularly because they're not involved in your scenario.

You're talking about properties and methods, but underneath it all, it's all just functions. When one function invokes another, control in your program transfers to the called function. When that function finishes running, control returns to the point where it was called. Your program automatically remembers where it needs to go back to, no matter how deeply functions call more functions.*

When your second function sets the index, what really happens is that the compiler translates the property-set operation into a function call. (Properties are ultimately just "syntactic sugar" for functions.) That function calls a bunch of other functions that aren't important to the scenario, except that one of them is the one that invokes the "index changed" event handler. It sees that you have a method associated with that event, and it calls your first method.

Your first method runs, and when it finishes, it returns to the "invoke the index-changed event handler" function. Eventually, that and all the other unimportant functions finish running (perhaps after making more function calls in sequence), and the "set the index property" function returns control to your second method.

You can prove to yourself that your first suggestion is how it works. Display a message box in your first method, and display another message box after the point in your second method where you set the index property. (Use different messages!) You should see the first message appear, and after you dismiss the message box, you should see the second message appear, thus showing that the second method did not continue executing while the first one was running.

* There is a limit, but it's rarely hit unless there's a bug in your program. When you have too many nested function calls, what happens is a stack overflow.

Rob Kennedy
very insightful, thanks for the help!
Sheldon
A: 

Assuming no multi-thread situation, the event will fire before he end of execution of the method. If you want to see this, code what you have suggested in a .NET language and examine the Il produced. You can do this with ILDASM, or Reflector, or even create your own relfletion application. You do have to understand the syntax of IL enough to see the branch, but it is not that difficult, as long as you understand programming concepts.

Rob has labeled this "syntactical sugar", which I will agree with somewhat. It is really a compiler trick, but I guess it falls under the label "syntactical sugar" as it is commonly used.

Gregory A Beamer
A: 

its is soo good for learning.................................

khan