Results 1 to 17 of 17

Thread: NEWBIE: Start Program using sub Main

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2002
    Posts
    24

    NEWBIE: Start Program using sub Main

    Heyas! New to this .NET stuff. I was actually getting pretty decent at VB6...

    Anyway, an easy one for ya'll, I'm sure. I did all the appropriate searches, found some examples, but as usual I'm missing some minor thing...

    How do you start up a program using the sub Main? I tried dimming my main form (let's call it frmMain) and then doing a .Show, as my examples showed me. But, the program briefly started up and then closed itself??? Calling frmMain as the startup form from the project properties works just fine, I just can't hide/show without frmMain being instantiated (is that the right word?) Or, what other ways can anyone suggest?

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    This is because Sub Main continues after opening the form. And when Sub Main finishes, the main app thread finishes, closing the app.

    There are 2 things you can do.

    Either:
    'Restart the app with frmMain as the main thread.
    Dim F As New frmMain
    Application.Run(F)

    or:
    'Show the form as a dialog, thus suspending code execution until it's closed.
    Dim F As New frmMain
    F.ShowDialog
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2002
    Posts
    24
    Pax,

    Thank you for the reply. Unfortunately, these are the examples my search turned up. However, I can't get either to work. Obviously, I'm doing something small wrong...

    In the first example, I set frmMain as the main thread. Where does the code go?

    In the second example, I set sub Main as the main thread. Where does the code go? It actually works appropriately, but I still can't get "F" to be recognized. For instance, if I try an F.Hide, it tells me "F" is not declared. I thought I had declared it in the sub Main? Even doing a Public Shared sub Main, the rest of the program doesn't recognize it.

    Obviously, I haven't gotten my mind around the instantiation in .NET. Thanks for any help.

  4. #4
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    If your declaration is in a module and you declare F as public (the example of pax declared it with Dim)

    Public F As New frmMain
    F.ShowDialog

    you should recognize it everywhere in your application.....or not?

    I'm intersted in your response! Good job!
    Live long and prosper (Mr. Spock)

  5. #5
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Sorry dude.

    I didn't catch the part about instantiating.

    But Alextyx allready covered for me...

    What he said, should work.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2002
    Posts
    24
    Ok, it's official, I'm an idiot. So, thanks for all the help so far.

    I understand what you fellas are telling me. I understand the (simple) code.

    But, I'm doing something wrong. Here's what I want to do: Simple program, two forms. The first one opens up at program startup. User presses a button and first form hides and second one opens. User presses button on second form and it hides and first one opens back up.

    Now, from the code section of the first form it's simple to declare a new instance of the second form and call it. My problem is getting the first form instantiated (again, I think I'm using the term correctly...) so I can call the .Show and .Hide methods.

    I've tried the code in sub Main, in a module, public, private, dim, shared, everything I can think of.

    Like I mentioned above, I've missed something minor here.

    Again, thanks for all the help fellas!

  7. #7
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    Don't worry. All beginning is hard. And there are no such thing as a dumb question.
    Remember, everybody on this forum was a beginner at some point...
    Soon, you'll be the one helping others.

    Anyway, I made a small sample, which I hope will help you.
    Attached Files Attached Files
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  8. #8
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    You are not an idiot, be sure!

    What you want to do, might be a little less easy than it appears.
    I tell you my application arch.:
    I start from a form, not from a module, that is called FrmStart. If, inside FrmStart, I declare and show a second Form (let's call it Form2), obviously FrmStart's code continues to run. If I try to use showdialog for Form2, it works perfectly, but there is a little problem, i can't hide FormStart.
    If I try to write this in FormStart:

    Dim Form2 As New GIN.FrmXProve
    Me.Hide()
    Form2.ShowDialog()
    Me.Show()

    FormStart disappears and immediately comes back, also if code does not run on Me.Show instruction. This due to the fact I called a modal object, I think. The same result I obtain with

    Dim Form2 As New GIN.FrmXProve
    Me.Hide()
    MsgBox("Is FrmStart Visible?")
    Me.Show()

    When msgbox is waiting for your OK, you can see also FormStart!
    This was not a problem for me because I come for an experience in firmware and I have a strange habit: my application run under an interrupt control. Before to see the effect I described above, I design my forms (FormStart and two other important Forms) with an internal Timer that every 300 msec. run an application that monitor which is the situation. If someone pressed a button, he only set a flag. My cicling main routine will decide if to serve this flag or not. This only for my mental habit to test I/O and serve serial buffer and so on, at specified intervals. So I can write:

    Dim Form2 As New GIN.FrmXProve
    Me.Hide()
    Form2.Show

    Not ShowDialog !!!

    FormStart disappears now, but 300 msec later, its Timer run the sub I called GestStart that tests a lot of things, among which if a specific public variable boolean (I called PblBlnFormStartNascondi) is True or False. If Form2 has finished to work (disappearing or closing it depends by your code) and before it have setted this variable properly, FormStart make itself become visible.

    Me.Show

    Obviously you can use a test on Form2, in place of testing the variable, but it not changes the basic idea.

    This is my way, but perhaps, starting with a module you might not have the problem, at all. I did not try that way because it was solved automatically by this way that I use for habit.


    I think other friends may give you better ideas.
    Live long and prosper (Mr. Spock)

  9. #9
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Originally posted by alextyx

    Dim Form2 As New GIN.FrmXProve
    Me.Hide()
    Form2.ShowDialog()
    Me.Show()

    FormStart disappears and immediately comes back, also if code does not run on Me.Show instruction. This due to the fact I called a modal object, I think. The same result I obtain with
    Strange...I have never seen this behaviour.
    That is exactly what I do in the sample I posted earlier, and that tested OK on my machine. FormStart should not be able to come back before Form2 is closed, since Form2 is shown as a dialog, thus suspending all code execution in the calling form.

    The only difference is that I use "Form2.ShowDialog(Me)", which tells Form2 who owns it.

    Maybe that's the reason, I don't really now.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  10. #10
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Dear pax, your words were very useful! I made some considerations and tests to verify the problem Dador might encounter. The first was:

    Since I use FormStart as starting object and I not declare and istance it esplicitly, I can't write, for example, FrmStart.Hide or FrmStart.Show, in the famous Form2! So I can't manage it from that position.


    The second was using a showdialog as described, but, as I said I have a Timer structure. Once I read your words a suspect will raise in my mind: Is FrmStart's code execution really stopped by show dialog? Answer: The lines code after showdialog are not executed until Form2 is closed, but.....the timer routine yes
    I'm not enough experienced and I supposed a different behaviour, but this is the fact!....and in my timer routine what we could find....the test of the variable and consequent command Me.show

    Dador, what we are discussing about, is your problem? If it's different, speak freely....one problem more, one problem less...!
    (more enemies, more honour!)

    I'm very ostinate and I'm sure we can solve it. What is not working in your application? Where is exactly the failure?
    Live long and prosper (Mr. Spock)

  11. #11
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Ahhh, the infamous timer...I've never really noticed, but now that you mention it, that will probably keep firing it's event. Maybe it runs on another thread??? I don't know, but it's beginning to make sence, as to what's really going on here.

    But I think the real problem that Dador is experiencing, is that he uses F.Hide or frmMain.Hide instead of Me.Hide
    And that could make all the difference.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  12. #12
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Dear Pax, the strange thing is that for what I've read about timer, they are executed in the same thread! I belevied to know what is a thread, but I never tried to add a thread at my app. If I had to do it, I'd have to refresh my memory on books, because I've never written something of that kind!!! Note that Timer was running continously, not once only. With Form2 waiting for my action and lines after ShowDialog not reached by the execution (I put a break there) just as it should happen, the timer werw fired continously

    So I'm realizing that thread is not what I thought. If your code is stopped, in a certain path, others line, also in the same object, can be executed. I think the right concept (by the facts I observed) is that thread must consider not as a constraint for lines to be executed, but only for resources available. If my app. were running in a cicle, using heavily all the resources of the thread, the Timer should stay silent. If I put Timer in a separate thread it works, because has machine resource available. Isn't it?
    Live long and prosper (Mr. Spock)

  13. #13
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hmmm...Now you got me all confused

    To be honest, I don't know much about threading, so perhaps I was wrong about the showdialog halting everything. It could be that only the sub where the call is in is halted, and the rest of the forms code actually run as normal?!?

    I have done a bit of threading on a few small app's, and I usually end up screwing everything up, because I loose control over what happens when.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  14. #14
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    First of all, I want to say to Dador that we are far from his path, but I'm always waiting to have a chance to be useful for him.... You we're not forgiven
    While Dador probably is asking to itself why it's so diffcult to obtain a definitve help by me (It's because I've a certain age, I think, my brain is not very efficient ) and with a lot of patience studies a way to explain me where his problem is, I invite you, Pax, to test the fact I told you. I don't believe I made mistake, but.....you showed that it can happen. Your feedback was very useful before!
    If you will have the same result, probably we will have a better knowledge of what Thread means.
    You need only to put a timer in a form and execute a showdialog, then see what happens to timer routine.
    Live long and prosper (Mr. Spock)

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Apr 2002
    Posts
    24
    Again, thanks fellas!

    Yeah, you guys are seeing some of the problems I was seeing.

    In VB6, I understand the form was instantiated immediately, so your startup form could simply be closed with a me.close (or a Form1.close) without ever having to declare the form in the first place. Then, I could call it back from a second form without any problems.

    In .NET, yes I can use a me.close. But, once in the second form, I can't call the first one.

    So, I did this trick in the first form (which is what ya'll were telling me to do, I just didn't get it):

    VB Code:
    1. Private Sub btnMVS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMVS.Click
    2.  
    3.         Me.Hide()
    4.         Dim frmMVS As New frmMVS
    5.         frmMVS.ShowDialog()
    6.         Me.Show()
    7.  
    8.     End Sub

    That works perfect for me, as the second form, frmMVS, can just close and the first form picks right back up.

    THanks again folks!

  16. #16
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Perfect! So, our chattering was useful in some way!
    Good job Dador and....avoid Timers
    Live long and prosper (Mr. Spock)

  17. #17
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Originally posted by alextyx
    I invite you, Pax, to test the fact I told you. I don't believe I made mistake, but.....you showed that it can happen. Your feedback was very useful before!
    If you will have the same result, probably we will have a better knowledge of what Thread means.
    You need only to put a timer in a form and execute a showdialog, then see what happens to timer routine.
    I just tried it, and yes, the timer event does fire indeed. So it appears that it's only the calling sub that's halted.

    So, before they move this thread to chit-chat, I want to thank both of you for a good chat. I definitly learned some new things.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width