Home All Groups Group Topic Archive Search About

Program not ending???

Author
1 Feb 2006 4:52 PM
cj
I run this program and to exit click the X in the upper right corner.
But apparently it isn't really ending the program.  If I return to VB
and make changes then try to rebuild the app it says the exe is still in
use--I find it is still a process in Task Manager.  What do I need to do
to make clicking that X actually end the program?


Public Class Form1
     Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
         Me.Show()
         Application.DoEvents()

         Dim result As DialogResult
         result = FolderBrowserDialog1.ShowDialog()
         If result = DialogResult.Cancel Then
             End
         End If

         ' Create a reference to the current directory.
         Dim DirInfo As New
System.IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
         ' Create an array representing the files in the current directory.
         Dim FileInfo As System.IO.FileInfo() = DirInfo.GetFiles("*.jpg")

         For Each File As System.io.FileInfo In FileInfo
             Label1.Text = File.FullName
             PictureBox1.Image = Image.FromFile(File.FullName)
             Application.DoEvents()
             System.Threading.Thread.Sleep(2000)
         Next

         End
     End Sub
End Class

Author
1 Feb 2006 5:03 PM
Armin Zingler
Show quote Hide quote
"cj" <cj@nospam.nospam> schrieb
> I run this program and to exit click the X in the upper right
> corner. But apparently it isn't really ending the program.  If I
> return to VB and make changes then try to rebuild the app it says
> the exe is still in use--I find it is still a process in Task
> Manager.  What do I need to do to make clicking that X actually end
> the program?
>
>
> Public Class Form1
>     Inherits System.Windows.Forms.Form
>
> #Region " Windows Form Designer generated code "
>
>     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>         Me.Show()
>         Application.DoEvents()

This is bad design. Don't call Show in Form_Load. Load has been raised
because Show has been executed before. I also don't know why you want to
process all windows messages in the message queue by calling DoEvents.

>         Dim result As DialogResult
>         result = FolderBrowserDialog1.ShowDialog()
>         If result = DialogResult.Cancel Then
>             End

Don't use End. It is not necessary.

Show quoteHide quote
>         End If
>
>         ' Create a reference to the current directory.
>         Dim DirInfo As New
> System.IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
>         ' Create an array representing the files in the current
> directory.         Dim FileInfo As System.IO.FileInfo() =
> DirInfo.GetFiles("*.jpg")
>
>         For Each File As System.io.FileInfo In FileInfo
>             Label1.Text = File.FullName
>             PictureBox1.Image = Image.FromFile(File.FullName)
>             Application.DoEvents()
>             System.Threading.Thread.Sleep(2000)
>         Next
>
>         End
>     End Sub
> End Class


You never exit the loop, thus the program never ends.

If you do everything in Form_Load, the load will never be complete.


Armin
Author
1 Feb 2006 6:33 PM
cj
Armin Zingler wrote:
Show quoteHide quote
> "cj" <cj@nospam.nospam> schrieb
>> I run this program and to exit click the X in the upper right
>> corner. But apparently it isn't really ending the program.  If I
>> return to VB and make changes then try to rebuild the app it says
>> the exe is still in use--I find it is still a process in Task
>> Manager.  What do I need to do to make clicking that X actually end
>> the program?
>>
>>
>> Public Class Form1
>>     Inherits System.Windows.Forms.Form
>>
>> #Region " Windows Form Designer generated code "
>>
>>     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
>> System.EventArgs) Handles MyBase.Load
>>         Me.Show()
>>         Application.DoEvents()
>
> This is bad design.

Perhaps

Don't call Show in Form_Load. Load has been raised
> because Show has been executed before. I also don't know why you want to
> process all windows messages in the message queue by calling DoEvents.
>

If I don't call me.show() the program never displays on the screen

>>         Dim result As DialogResult
>>         result = FolderBrowserDialog1.ShowDialog()
>>         If result = DialogResult.Cancel Then
>>             End
>
> Don't use End. It is not necessary.
>

If I don't put end there then if I choose cancel the program will try to
work when a directory has not been selected and it gives an error.

Show quoteHide quote
>>         End If
>>
>>         ' Create a reference to the current directory.
>>         Dim DirInfo As New
>> System.IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
>>         ' Create an array representing the files in the current
>> directory.         Dim FileInfo As System.IO.FileInfo() =
>> DirInfo.GetFiles("*.jpg")
>>
>>         For Each File As System.io.FileInfo In FileInfo
>>             Label1.Text = File.FullName
>>             PictureBox1.Image = Image.FromFile(File.FullName)
>>             Application.DoEvents()
>>             System.Threading.Thread.Sleep(2000)
>>         Next
>>
>>         End
>>     End Sub
>> End Class
>
>
> You never exit the loop, thus the program never ends.

It's a for each loop.  The loop exits when all the files are displayed.

>
> If you do everything in Form_Load, the load will never be complete.

Hu?  the load would be complete when everything in it is done.  And the
last thing that is done is ending the program.

>
>
> Armin
>

Why wouldn't clicking the X just abruptly end the program instead of
just removing the form and leaving something running?
Author
1 Feb 2006 7:01 PM
Armin Zingler
"cj" <cj@nospam.nospam> schrieb
> > because Show has been executed before. I also don't know why you
> > want to process all windows messages in the message queue by
> > calling DoEvents.
> >
>
> If I don't call me.show() the program never displays on the screen

