By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,742 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,742 IT Pros & Developers. It's quick & easy.

Sub main() program start problem

P: n/a
I am starting a program from a module with the Sub main procedure and
I want it to display two forms for the program's interface, but when I
run the program both forms just open and then program closes.

Dim FORM1 As New Form1
Dim FORM2 As New form2

Sub main()
FORM1.Show()
FORM2.Show()
End Sub

I am assuming I need something after the second .Show(), to stop the
program from ending, but I have no idea what. I know other people have
used a dialogue to stop the procedure running through, but I want to
give the user access to both forms. Can anyone help?
Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"koorb" <ko***@raidrs.co.uk> schrieb
I am starting a program from a module with the Sub main procedure and
I want it to display two forms for the program's interface, but when I
run the program both forms just open and then program closes.

Dim FORM1 As New Form1
Dim FORM2 As New form2

Sub main()
FORM1.Show()
FORM2.Show()
End Sub

I am assuming I need something after the second .Show(), to stop the
program from ending, but I have no idea what. I know other people have
used a dialogue to stop the procedure running through, but I want to
give the user access to both forms. Can anyone help?


You have to define the main form of your application, e.g. if FORM1 is you
main form:

Sub Main()
System.Windows.Forms.Application.Run(new Form1())
End Sub

Cheers

Arne Janning
Nov 21 '05 #2

P: n/a
yes. this is linked to a question that i just asked.

the problem is, the forms show, and then the module ends... taking the
forms with it in GC.

you need to keep the module alive. the way i do it is by making an 'idle
loop' after showing the forms. i have asked if this is the right way or
not, because i am not sure. you can make them modal as well:

sub main()

TheForm.Show
TheForm2.Show

Do
If g_bEnd = True Then Exit Do
IdleTime()
Loop

end sub

Sub IdleTime()

System.Threading.Thread.CurrentThread.Sleep(60)
Application.DoEvents()

End Sub
or you could do this:

TheForm.ShowDialog
TheForm2.ShowDialog

but it <> good i think
1§ 1§

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #3

P: n/a

"_IS_ -" <g0******@campus.ru.ac.za> schrieb
yes. this is linked to a question that i just asked.

the problem is, the forms show, and then the module ends... taking the
forms with it in GC.

you need to keep the module alive. the way i do it is by making an 'idle
loop' after showing the forms. i have asked if this is the right way or
not, because i am not sure. you can make them modal as well:

sub main()

TheForm.Show
TheForm2.Show

Do
If g_bEnd = True Then Exit Do
IdleTime()
Loop

end sub

Sub IdleTime()

System.Threading.Thread.CurrentThread.Sleep(60)
Application.DoEvents()

End Sub
or you could do this:

TheForm.ShowDialog
TheForm2.ShowDialog

but it <> good i think


Windows-programs have to be running, so a message queue has to be created
together with every Windows programm where Windows puts its Messages.
In Windows.Forms you create the message queue with
Application.Run(New myForm()) (typically in Sub Main)
where myForm is the main form of your application.

The program will end after and only after myForm has closed.

If you want to open another Form from inside myForm then add somewhere:

Dim f2 as myForm2 = new myForm2()
f2.Show()

Cheers

Arne Janning
Nov 21 '05 #4

P: n/a
* koorb <ko***@raidrs.co.uk> scripsit:
I am starting a program from a module with the Sub main procedure and
I want it to display two forms for the program's interface, but when I
run the program both forms just open and then program closes.

Dim FORM1 As New Form1
Dim FORM2 As New form2

Sub main()
FORM1.Show()
FORM2.Show()
\\\
Application.Run()
///
End Sub


You can use 'Application.ExitThread' to exit the application. Take a
look at the 'ApplicationContext' class too.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #5

P: n/a
* _IS_ - <g0******@campus.ru.ac.za> scripsit:
you need to keep the module alive. the way i do it is by making an 'idle
loop' after showing the forms. i have asked if this is the right way or
not, because i am not sure. you can make them modal as well:

sub main()

TheForm.Show
TheForm2.Show

Do
If g_bEnd = True Then Exit Do
IdleTime()
Loop

end sub

Sub IdleTime()

System.Threading.Thread.CurrentThread.Sleep(60)
Application.DoEvents()

End Sub


Mhm... That's kind of busy waiting and thus not an optimal solution.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #6

P: n/a
hello, hope tht i can reuse this thread to ask... what would be the
optimum solution to "keep a module class alive"? or am i looking at it
wrong?
Is "busy waiting" not what the pc does anyway? System idle process etc?

ps code is a very pretty way to express things...

1§ 1§

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #7

P: n/a
Hello, koorb:

