Sub Main is not waiting!!!??

H

Hugh Janus

OK,

My app opens a form and waits for a user to press a button called "Go".
If I pass the parameter Q at startup I want it to not show the form
but call "Go.PerformClick". So in essence it runs like a console app
writing text out to the DOS window that called it.

The problem I have is that when Sub Main runs, it works out if the
correct parameter has been passed and parses the PerformClick sub but
does not execute it and then carries on with the rest of Sub Main
before ending the app. See code below:

Sub Main(ByVal Args() As String)

If Args.Length > 0 Then

If Args.GetValue(0) = "Q" Then

Console.WriteLine("Fase 3 started: "
&Now.ToShortTimeString)
Dim newF3 As New Phase3
newF3.Hide()

Call newF3.Gop.PerformClick

Console.WriteLine("Fase 3 ended: " &
Now.ToShortTimeString)

Else

Dim newF3 As New Phase3
newF3.ShowDialog()

End If


What am I doing wrong and what do I need to do to force it to wait
until the Sub has finished? I even tried putting it in a thread and
then using .Join but it even jumps this.

Hugh
 
A

Armin Zingler

Hugh Janus said:
OK,

My app opens a form and waits for a user to press a button called
"Go". If I pass the parameter Q at startup I want it to not show the
form but call "Go.PerformClick". So in essence it runs like a
console app writing text out to the DOS window that called it.

The problem I have is that when Sub Main runs, it works out if the
correct parameter has been passed and parses the PerformClick sub
but does not execute it and then carries on with the rest of Sub
Main before ending the app. See code below:

Sub Main(ByVal Args() As String)

If Args.Length > 0 Then

If Args.GetValue(0) = "Q" Then

Console.WriteLine("Fase 3 started: "
&Now.ToShortTimeString)
Dim newF3 As New Phase3
newF3.Hide()

Call newF3.Gop.PerformClick

Console.WriteLine("Fase 3 ended: " &
Now.ToShortTimeString)

Else

Dim newF3 As New Phase3
newF3.ShowDialog()

End If


What am I doing wrong and what do I need to do to force it to wait
until the Sub has finished? I even tried putting it in a thread and
then using .Join but it even jumps this.


IMO, the approach is not very good. A Form should be used to show something.
If you don't intend to show anything, do not create a Form instance.

Put the code you currently have in the button's click event into a seperate
procedure /outside/ the Form. For example, you can put it into a class and
use the Shared modifier, or into a Module. Then, You can call the procedure
from the button's click event handler as well as from sub main. Create the
Form instance only if no args have been passed.


Armin
 
Z

zacks

Not really sure what is causing your problem, but I would attack it
thussly. Create a booolean property in your form with an initial value
of false. In Sub Main, always dim the form as a new instance. If the
command line paremeter is present, set the property to true. Show the
form and in the form' Load event, check the property. If true, hide and
perform click.
 
H

Hugh Janus

typical, I spend ages looking for the solution but can't find it. i
post the question here and then 2 minutes later I work it out.

Changing newF3.Go.PerformClick for the following seems to do the trick:

Application.Run(newF3)


and then:

Private Sub Phase3_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Me.Hide
Call Me.Go_Click(Me, e.Empty)
Application.Exit()

End Sub


But, I still can't get it to write text back to the DOS prompt that
called it. Ideas anyone?
 
H

Hugh Janus

And once again!!!!! Time to stop posting i think.

change

Console.WriteLine("Hello World.")

for

Console.Out.WriteLine("Hello World.")
 
A

Armin Zingler

Hugh Janus said:
typical, I spend ages looking for the solution but can't find it. i
post the question here and then 2 minutes later I work it out.

Changing newF3.Go.PerformClick for the following seems to do the
trick:

Application.Run(newF3)


and then:

Private Sub Phase3_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Me.Hide

Application.Run shows the Form. Why do you show a Form if you hide it?

Call Me.Go_Click(Me, e.Empty)
Application.Exit()

In most cases, Application.Exit is not required. Exit all procedures in
revers order and the thread/process will exit.
End Sub


But, I still can't get it to write text back to the DOS prompt that
called it. Ideas anyone?


Armin
 
H

Herfried K. Wagner [MVP]

Hugh Janus said:
My app opens a form and waits for a user to press a button called "Go".
If I pass the parameter Q at startup I want it to not show the form
but call "Go.PerformClick". So in essence it runs like a console app
writing text out to the DOS window that called it.
[...]
What am I doing wrong and what do I need to do to force it to wait
until the Sub has finished? I even tried putting it in a thread and
then using .Join but it even jumps this.

Check out 'Application.Run' and the 'ApplicationContext' class.
 
B

Brian Henry

its not "waiting" because it is dropping out because there is no message
queue loop. look at the application class and the application.run method
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top