Program not ending???

C

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
 
A

Armin Zingler

cj said:
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.
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
 
A

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
 
C

cj

Armin said:
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
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.
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.

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

cj

Gee, ah, thanks. I guess, you studied OOP instead of structured
programming in college. Ever used COBOL?
 
A

Armin Zingler

cj said:
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
?
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)
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
 
C

cj

Armin said:
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.
 
A

Armin Zingler

cj said:
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
 
C

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.
 
A

Armin Zingler

cj said:
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
 
C

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.
 
A

Armin Zingler

cj said:
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
 
C

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.
 
C

cj

does f.Start(f.FolderBrowserDialog1.SelectedPath)
call Public Sub Start(ByVal Path As String)
? must i reckon
 
C

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.
 
A

Armin Zingler

cj said:
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
 
C

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.
 
A

Armin Zingler

cj said:
does f.Start(f.FolderBrowserDialog1.SelectedPath)
call Public Sub Start(ByVal Path As String)
? must i reckon


Yes, it calls sub start.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top