Your problem arises because all Windows Forms programs must have a message loop.
You can create the message loop with the application.run method. Depending on how you call application.run, you should exit the message loop in a different way:

· When calling application.run() the message loop keeps working until you call application.exitthread. Until then, it keeps running even if you close all the forms. When you call application.exitthread, all forms depending on that message loop are closed.

· When calling application.run(form) the message loop ends when you close the form.

· When calling application.run(applicationcontext) the message loop ends when you close the applicationcontext.mainform. Note that you can change the applicationcontext.mainform to a second form, then close (or leave visible) the first one; this way the message loop ends when you close the second form.
I haven't been able to understand completely the behavior of application.exitthread when I use application.run(form) or application.run(applicationcontext). It seems to end the message loop, close the form and then skip the two following message loops. It seems that a modal form (like messagebox or form.showdialog) creates a message loop if there isn't one running.

I have ilustrated all with a sample that you can compile with the following command:
\WINDOWS\Microsoft.NET\Framework\v1.1.4322\vbc.exe /target:winexe /imports:system.windows.forms,microsoft.visualbasic ,system.collections /r:system.windows.forms.dll,system.dll,System.Drawi ng.dll,Microsoft.VisualBasic.dll /m:startmodule appexit.vb

Note the following:
· Using form.close in all the forms of the second loop of my sample will make the program keep working without user interface. In that case, use the task manager to kill the process.
· For modeless forms, pressing the form's close button ("x") is the same that calling form.close.
· The form.closing event is not fired if the form is not explicitly closed, for example when calling application.exitthread.

'********************** start of appexit.vb file **********************
imports system.windows.forms

module StartModule
friend ac as applicationcontext
public sub Main()
dim a,b,c as form
msgbox("Program starts.")

a=new form1
a.text="First message loop. Use the close button."
application.run(a)
msgbox("First message loop ended.")

a=new form1:b=new form1:c=new form2
a.text="Second message loop. Use ExitThread on any form."
a.show:b.show:c.show
application.run() 'Use ExitThread. If you close all the forms, the message loop keeps running and the program does not end.
'Note: When using application.run() you can use the form.closing event to call application.exitthread.
msgbox("Second message loop ended.")

a=new form1
a.text="Third message loop. Use the close button or change the form."
ac=new applicationcontext(a)
application.run(ac)
msgbox("Program ends.")
end sub
end module

class Form1
inherits form
private withevents b1 as button
private withevents b2 as button
private withevents b3 as button
private withevents b4 as button

sub new
mybase.new
me.text="This is Form1"
b1=new button
b1.text="Open another"
b1.visible=false
b2=new button
b2.text="ExitThread"
b2.location=new system.drawing.point(0,25)
b3=new button
b3.text="Close"
b3.location=new system.drawing.point(0,50)
b4=new button
b4.text="Change form"
b4.location=new system.drawing.point(0,75)
me.controls.addrange(new control(){b1,b2,b3,b4})
end sub

private sub b1_click(p1 as object, p2 as system.eventargs) handles b1.click
call (new form1).show
end sub

private sub b2_click(p1 as object, p2 as system.eventargs) handles b2.click
application.exitthread
end sub

private sub b3_click(p1 as object, p2 as system.eventargs) handles b3.click
me.close
end sub

private sub b4_click(p1 as object, p2 as system.eventargs) handles b4.click
if not ac is nothing then
ac.mainform=new form2
ac.mainform.show 'The new form is not showed automatically.
me.close '«me» is not related now whith the applicationcontext object.
end if
end sub
end class

class form2
inherits form 'Empty form.
end class
'********************** end of appexit.vb file **********************

Regards.
"koorb" <ko***@raidrs.co.uk> escribió en el mensaje news:dt********************************@4ax.com...
| I am starting a program from a module with the Sub main procedure and
| I want it to display two forms for the program's interface, but when I
| run the program both forms just open and then program closes.
|
| Dim FORM1 As New Form1
| Dim FORM2 As New form2
|
| Sub main()
| FORM1.Show()
| FORM2.Show()
| End Sub
|
| I am assuming I need something after the second .Show(), to stop the
| program from ending, but I have no idea what. I know other people have
| used a dialogue to stop the procedure running through, but I want to
| give the user access to both forms. Can anyone help?
Nov 21 '05 #8

P: n/a
On Fri, 27 Aug 2004 10:54:14 -0700, _IS_ - wrote:
the problem is, the forms show, and then the module ends... taking the
forms with it in GC.
A better solution would be something like this:
sub main()

TheForm.Show
TheForm2.Show
Application.Run()

end sub


In order to close the application later, you would have to call
Application.Exit.
--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.