Home All Groups Group Topic Archive Search About

Sub Main is not waiting!!!??

Author
14 Mar 2006 3:02 PM
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

Author
14 Mar 2006 3:16 PM
Armin Zingler
Show quote Hide quote
"Hugh Janus" <my-junk-acco***@hotmail.com> schrieb
> 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
Author
14 Mar 2006 3:28 PM
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.
Author
14 Mar 2006 3:28 PM
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?
Author
14 Mar 2006 3:50 PM
Hugh Janus
And once again!!!!!  Time to stop posting i think.

change

Console.WriteLine("Hello World.")

for

Console.Out.WriteLine("Hello World.")
Author
14 Mar 2006 3:57 PM
Armin Zingler
Show quote Hide quote
"Hugh Janus" <my-junk-acco***@hotmail.com> schrieb
> 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
Author
14 Mar 2006 5:11 PM
Herfried K. Wagner [MVP]
"Hugh Janus" <my-junk-acco***@hotmail.com> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
14 Mar 2006 6:12 PM
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


Show quoteHide quote
"Hugh Janus" <my-junk-acco***@hotmail.com> wrote in message
news:1142348548.285891.119600@i39g2000cwa.googlegroups.com...
> 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
>