473,394 Members | 1,751 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Sub main() program start problem

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
8 6417
"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
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

"_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
* 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
* _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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alex Vinokur | last post by:
Hi, I have a problem with invoking an instance before main() when using memory allocation for static member. Is the program below valid? =========================================== Windows...
4
by: Jacek Dziedzic | last post by:
Hi! First of all, I hope my problem is not too loosely tied to the "standard C++" that is the topic of this group. I have some code that exhibits a strange behaviour: on one computer, where I...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
75
by: Beni | last post by:
I have been programming in C for about a year now. It sounds silly, but I never took the time to question why a C(or C++ or Java) program execution begins only at the main(). Is it a convention or...
5
by: Guruz | last post by:
hi C gurus do anyone of u know how to write a program in C without main and still create a executable out of it. Remember, I said no main() function not in -->include files -->libraries -->no...
13
by: Sokar | last post by:
I have my main function set up as int main(int argv, char *argv) so taht i can read in a variable which is passed to the program on the command line. The problem is that main calls other...
4
by: Bob Day | last post by:
Using VS 2003, VB.net , MSDE .... I am confused by the help example below. In a sub main (whether in a module or winform), do you have to explicitly start the Main_Thread, as they do in this...
16
by: Adam Honek | last post by:
This is probably the most silly question asked here but I just can't figure it out. In VB6 letting Sub Main() be the app's startup directory was real easy under project properties. In VB.Net...
9
by: Tyler | last post by:
Hello All: I am currently working on a project to create an FEM model for school. I was thinking about using wxPython to gather the 12 input variables from the user, then, after pressing the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.