Yes, because you execute the code /before/ the Form has been shown, i.e. in
form_load. Why not
1. show the form
2. execute the code
?

> >
> > Don't use End. It is not necessary.
> >
>
> If I don't put end there then if I choose cancel the program will
> try to work when a directory has not been selected and it gives an
> error.

Yes, but only because you put the code in the wrong place. (see other post)

> > You never exit the loop, thus the program never ends.
>
> It's a for each loop.  The loop exits when all the files are
> displayed.

Sorry, chose the wrong words. Wanted to say: Nowhere you exit the loop when
the Form has been closed.


> Why wouldn't clicking the X just abruptly end the program instead of
> just removing the form and leaving something running?

The loop is still running. You don't leave it when the form has been closed.


Armin
Author
1 Feb 2006 7:35 PM
cj
Armin Zingler wrote:
Show quoteHide quote
> "cj" <cj@nospam.nospam> schrieb
>> > because Show has been executed before. I also don't know why you
>> > want to process all windows messages in the message queue by
>> > calling DoEvents.
>> >
>>
>> If I don't call me.show() the program never displays on the screen
>
> Yes, because you execute the code /before/ the Form has been shown, i.e.
> in form_load. Why not
> 1. show the form
> 2. execute the code
> ?
>

That's fine.  What event would I use.  I do not wish to use another
control--timer.  I want this to run when the program starts.  Since you
indicated you have experience pre-.net please help me from that mindset.
  I do not understand your .net code.

Show quoteHide quote
>> >
>> > Don't use End. It is not necessary.
>> >
>>
>> If I don't put end there then if I choose cancel the program will
>> try to work when a directory has not been selected and it gives an
>> error.
>
> Yes, but only because you put the code in the wrong place. (see other post)
>
>> > You never exit the loop, thus the program never ends.
>>
>> It's a for each loop.  The loop exits when all the files are
>> displayed.
>
> Sorry, chose the wrong words. Wanted to say: Nowhere you exit the loop
> when the Form has been closed.
>
>
>> Why wouldn't clicking the X just abruptly end the program instead of
>> just removing the form and leaving something running?
>
> The loop is still running. You don't leave it when the form has been
> closed.
>
>
> Armin
Author
1 Feb 2006 8:08 PM
Armin Zingler
"cj" <cj@nospam.nospam> schrieb
> > >
> > > If I don't call me.show() the program never displays on the
> > > screen
> >
> > Yes, because you execute the code /before/ the Form has been
> > shown, i.e. in form_load. Why not
> > 1. show the form
> > 2. execute the code
> > ?
> >
>
> That's fine.  What event would I use.

There is no event. These are two commands or two blocks of commands.

>  I do not wish to use another
> control--timer.

Why?

> I want this to run when the program starts.

See my other code: it runs in Sub Main. Sub Main is the sub that is started
when you start the application (open the project properties to set the
"startup object")

> Since
> you indicated you have experience pre-.net please help me from that
> mindset.  I do not understand your .net code.

Which part? The code only

1. shows the Form
2. Shows the Folderbrowserdialog
3. If step two has not been cancelled:
3.1. The path is passed to the Form and the timer is started.
3.2. Application.run starts the message loop (running til the Form has been
closed)


Armin
Author
1 Feb 2006 8:35 PM
cj
Allright first off I can't keep updating down in the message.  I'm
getting lost.

Where do I put your code?  I'm looking at a form with a timer on it.


Armin Zingler wrote:
Show quoteHide quote
> "cj" <cj@nospam.nospam> schrieb
>> > >
>> > > If I don't call me.show() the program never displays on the
>> > > screen
>> >
>> > Yes, because you execute the code /before/ the Form has been
>> > shown, i.e. in form_load. Why not
>> > 1. show the form
>> > 2. execute the code
>> > ?
>> >
>>
>> That's fine.  What event would I use.
>
> There is no event. These are two commands or two blocks of commands.
>
>>  I do not wish to use another
>> control--timer.
>
> Why?
>
>> I want this to run when the program starts.
>
> See my other code: it runs in Sub Main. Sub Main is the sub that is started
> when you start the application (open the project properties to set the
> "startup object")
>
>> Since
>> you indicated you have experience pre-.net please help me from that
>> mindset.  I do not understand your .net code.
>
> Which part? The code only
>
> 1. shows the Form
> 2. Shows the Folderbrowserdialog
> 3. If step two has not been cancelled:
> 3.1. The path is passed to the Form and the timer is started.
> 3.2. Application.run starts the message loop (running til the Form has been
> closed)
>
>
> Armin
>
Author
1 Feb 2006 8:40 PM
Armin Zingler
"cj" <cj@nospam.nospam> schrieb
> Allright first off I can't keep updating down in the message.  I'm
> getting lost.
>
> Where do I put your code?  I'm looking at a form with a timer on it.

Where's the folderbrowserdialog, the picturebox and the label?  :-)



Public Class Form1
     Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "


    '<--- Put my code here --->

End Class


Armin
Author
1 Feb 2006 8:53 PM
cj
What is Shared Sub main()?  Does VB automatically run it?   I've always
used the form load event in clasic VB.  Never seen a sub main().
Haven't seen that in any book on .net either not that I've found any
good books on .net.

