How to pause execution in a Form?
DOes anyone know how to pause execution in one form/method until another form/method is through then finish its own program. I want to be able to switch to another form, run some execution, then take the results to pass back to the original form and let it finish execution. WHen I try it, it will switch over and even pass info back, but the original form will fishish its sub-routine with out waiting for the results. Know it sounds confusiong.
[447 byte] By [
Rolan_64] at [2007-12-16]
Im not sure I understand the complexity of your problem here but what I think you are looking for is something like the following. If you have Form1 and Form2 for example you just need a function in Form2, Form1 will wait until Form2 returns and then will continue, consider the following:
In Form1:
| |
Dim form1Var as Integer = 0 Dim form2Var As Integer = 0 Do Until form1Var = 100 form1Var += 1 Loop form2Var = Form2.DoSomething() MessageBox.Show("Value of form1Var is: " + form1Var.ToString) MessageBox.Show("Value of form2Var is: " + form2Var.ToString) |
In Form2:
| |
Public Function DoSomething() As Integer Dim i As Integer = 0 Do Until i = 200 i += 1 Loop Return i End Function |
Hope this is what you were looking for.
-Joe
I dont think he wanted a delay routine. In case delay was what he needed then a Thread.Sleep(1000) or any desired value in milliseconds will suffice.
In .NET you can call any method asynchronously using the BeginInvoke and EndInvoke commands and register callbacks which trigger when the method completes and a result is available. See if this fits in for what you are trying to do:
1. Asynchronous programming in .NET
2. Sample
Regards,
Vikram
Thread.Sleep will not allow any execution at all. The code above is just a simple sample of how to transfer code execution from Form1 over to Form2 and halt the execution in the Form1 method until the Form2 method is done whatever needs to happen, after which Form1 may resume. This is not Asynchronous, its a blocking call. His question does not possess enough information on what information he needs. For example, is the method in form2 raising an event, does it require a callback, etc. If he just wants form2 to take over in the middle of a method in form1, the code above is a sample of that.
-Joe
What is sounds like is that you want to display a form in front of another and then pass the results of the new form back to its owner.
Have a look at the Form.ShowDialog() method, like so:
| | Dim value As IntegerUsing form As New MyForm form.ShowDialog() value = form.Results End Using |