Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB Exe

Status
Not open for further replies.

akutty

IS-IT--Management
Jul 7, 2004
31
0
0
GB
I have written a vb exe which checks whether a text file has changed evey 10 minutes. The application is working fine. Only problem is when a user shuts down the machine it gives program not responding End Task, user has to do a end task everytime. How to avoid this? need to ensure a smooth close of the program
 
Without seeing the code it is difficult to know why it is hanging up.

However doing some of these may help.
- Use DoEvents in any big loops, this should mean that when Windows tries to kill the app the command will get acted upon and it should shut down
- Try to make sure that all forms are closed when the main form is. e.g.
Code:
Private Sub Form_Unload(Cancel As Integer) 
    Dim AForm As Form
    For Each AForm In Forms
        Unload AForm
    Next
End Sub
- Use an End Statement to kill off the app (not seen as good programming practice) e.g.
Code:
Private Sub Form_Unload(Cancel As Integer) 
    Dim AForm As Form
    For Each AForm In Forms
        Unload AForm
    Next
    [b]End[/b]
End Sub

HTH

Dave
 
Hi Dave, below is the attached code


Private Sub Form_Load()

Readtextfile ' Call Function to Read the text files and Display the messages
Form1.Visible = False


End Sub

Function Readtextfile()

Dim fsoAny
Dim txtfilemessage
Dim txtfilecounter
Dim txtfilehtml
Dim sRecordmessage As String
Dim sRecordcounter As String
Dim Path1 As String
Dim Path2 As String
Dim htmltext
Path1 = "C:\Techfol\Messagefile.txt"
Path2 = "C:\Techfol\Counterfile.txt"
Path3 = "C:\Techfol\message.htm"
Const ForReading = 1, ForWriting = 2, ForAppending = 3
i = 0
Do While i <> 1 ' Putting in a infinite Loop
htmltext = ""
htmltext = "<table><tr><td><img src =insideGElogo_37.gif></td><td><img src =eef.gif></td><td><b><font face=Arial size=4 color=#C0C0C0>- EEF Technical Services</font></b></td></tr></table><p><p><p>"
Set fsoAny = CreateObject("Scripting.FileSystemObject")
Set txtfilemessage = fsoAny.OpenTextFile(Path1, ForReading, False)
Set txtfilecounter = fsoAny.OpenTextFile(Path2, ForReading, False)

If Not txtfilemessage.AtEndOfStream Then
sRecordmessage = txtfilemessage.ReadLine()
Else
sRecordmessage = ""
End If

If Not txtfilecounter.AtEndOfStream Then
sRecordcounter = txtfilecounter.ReadLine()
Else
sRecordcounter = ""
End If

If sRecordmessage <> sRecordcounter Then

txtfilecounter.Close
Set txtfilecounter = fsoAny.OpenTextFile(Path2, ForWriting, False)
txtfilecounter.WriteLine (sRecordmessage)

Set txtfilehtml = fsoAny.OpenTextFile(Path3, ForWriting, False)
htmltext = htmltext & "<marquee>" & sRecordmessage & "</marquee>"
txtfilehtml.WriteLine (htmltext)
txtfilehtml.Close

test = Shell("""C:\Program Files\Internet Explorer\iexplore.exe""c:\techfol\message.htm", 5)

End If

txtfilemessage.Close
txtfilecounter.Close
sSleep (10000)
Loop
End Function
 
Digsy is correct. You program is basically ignoring windows telling it to close.

Rather than using a loop the way you are, you would be better using a timer. Since you are checking every 10 minutes the standard vb timer should be fine.

The way you are doing it now will waste a lot of resources on the machine it is running on.

In this case the doevents will not help either as you are telling the app to sleep for 10 minutes.

regards

Matt

If you can keep your head while those around you are losing theirs, you obviously haven't grasped the seriousness of the situation
 
The problem is that you program is doing the sleep in one lump.

You could maybe try changing it so that it sleeps for lots of smaller times in a loop and put a DoEvents in with it to allow the program to respond to Windows trying to close it.

e.g.
Add a new integer varaible Counter and then instead of
sSleep(10000)
try
For Counter = 1 to 1000
sSleep(10)
DoEvents
Next
or something similar


Dave
 
>waste a lot of resources on the machine it is running on

Oddly, even though it is a tight loop, it won't, thanks to the use of Sleep
 
Also

You may need to experiment with the maximum value of Counter and the amount of time it sleeps for until you get it right.
 
but sleep will make it ignore the shutdown signal, wont it?



If you can keep your head while those around you are losing theirs, you obviously haven't grasped the seriousness of the situation
 
> but sleep will make it ignore the shutdown signal, wont it?

It will, which is why I have suggested he makes it sleeps for shorter periods within a loopp and does a DoEvents after each shorter sleep
 
Sure. Not arguing with the general consensus on the cause of the problem; just correcting an assumption.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top