Armin Zingler wrote:
Show quoteHide quote
> "cj" <cj@nospam.nospam> schrieb
>> Allright first off I can't keep updating down in the message.  I'm
>> getting lost.
>>
>> Where do I put your code?  I'm looking at a form with a timer on it.
>
> Where's the folderbrowserdialog, the picturebox and the label?  :-)
>
>
>
> Public Class Form1
>     Inherits System.Windows.Forms.Form
>
> #Region " Windows Form Designer generated code "
>
>
>    '<--- Put my code here --->
>
> End Class
>
>
> Armin
Author
1 Feb 2006 9:02 PM
Armin Zingler
"cj" <cj@nospam.nospam> schrieb
> What is Shared Sub main()?  Does VB automatically run it?

>  I've
> always used the form load event in clasic VB.  Never seen a sub
> main().

Didn't I mention it already?

<quote>
See my other code: it runs in Sub Main. Sub Main is the sub that is started
when you start the application (open the project properties to set the
"startup object")
</quote>

Yes, you can also choose a Form class as the startup object. In that case,
sub main is internally created automatically. If you choose "Form1" as the
startup object, this sub is created within Form1:

Shared Sub Main
    Application.Run(New Form1())
End Sub


> Haven't seen that in any book on .net either not that I've
> found any good books on .net.


<F1> is sometimes sufficient:  ;-)

http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconVisualBasicModules.asp


Haven't found it in any book, too - probably because I don't own one. :)


Armin
Author
1 Feb 2006 9:36 PM
cj
never set a startup object before.  never seen Application.Run(New
Form1()) either.  If it's the default I guess it must be in that Windows
form designer code area--I don't dare look in there.

I know you'll correct me if I'm wrong but sub main didn't exist in
classic VB?  I used form load in classic VB.  Is form load now
considered inappropriate for running code that needs to run when the
program starts?

I'll have to disagree with you on F1.  It's way to much stuff and too
confusing for me now at least.  I used to love it back in VB4 but now
it's worthless.  The sample code is always complicated and I can't just
cut and paste it to see it work.

Armin Zingler wrote:
Show quoteHide quote
> "cj" <cj@nospam.nospam> schrieb
>> What is Shared Sub main()?  Does VB automatically run it?
>
>>  I've
>> always used the form load event in clasic VB.  Never seen a sub
>> main().
>
> Didn't I mention it already?
>
> <quote>
> See my other code: it runs in Sub Main. Sub Main is the sub that is started
> when you start the application (open the project properties to set the
> "startup object")
> </quote>
>
> Yes, you can also choose a Form class as the startup object. In that
> case, sub main is internally created automatically. If you choose
> "Form1" as the startup object, this sub is created within Form1:
>
> Shared Sub Main
>    Application.Run(New Form1())
> End Sub
>
>
>> Haven't seen that in any book on .net either not that I've
>> found any good books on .net.
>
>
> <F1> is sometimes sufficient:  ;-)
>
> http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconVisualBasicModules.asp
>
>
>
> Haven't found it in any book, too - probably because I don't own one. :)
>
>
> Armin
Author
1 Feb 2006 9:57 PM
Armin Zingler
"cj" <cj@nospam.nospam> schrieb
> never set a startup object before.  never seen Application.Run(New
> Form1()) either.  If it's the default I guess it must be in that
> Windows form designer code area--I don't dare look in there.

No. In a WindowsApplication, Sub Main is internally created automatically.
You don't see it. It does not exist in source code, also not in the designer
generated code. The compiler creates this sub in the generated EXE as if it
was part of the source code. This is done if the startup object is a Form
class. You can also write your sub Main on your own. If you do, it is not
generated automatically (because it doesn't have to be).


> I know you'll correct me if I'm wrong but sub main didn't exist in
> classic VB?

It was there, too. Never opened the project properites in VB6? :-) In small
projects, it was often sufficient to show the main form at startup, but in
most cases some initialization, opening database, showing splash screen
etc., was done before in sub main.

> I used form load in classic VB.  Is form load now
> considered inappropriate for running code that needs to run when the
> program starts?

No, of course you can put the code there. What you shouldn't do is terminate
the application in Form_Load. It's better to check if the preconditions are
met to show the form /before/ you show it. This can be done in Sub Main.

It's pretty simple: Determine what you want to do. 1. Do some checks, 2. if
checks are successful show the main form. (or see the more detailed steps
1. - 3.2. in the previous post).


> I'll have to disagree with you on F1.  It's way to much stuff and
> too confusing for me now at least.  I used to love it back in VB4
> but now it's worthless.  The sample code is always complicated and I
> can't just cut and paste it to see it work.

I think the table of contents is pretty good. I'd also rearrange some
topics, but in general, I have no problem with it.

Concerning the link I provided: This is one of the first topics of the VB
language tour. I think a good place because the "Structure of a Visual Basic
Program" is basic knowledge. The next topic ("hello world") is also helpful
concerning sub main. If you don't read it, you can not learn from it. :-)


Armin
Author
1 Feb 2006 9:59 PM
CMM
1) Sub Main has been part of VB always (since VB3 as far as I know and
probably before). You set it in the Project properties dialog.
2) F1 behavies differently in VS2005. Don't highlight any part of the word
in your source code that you're trying to look up. Just keep the cursor
anywhere in it, then hit F1.

