Waiting for a method to execute.

Hi,

I imagine that this is a fairly common question. I have two methods that I would like to run in succession, i.e, let the latter wait for the former to execute. Logic tells me that a better approach would be the use of threads, but alas, I am stuck.

For clarity:

Help.Call();
//wait for Help.Call() to execute
Proceed.PlanB();

Thanks.




Answer this question

Waiting for a method to execute.

  • Pythos

    It's better to do anything that might interact with the controls of the form in the Load event of the form instead of the constructor. Populating the ListView in the constructor might be particularly troublesome...

    Of course, your problem could in fact be that the first method spawns a second thread, in which case you'll need to get a WaitHandle or something, which would probably require you to modify the Help.Call function. (I suppose you could use the Process.Threads property if you are unable to modify that function, although it would be a little work).

  • fibonacci1123

    There is a design issue in your application. Usually, the UI thread is not used for doing any other processing except for interacting with the user and or modifying UI. If your processing is tedious then you should prefer doing that in a seperate thread which in turn is a Worker Thread of your Main thread. In this design, your worker thread would do all the work under the hood that you want it to perform while the UI thread is always available for User to interact with it.

    Hope it helps.



  • Matt Grove

    Hi,

    that is the normal behaviour. One thread executes one operation after the other.

    The opposite would be more work: if you want Call and PlanB executed simultaneously, then you would need two threads, each executing one method call.

    Do you have any code which makes you think, code is executed simultaneously

    --
    SvenC


  • Long Xue

    I have been trying to get my head around it. It must be a problem with my methods.

    I am calling two methods from within the constructor of a form - which contains just a ListView. The first method establishes a WMI connection with a remote machine and retrieves the running process. The second is just a "ShowDialog".

    The problem is that the first method is supposed to call another method (all of different classes) that populates the ListView with the retrieved data. Before the second method, but the second method seems to butt in while the items are being listed.

    Back to the drawing board!


  • Pankaj11

    Since you are working with a form, my advice is to drop a BackgroundWorker component onto your form, and use that to run the thread in the background.

    Look up the MSDN help for BackgroundWorker; it isn't too hard to use.

  • Waiting for a method to execute.