Show quoteHide quote
"cj" <cj@nospam.nospam> wrote in message
news:%23s9BLe3JGHA.1288@TK2MSFTNGP09.phx.gbl...
> never set a startup object before.  never seen Application.Run(New
> Form1()) either.  If it's the default I guess it must be in that Windows
> form designer code area--I don't dare look in there.
>
> I know you'll correct me if I'm wrong but sub main didn't exist in classic
> VB?  I used form load in classic VB.  Is form load now considered
> inappropriate for running code that needs to run when the program starts?
>
> I'll have to disagree with you on F1.  It's way to much stuff and too
> confusing for me now at least.  I used to love it back in VB4 but now it's
> worthless.  The sample code is always complicated and I can't just cut and
> paste it to see it work.
>
> Armin Zingler wrote:
>> "cj" <cj@nospam.nospam> schrieb
>>> What is Shared Sub main()?  Does VB automatically run it?
>>
>>>  I've
>>> always used the form load event in clasic VB.  Never seen a sub
>>> main().
>>
>> Didn't I mention it already?
>>
>> <quote>
>> See my other code: it runs in Sub Main. Sub Main is the sub that is
>> started
>> when you start the application (open the project properties to set the
>> "startup object")
>> </quote>
>>
>> Yes, you can also choose a Form class as the startup object. In that
>> case, sub main is internally created automatically. If you choose "Form1"
>> as the startup object, this sub is created within Form1:
>>
>> Shared Sub Main
>>    Application.Run(New Form1())
>> End Sub
>>
>>
>>> Haven't seen that in any book on .net either not that I've
>>> found any good books on .net.
>>
>>
>> <F1> is sometimes sufficient:  ;-)
>>
>> http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconVisualBasicModules.asp
>> Haven't found it in any book, too - probably because I don't own one. :)
>>
>>
>> Armin
Author
1 Feb 2006 10:03 PM
Armin Zingler
"CMM" <cmm@nospam.com> schrieb
> 2) F1 behavies differently in VS2005. Don't highlight any part of
> the word in your source code that you're trying to look up. Just
> keep the cursor anywhere in it, then hit F1.


As he mentioned "#Region " Windows Form Designer generated code ", I guess
he is not using VB 2005.


Armin
Author
2 Feb 2006 1:53 PM
cj
Lets see if I can answer some of the question that came up overnight.

I'm using VS 2003.  Of course now 50% of the time I'm using Visual Fox
Pro 9 which I've never touched before.

No, never used project properties.  I wrote programs not projects. :)
Before .net I used VB4.  No flash screens either.  My programming has
always been for in-house use.  No need to advertise and nobody wants
extra glitz.

I'll take a look at your link as soon as I can.


Armin Zingler wrote:
Show quoteHide quote
> "CMM" <cmm@nospam.com> schrieb
>> 2) F1 behavies differently in VS2005. Don't highlight any part of
>> the word in your source code that you're trying to look up. Just
>> keep the cursor anywhere in it, then hit F1.
>
>
> As he mentioned "#Region " Windows Form Designer generated code ", I
> guess he is not using VB 2005.
>
>
> Armin
Author
1 Feb 2006 9:45 PM
cj
does f.Start(f.FolderBrowserDialog1.SelectedPath)
call Public Sub Start(ByVal Path As String)
?  must i reckon

Armin Zingler wrote:
Show quoteHide quote
> "cj" <cj@nospam.nospam> schrieb
>> What is Shared Sub main()?  Does VB automatically run it?
>
>>  I've
>> always used the form load event in clasic VB.  Never seen a sub
>> main().
>
> Didn't I mention it already?
>
> <quote>
> See my other code: it runs in Sub Main. Sub Main is the sub that is started
> when you start the application (open the project properties to set the
> "startup object")
> </quote>
>
> Yes, you can also choose a Form class as the startup object. In that
> case, sub main is internally created automatically. If you choose
> "Form1" as the startup object, this sub is created within Form1:
>
> Shared Sub Main
>    Application.Run(New Form1())
> End Sub
>
>
>> Haven't seen that in any book on .net either not that I've
>> found any good books on .net.
>
>
> <F1> is sometimes sufficient:  ;-)
>
> http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconVisualBasicModules.asp
>
>
>
> Haven't found it in any book, too - probably because I don't own one. :)
>
>
> Armin
Author
1 Feb 2006 10:01 PM
Armin Zingler
"cj" <cj@nospam.nospam> schrieb
> does f.Start(f.FolderBrowserDialog1.SelectedPath)
> call Public Sub Start(ByVal Path As String)
> ?  must i reckon


Yes, it calls sub start.


Armin
Author
1 Feb 2006 9:55 PM
CMM
Without really reading the rest of this thread (boring)...
What you want to do is
start up in Sub Main not Form1
Show your form from there SubMain: frm.Show
Execute the proc from Sub Main:  frm.DoSomething
Then close it from SubMain: frm.Close
then probably dispose of it frm.Dispose

I call this "puppeteer-ing a form"... which is OK.... it's really just a
class.

Show quoteHide quote
"cj" <cj@nospam.nospam> wrote in message
news:O1YDK41JGHA.516@TK2MSFTNGP15.phx.gbl...
>
>
> Armin Zingler wrote:
>> "cj" <cj@nospam.nospam> schrieb
>>> I run this program and to exit click the X in the upper right
>>> corner. But apparently it isn't really ending the program.  If I
>>> return to VB and make changes then try to rebuild the app it says
>>> the exe is still in use--I find it is still a process in Task
>>> Manager.  What do I need to do to make clicking that X actually end
>>> the program?
>>>
>>>
>>> Public Class Form1
>>>     Inherits System.Windows.Forms.Form
>>>
>>> #Region " Windows Form Designer generated code "
>>>
>>>     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
>>> System.EventArgs) Handles MyBase.Load
>>>         Me.Show()
>>>         Application.DoEvents()
>>
>> This is bad design.
>
> Perhaps
>
> Don't call Show in Form_Load. Load has been raised
>> because Show has been executed before. I also don't know why you want to
>> process all windows messages in the message queue by calling DoEvents.
>>
>
> If I don't call me.show() the program never displays on the screen
>
>>>         Dim result As DialogResult
>>>         result = FolderBrowserDialog1.ShowDialog()
>>>         If result = DialogResult.Cancel Then
>>>             End
>>
>> Don't use End. It is not necessary.
>>
>
> If I don't put end there then if I choose cancel the program will try to
> work when a directory has not been selected and it gives an error.
>
>>>         End If
>>>
>>>         ' Create a reference to the current directory.
>>>         Dim DirInfo As New
>>> System.IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
>>>         ' Create an array representing the files in the current
>>> directory.         Dim FileInfo As System.IO.FileInfo() =
>>> DirInfo.GetFiles("*.jpg")
>>>
>>>         For Each File As System.io.FileInfo In FileInfo
>>>             Label1.Text = File.FullName
>>>             PictureBox1.Image = Image.FromFile(File.FullName)
>>>             Application.DoEvents()
>>>             System.Threading.Thread.Sleep(2000)
>>>         Next
>>>
>>>         End
>>>     End Sub
>>> End Class
>>
>>
>> You never exit the loop, thus the program never ends.
>
> It's a for each loop.  The loop exits when all the files are displayed.
>
>>
>> If you do everything in Form_Load, the load will never be complete.
>
> Hu?  the load would be complete when everything in it is done.  And the
> last thing that is done is ending the program.
>
>>
>>
>> Armin
>>
>
> Why wouldn't clicking the X just abruptly end the program instead of just
> removing the form and leaving something running?
Author
2 Feb 2006 2:17 PM
cj
I agree boring, if I didn't need to get this info I wouldn't be reading
it.  What perplexes me is I see you did continue to read because you
posted at the end of the longest "thread" emanating from my original post.

Your suggestion is interesting however quite different from Armin's code
which has stuff like application.run(form) in it.  If you're both right,
and I think you are, then I must conclude there is no "right" way to
write this program.  Which makes me wonder if there was any real problem
with my approach to the program--except the closing issue I noted.
Which I solved by putting end in the form1.closing event, everyone gasp now.


CMM wrote:
Show quoteHide quote
> Without really reading the rest of this thread (boring)...
> What you want to do is
> start up in Sub Main not Form1
> Show your form from there SubMain: frm.Show
> Execute the proc from Sub Main:  frm.DoSomething
> Then close it from SubMain: frm.Close
> then probably dispose of it frm.Dispose
>
> I call this "puppeteer-ing a form"... which is OK.... it's really just a
> class.
>
> "cj" <cj@nospam.nospam> wrote in message
> news:O1YDK41JGHA.516@TK2MSFTNGP15.phx.gbl...
>>
>> Armin Zingler wrote:
>>> "cj" <cj@nospam.nospam> schrieb
>>>> I run this program and to exit click the X in the upper right
>>>> corner. But apparently it isn't really ending the program.  If I
>>>> return to VB and make changes then try to rebuild the app it says
>>>> the exe is still in use--I find it is still a process in Task
>>>> Manager.  What do I need to do to make clicking that X actually end
>>>> the program?
>>>>
>>>>
>>>> Public Class Form1
>>>>     Inherits System.Windows.Forms.Form
>>>>
>>>> #Region " Windows Form Designer generated code "
>>>>
>>>>     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
>>>> System.EventArgs) Handles MyBase.Load
>>>>         Me.Show()
>>>>         Application.DoEvents()
>>> This is bad design.
>> Perhaps
>>
>> Don't call Show in Form_Load. Load has been raised
>>> because Show has been executed before. I also don't know why you want to
>>> process all windows messages in the message queue by calling DoEvents.
>>>
>> If I don't call me.show() the program never displays on the screen
>>
>>>>         Dim result As DialogResult
>>>>         result = FolderBrowserDialog1.ShowDialog()
>>>>         If result = DialogResult.Cancel Then
>>>>             End
>>> Don't use End. It is not necessary.
>>>
>> If I don't put end there then if I choose cancel the program will try to
>> work when a directory has not been selected and it gives an error.
>>
>>>>         End If
>>>>
>>>>         ' Create a reference to the current directory.
>>>>         Dim DirInfo As New
>>>> System.IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
>>>>         ' Create an array representing the files in the current
>>>> directory.         Dim FileInfo As System.IO.FileInfo() =
>>>> DirInfo.GetFiles("*.jpg")
>>>>
>>>>         For Each File As System.io.FileInfo In FileInfo
>>>>             Label1.Text = File.FullName
>>>>             PictureBox1.Image = Image.FromFile(File.FullName)
>>>>             Application.DoEvents()
>>>>             System.Threading.Thread.Sleep(2000)
>>>>         Next
>>>>
>>>>         End
>>>>     End Sub
>>>> End Class
>>>
>>> You never exit the loop, thus the program never ends.
>> It's a for each loop.  The loop exits when all the files are displayed.
>>
>>> If you do everything in Form_Load, the load will never be complete.
>> Hu?  the load would be complete when everything in it is done.  And the
>> last thing that is done is ending the program.
>>
>>>
>>> Armin
>>>
>> Why wouldn't clicking the X just abruptly end the program instead of just
>> removing the form and leaving something running?
>
>
Author
2 Feb 2006 11:32 PM
CMM
Wait a minute. I just tried your code (its not boring... it's
interesting).... it works here... are you saying you want to be able to
ABORT the loop when the user closes the form? I see

Here's my elegant fix (note I also avoid short-circuiting with is
problematic and discouraged):

Private cancelLoop As Boolean

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

    Me.Show()
    Application.DoEvents()

    If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
        ' Create a reference to the current directory.
        Dim DirInfo As New
System.IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
        ' Create an array representing the files in the current directory.
        Dim FileInfo As System.IO.FileInfo() = DirInfo.GetFiles("*.jpg")

        For Each File As System.io.FileInfo In FileInfo
            Label1.Text = File.FullName
            PictureBox1.Image = Image.FromFile(File.FullName)
            Application.DoEvents()
            System.Threading.Thread.Sleep(2000)

            If cancelLoop Then
                Exit For
            End If
        Next
    End If

End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

    cancelLoop = True

End Sub


Show quoteHide quote
"cj" <cj@nospam.nospam> wrote in message
news:eL6kvNAKGHA.500@TK2MSFTNGP15.phx.gbl...
>
> I agree boring, if I didn't need to get this info I wouldn't be reading
> it.  What perplexes me is I see you did continue to read because you
> posted at the end of the longest "thread" emanating from my original post.
>
> Your suggestion is interesting however quite different from Armin's code
> which has stuff like application.run(form) in it.  If you're both right,
> and I think you are, then I must conclude there is no "right" way to write
> this program.  Which makes me wonder if there was any real problem with my
> approach to the program--except the closing issue I noted. Which I solved
> by putting end in the form1.closing event, everyone gasp now.
>
>
> CMM wrote:
>> Without really reading the rest of this thread (boring)...
>> What you want to do is
>> start up in Sub Main not Form1
>> Show your form from there SubMain: frm.Show
>> Execute the proc from Sub Main:  frm.DoSomething
>> Then close it from SubMain: frm.Close
>> then probably dispose of it frm.Dispose
>>
>> I call this "puppeteer-ing a form"... which is OK.... it's really just a
>> class.
>>
>> "cj" <cj@nospam.nospam> wrote in message
>> news:O1YDK41JGHA.516@TK2MSFTNGP15.phx.gbl...
>>>
>>> Armin Zingler wrote:
>>>> "cj" <cj@nospam.nospam> schrieb
>>>>> I run this program and to exit click the X in the upper right
>>>>> corner. But apparently it isn't really ending the program.  If I
>>>>> return to VB and make changes then try to rebuild the app it says
>>>>> the exe is still in use--I find it is still a process in Task
>>>>> Manager.  What do I need to do to make clicking that X actually end
>>>>> the program?
>>>>>
>>>>>
>>>>> Public Class Form1
>>>>>     Inherits System.Windows.Forms.Form
>>>>>
>>>>> #Region " Windows Form Designer generated code "
>>>>>
>>>>>     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
>>>>> System.EventArgs) Handles MyBase.Load
>>>>>         Me.Show()
>>>>>         Application.DoEvents()
>>>> This is bad design.
>>> Perhaps
>>>
>>> Don't call Show in Form_Load. Load has been raised
>>>> because Show has been executed before. I also don't know why you want
>>>> to process all windows messages in the message queue by calling
>>>> DoEvents.
>>>>
>>> If I don't call me.show() the program never displays on the screen
>>>
>>>>>         Dim result As DialogResult
>>>>>         result = FolderBrowserDialog1.ShowDialog()
>>>>>         If result = DialogResult.Cancel Then
>>>>>             End
>>>> Don't use End. It is not necessary.
>>>>
>>> If I don't put end there then if I choose cancel the program will try to
>>> work when a directory has not been selected and it gives an error.
>>>
>>>>>         End If
>>>>>
>>>>>         ' Create a reference to the current directory.
>>>>>         Dim DirInfo As New
>>>>> System.IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
>>>>>         ' Create an array representing the files in the current
>>>>> directory.         Dim FileInfo As System.IO.FileInfo() =
>>>>> DirInfo.GetFiles("*.jpg")
>>>>>
>>>>>         For Each File As System.io.FileInfo In FileInfo
>>>>>             Label1.Text = File.FullName
>>>>>             PictureBox1.Image = Image.FromFile(File.FullName)
>>>>>             Application.DoEvents()
>>>>>             System.Threading.Thread.Sleep(2000)
>>>>>         Next
>>>>>
>>>>>         End
>>>>>     End Sub
>>>>> End Class
>>>>
>>>> You never exit the loop, thus the program never ends.
>>> It's a for each loop.  The loop exits when all the files are displayed.
>>>
>>>> If you do everything in Form_Load, the load will never be complete.
>>> Hu?  the load would be complete when everything in it is done.  And the
>>> last thing that is done is ending the program.
>>>
>>>>
>>>> Armin
>>>>
>>> Why wouldn't clicking the X just abruptly end the program instead of
>>> just removing the form and leaving something running?
>>
Author
3 Feb 2006 4:47 PM
cj
I like it.  It fixes my problem w/o radically changing my code.  Hence I
completely understand it.  Although Armin did pretty good.  I think you
need me.close after the for loop to match my original functionality but
who cares.

Now, suppose I put a menu with file/exit on it.  What code would you put
in it?  Perhaps just me.close() since that would make the closing
routine run which stop the loop.  (I first tried this reversed.  Having
the menu item do the cancelloop and me.close and the closing event call
the menu item click but that gave me lots of problems.)  It seems
logical.  Let The closing event could stop all the crap that's running
and tidy up.  All the exit item has to do is start the form closing
which is the exact same thing as clicking the X which so many folks like
to do.  Does that sounds logical to you too?


CMM wrote:
Show quoteHide quote
> Wait a minute. I just tried your code (its not boring... it's
> interesting).... it works here... are you saying you want to be able to
> ABORT the loop when the user closes the form? I see
>
> Here's my elegant fix (note I also avoid short-circuiting with is
> problematic and discouraged):
>
> Private cancelLoop As Boolean
>
> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>
>     Me.Show()
>     Application.DoEvents()
>
>     If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
>         ' Create a reference to the current directory.
>         Dim DirInfo As New
> System.IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
>         ' Create an array representing the files in the current directory.
>         Dim FileInfo As System.IO.FileInfo() = DirInfo.GetFiles("*.jpg")
>
>         For Each File As System.io.FileInfo In FileInfo
>             Label1.Text = File.FullName
>             PictureBox1.Image = Image.FromFile(File.FullName)
>             Application.DoEvents()
>             System.Threading.Thread.Sleep(2000)
>
>             If cancelLoop Then
>                 Exit For
>             End If
>         Next
>     End If
>
> End Sub
>
> Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
> System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
>
>     cancelLoop = True
>
> End Sub
>
>
> "cj" <cj@nospam.nospam> wrote in message
> news:eL6kvNAKGHA.500@TK2MSFTNGP15.phx.gbl...
>> I agree boring, if I didn't need to get this info I wouldn't be reading
>> it.  What perplexes me is I see you did continue to read because you
>> posted at the end of the longest "thread" emanating from my original post.
>>
>> Your suggestion is interesting however quite different from Armin's code
>> which has stuff like application.run(form) in it.  If you're both right,
>> and I think you are, then I must conclude there is no "right" way to write
>> this program.  Which makes me wonder if there was any real problem with my
>> approach to the program--except the closing issue I noted. Which I solved
>> by putting end in the form1.closing event, everyone gasp now.
>>
>>
>> CMM wrote:
>>> Without really reading the rest of this thread (boring)...
>>> What you want to do is
>>> start up in Sub Main not Form1
>>> Show your form from there SubMain: frm.Show
>>> Execute the proc from Sub Main:  frm.DoSomething
>>> Then close it from SubMain: frm.Close
>>> then probably dispose of it frm.Dispose
>>>
>>> I call this "puppeteer-ing a form"... which is OK.... it's really just a
>>> class.
>>>
>>> "cj" <cj@nospam.nospam> wrote in message
>>> news:O1YDK41JGHA.516@TK2MSFTNGP15.phx.gbl...
>>>> Armin Zingler wrote:
>>>>> "cj" <cj@nospam.nospam> schrieb
>>>>>> I run this program and to exit click the X in the upper right
>>>>>> corner. But apparently it isn't really ending the program.  If I
>>>>>> return to VB and make changes then try to rebuild the app it says
>>>>>> the exe is still in use--I find it is still a process in Task
>>>>>> Manager.  What do I need to do to make clicking that X actually end
>>>>>> the program?
>>>>>>
>>>>>>
>>>>>> Public Class Form1
>>>>>>     Inherits System.Windows.Forms.Form
>>>>>>
>>>>>> #Region " Windows Form Designer generated code "
>>>>>>
>>>>>>     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
>>>>>> System.EventArgs) Handles MyBase.Load
>>>>>>         Me.Show()
>>>>>>         Application.DoEvents()
>>>>> This is bad design.
>>>> Perhaps
>>>>
>>>> Don't call Show in Form_Load. Load has been raised
>>>>> because Show has been executed before. I also don't know why you want
>>>>> to process all windows messages in the message queue by calling
>>>>> DoEvents.
>>>>>
>>>> If I don't call me.show() the program never displays on the screen
>>>>
>>>>>>         Dim result As DialogResult
>>>>>>         result = FolderBrowserDialog1.ShowDialog()
>>>>>>         If result = DialogResult.Cancel Then
>>>>>>             End
>>>>> Don't use End. It is not necessary.
>>>>>
>>>> If I don't put end there then if I choose cancel the program will try to
>>>> work when a directory has not been selected and it gives an error.
>>>>
>>>>>>         End If
>>>>>>
>>>>>>         ' Create a reference to the current directory.
>>>>>>         Dim DirInfo As New
>>>>>> System.IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
>>>>>>         ' Create an array representing the files in the current
>>>>>> directory.         Dim FileInfo As System.IO.FileInfo() =
>>>>>> DirInfo.GetFiles("*.jpg")
>>>>>>
>>>>>>         For Each File As System.io.FileInfo In FileInfo
>>>>>>             Label1.Text = File.FullName
>>>>>>             PictureBox1.Image = Image.FromFile(File.FullName)
>>>>>>             Application.DoEvents()
>>>>>>             System.Threading.Thread.Sleep(2000)
>>>>>>         Next
>>>>>>
>>>>>>         End
>>>>>>     End Sub
>>>>>> End Class
>>>>> You never exit the loop, thus the program never ends.
>>>> It's a for each loop.  The loop exits when all the files are displayed.
>>>>
>>>>> If you do everything in Form_Load, the load will never be complete.
>>>> Hu?  the load would be complete when everything in it is done.  And the
>>>> last thing that is done is ending the program.
>>>>
>>>>> Armin
>>>>>
>>>> Why wouldn't clicking the X just abruptly end the program instead of
>>>> just removing the form and leaving something running?
>
Author
3 Feb 2006 8:55 PM
CMM
Yup sounds logical. If Form_Closing sets the flag, there's no need to
reproduce it in the menu. Just do Me.Close in the menu.
Author
1 Feb 2006 5:31 PM
Armin Zingler
Alternative suggestion:

Put a Timer (name=Timer1; interval=2000; enabled=false) on the Form.

   Shared Sub main()

      Dim f As New Form1
      Dim result As DialogResult

      f.Show()

      result = f.FolderBrowserDialog1.ShowDialog()

      If result <> System.Windows.Forms.DialogResult.Cancel Then
         f.Start(f.FolderBrowserDialog1.SelectedPath)
         Application.Run(f)
      End If

   End Sub

   Private f_FileInfos As System.IO.FileInfo()
   Private f_Index As Integer

   Public Sub Start(ByVal Path As String)

      Dim DirInfo As New System.IO.DirectoryInfo(Path)

      f_FileInfos = DirInfo.GetFiles("*.jpg")
      If f_FileInfos.Length > 0 Then Timer1.Start()

   End Sub
   Private Sub Timer1_Tick( _
      ByVal sender As System.Object, ByVal e As System.EventArgs) _
      Handles Timer1.Tick

      Dim File As System.IO.FileInfo

      File = f_FileInfos(f_Index)

      Label1.Text = File.FullName
      PictureBox1.Image = Image.FromFile(File.FullName)

      f_Index += 1

      If f_Index > f_FileInfos.GetUpperBound(0) Then
         Timer1.Stop()
      End If

   End Sub



Armin
Author
1 Feb 2006 6:38 PM
cj
Gee, ah, thanks.  I guess, you studied OOP instead of structured
programming in college.  Ever used COBOL?


Armin Zingler wrote:
Show quoteHide quote
> Alternative suggestion:
>
> Put a Timer (name=Timer1; interval=2000; enabled=false) on the Form.
>
>   Shared Sub main()
>
>      Dim f As New Form1
>      Dim result As DialogResult
>
>      f.Show()
>
>      result = f.FolderBrowserDialog1.ShowDialog()
>
>      If result <> System.Windows.Forms.DialogResult.Cancel Then
>         f.Start(f.FolderBrowserDialog1.SelectedPath)
>         Application.Run(f)
>      End If
>
>   End Sub
>
>   Private f_FileInfos As System.IO.FileInfo()
>   Private f_Index As Integer
>
>   Public Sub Start(ByVal Path As String)
>
>      Dim DirInfo As New System.IO.DirectoryInfo(Path)
>
>      f_FileInfos = DirInfo.GetFiles("*.jpg")
>      If f_FileInfos.Length > 0 Then Timer1.Start()
>
>   End Sub
>   Private Sub Timer1_Tick( _
>      ByVal sender As System.Object, ByVal e As System.EventArgs) _
>      Handles Timer1.Tick
>
>      Dim File As System.IO.FileInfo
>
>      File = f_FileInfos(f_Index)
>
>      Label1.Text = File.FullName
>      PictureBox1.Image = Image.FromFile(File.FullName)
>
>      f_Index += 1
>
>      If f_Index > f_FileInfos.GetUpperBound(0) Then
>         Timer1.Stop()
>      End If
>
>   End Sub
>
>
>
> Armin
Author
1 Feb 2006 6:55 PM
Armin Zingler
"cj" <cj@nospam.nospam> schrieb
> Gee, ah, thanks.  I guess, you studied OOP instead of structured
> programming in college.

First structured programming, later OOP. :)

>  Ever used COBOL?

No.


Armin
Author
1 Feb 2006 7:36 PM
cj
Armin Zingler wrote:
> "cj" <cj@nospam.nospam> schrieb
>> Gee, ah, thanks.  I guess, you studied OOP instead of structured
>> programming in college.
>
> First structured programming, later OOP. :)
>
>>  Ever used COBOL?
>
> No.
>
>
> Armin

You don't know what your missing.  :)
Author
2 Feb 2006 12:56 AM
Dennis
Armin, I admire your patience and persistence!
--
Dennis in Houston


Show quoteHide quote
"Armin Zingler" wrote:

> "cj" <cj@nospam.nospam> schrieb
> > Gee, ah, thanks.  I guess, you studied OOP instead of structured
> > programming in college.
>
> First structured programming, later OOP. :)
>
> >  Ever used COBOL?
>
> No.
>
>
> Armin
>
Author
2 Feb 2006 6:54 AM
Cor Ligthert [MVP]
Dennis,

> Armin, I admire your patience and persistence!
> --
I did not know how to write this, you saved me some thinking time

:-)

Cor