Is it possible to put a command to kill the process after it has finished. i.e after clicking a command button that writes to a file. when it has finished can I kill that somehow -
Thanks
R
Printable View
Is it possible to put a command to kill the process after it has finished. i.e after clicking a command button that writes to a file. when it has finished can I kill that somehow -
Thanks
R
You can't kill a process that has already ended.
does this make sense then. I click a button that updates my file but it renders all my other command buttons useless and I cant understand why.
thanks
R
Try using a DoEvents if you have some loop.
It is when this is happening, I will try and have a look for info on what you just suggested.
Thanks
R
VB Code:
Private Sub Command2_Click() Dim f1 As Integer, xmlpath As String, songpath As String, mp3path As String, f2 As Integer, i As Integer, pos As Integer Dim Artist As String, SongName As String, artname As String, fname As String, MyPath As String Dim mysong As String Dim mytag As Boolean mp3path = "C:\Program Files\mgamerz\XML PLAYER\my_mp3s\" xmlpath = "C:\Program Files\mgamerz\XML PLAYER\" SongName = Dir(mp3path & "\*.mp3") fname = mp3path & SongName With Form4 While Not Len(SongName) = 0 ' Artist = GetMP3Tag(fname) If Not i = 0 Then Load .Label1(i): Load .Text1(i) .Label1(i).Top = .Label1(i - 1).Top + 350 .Text1(i).Top = .Text1(i - 1).Top + 350 .Label1(i).Visible = True: .Text1(i).Visible = True End If .Label1(i) = SongName mytag = GetMP3Tag(fname) .Text1(i) = RTrim(MP3Info.sArtist) SongName = Dir fname = mp3path & SongName i = i + 1 Wend .Height = .Text1(.Text1.Count - 1).Top + 1600 .TAG = mp3path MDIForm1.Caption = "Close This Form to Update XML Playlist" .Show ' .WindowState = 2 End With TAG = "wait" While TAG = "wait" DoEvents Wend i = 1 f2 = FreeFile Open mp3path & "mp3.txt" For Output As #f2 f1 = FreeFile Open xmlpath & "audiolist.xml" For Output As #f1 Print #f1, "<?xml version=""1.0""?>" Print #f1, "<songs>" SongName = Dir(mp3path & "\*.mp3") i = 1 While Not Len(SongName) = 0 ' if you can return the name of the artist you put the code in here mytag = GetMP3Tag(mp3path & SongName) 'pos = InStr(mytag, "\") mytag = Replace(mytag, "\", "/") Do While InStr(mytag, " ") mytag = Replace(mytag, " ", "") Loop Artist = RTrim(MP3Info.sArtist) If Len(Artist) = 0 Then Artist = "UNKOWN" MyPath = Right(mp3path, (Len(mp3path) - Len(xmlpath))) & " " & SongName MyPath = Replace(MyPath, "\", "/") MyPath = Replace(MyPath, " ", "", 1, 1) mysong = Left$(SongName, Len(SongName) - 4) songpath = "<song path=""" & MyPath & """ artist=""" & Artist & """ title=""" & mysong & """/> " Print #f1, songpath Print #f2, SongName Sleep 100 SongName = Dir Wend Print #f1, "</songs>" Close End Sub
I would put one DoEvents into each While/Wend loop.
not too sure where to add it or how to do that - would it just need to be in the update XML code?
Thanks
R
doevents let your computer do other things while its doing your code, so it wont freeze. Put it rigth before the wend, remember its one word-doevents
I'll need to do some reading into that thanks
will I be able to add the do events to the exixting code I posted or will it all need changing,
Thanks
R
I'll format your code for you, so you can see where it would go :wave:
VB Code:
Private Sub Command2_Click() Dim f1 As Integer, xmlpath As String, songpath As String, mp3path As String, f2 As Integer, i As Integer, pos As Integer Dim Artist As String, SongName As String, artname As String, fname As String, MyPath As String Dim mysong As String Dim mytag As Boolean mp3path = "C:\Program Files\mgamerz\XML PLAYER\my_mp3s\" xmlpath = "C:\Program Files\mgamerz\XML PLAYER\" SongName = Dir(mp3path & "\*.mp3") fname = mp3path & SongName With Form4 While Not Len(SongName) = 0 If Not i = 0 Then Load .Label1(i): Load .Text1(i) .Label1(i).Top = .Label1(i - 1).Top + 350 .Text1(i).Top = .Text1(i - 1).Top + 350 .Label1(i).Visible = True: .Text1(i).Visible = True End If .Label1(i) = SongName mytag = GetMP3Tag(fname) .Text1(i) = RTrim(MP3Info.sArtist) SongName = Dir fname = mp3path & SongName i = i + 1 Wend .Height = .Text1(.Text1.Count - 1).Top + 1600 .TAG = mp3path MDIForm1.Caption = "Close This Form to Update XML Playlist" .Show End With f2 = FreeFile Open mp3path & "mp3.txt" For Output As #f2 f1 = FreeFile Open xmlpath & "audiolist.xml" For Output As #f1 Print #f1, "<?xml version=""1.0""?>" Print #f1, "<songs>" SongName = Dir(mp3path & "\*.mp3") i = 1 While Not Len(SongName) = 0 [B]DoEvents[/B] ' <---------------------------- HERE \\ mytag = GetMP3Tag(mp3path & SongName) mytag = Replace(mytag, "\", "/") Do While InStr(mytag, " ") mytag = Replace(mytag, " ", "") Loop Artist = RTrim(MP3Info.sArtist) If Len(Artist) = 0 Then Artist = "UNKOWN" MyPath = Right(mp3path, (Len(mp3path) - Len(xmlpath))) & " " & SongName MyPath = Replace(MyPath, "\", "/") MyPath = Replace(MyPath, " ", "", 1, 1) mysong = Left$(SongName, Len(SongName) - 4) songpath = "<song path=""" & MyPath & """ artist=""" & Artist & """ title=""" & mysong & """/> " Print #f1, songpath Print #f2, SongName Sleep 100 SongName = Dir Wend Print #f1, "</songs>" Close #f1 close #f2 End Sub
Thanks for that but Im still unsure of the next step. I have been studying my books but I just cant seem to fathom it out. I thought this would be the process that needs to end before anything else can be carried out.
Sorry to be a nuisance as usual
Rob
That would suck. Say you are playing music in a game. until the music finishes you cannot play the game? Yeah, it would make you mute your game more often. It is my understanding that code runs from top to bottom in every event, but multiple events can be running at the same time.
What do you want to do while the computer is saving files? You could set enabled = false for all of the butons so that they cannot be clicked until the save finishes.
Explain what you want to be able to do while the computer is saving the files?
what I want is that when the program has finished writing the XML file then the user can add more files or delete files but currently after the button is clicked to update the XML file the other buttons stop functioning.
Thanks
Rob
would it be easier if I made it into 2 seperate programs?
If set enabled = false for all of the butons would that allow them to work when the xml file has been processed?
thanks
R
You turn them back on when the code has finished. You could also search for LockWindowUpdate, which prevents doing anything until Unlock Window
would I put the code to disable the buttons at the beginning of the code and then enable them at the end?
Yes. That would prevent them doing anthing else while the code is executing.
:wave:
thanks for that, I havent goot VB installed on this machine because Ive been trying to find it in books but am I right in thinking that they wiil need to be disabled before this line
VB Code:
Dim f1 As Integer, xmlpath As String, songpath As String, mp3path As String, f2 As Integer, i As Integer, pos As Integer
and then enable them after this line
VB Code:
Print #f1, "</songs>" Close #f1 close #f2
Is it only because the code hasn't finished executing that the buttons wont work currently
You will like this. I have just been changing it to suit you. Also saved it for next time, so I don't have to search the forums.
VB Code:
Option Explicit Private Sub Form_Load() Dim ctlAll As Control For Each ctlAll In Me.Controls If TypeOf ctlAll Is CommandButton Then ctlAll.Enabled = False End If Next End Sub
Turns of ALL buttons in the form. Just change it to TRUE to turn them back on.
Thanks very much as I say I cannot try it until later because I have no access to VB but I will keep you posted. It is really appreciated.
Rob
Just a quickie when I have added that to my code where will I need to add the code to enable them - Thanks very much again
R
Same loop from above, but change it to this:
VB Code:
ctlAll.Enabled = True
if the first bit of code goes on the form load where will the second go. Sorry I just want to get it clear because I want to try and not bother you later because I think Ive taken up enough of your time.
Thanks
R
Just like you said in Post #21. Disable them at the top and re-enable them at the bottom. Substitue the correct form name for your app. I used Form1 in my sample, but you probably have it named something else.
Thanks very much
Right, I have successfully added the code and got the buttons to be disabled when the file is bieng written but after the file has been written the buttons still cease to work. If I wanted to add or delete files I need to close the app and restart it.
Any other suggestions that could be caussing this.
Thanks
R
did you run the code to enable them?
put a breakpoint in to make sure it gets there.
are the buttons enabled?
the buttons disable when the filea are bieng added and then they are enabled again Here is where I added them to my code
VB Code:
Private Sub Command2_Click() Dim f1 As Integer, xmlpath As String, songpath As String, mp3path As String, f2 As Integer, i As Integer, pos As Integer Dim Artist As String, SongName As String, artname As String, fname As String, MyPath As String Dim mysong As String Dim mytag As Boolean Dim ctlAll As Control For Each ctlAll In Me.Controls If TypeOf ctlAll Is CommandButton Then ctlAll.Enabled = False End If Next mp3path = "C:\Program Files\mgamerz\XML PLAYER\my_mp3s\" xmlpath = "C:\Program Files\mgamerz\XML PLAYER\" SongName = Dir(mp3path & "\*.mp3") fname = mp3path & SongName With Form4 While Not Len(SongName) = 0 ' Artist = GetMP3Tag(fname) If Not i = 0 Then Load .Label1(i): Load .Text1(i) .Label1(i).Top = .Label1(i - 1).Top + 350 .Text1(i).Top = .Text1(i - 1).Top + 350 .Label1(i).Visible = True: .Text1(i).Visible = True End If .Label1(i) = SongName mytag = GetMP3Tag(fname) .Text1(i) = RTrim(MP3Info.sArtist) SongName = Dir fname = mp3path & SongName i = i + 1 Wend .Height = .Text1(.Text1.Count - 1).Top + 1600 .TAG = mp3path MDIForm1.Caption = "Close This Form to Update XML Playlist" .Show ' .WindowState = 2 End With TAG = "wait" While TAG = "wait" DoEvents Wend i = 1 f2 = FreeFile Open mp3path & "mp3.txt" For Output As #f2 f1 = FreeFile Open xmlpath & "audiolist.xml" For Output As #f1 Print #f1, "<?xml version=""1.0""?>" Print #f1, "<songs>" SongName = Dir(mp3path & "\*.mp3") i = 1 While Not Len(SongName) = 0 ' if you can return the name of the artist you put the code in here mytag = GetMP3Tag(mp3path & SongName) 'pos = InStr(mytag, "\") mytag = Replace(mytag, "\", "/") Do While InStr(mytag, " ") mytag = Replace(mytag, " ", "") Loop Artist = RTrim(MP3Info.sArtist) If Len(Artist) = 0 Then Artist = "UNKOWN" MyPath = Right(mp3path, (Len(mp3path) - Len(xmlpath))) & " " & SongName MyPath = Replace(MyPath, "\", "/") MyPath = Replace(MyPath, " ", "", 1, 1) mysong = Left$(SongName, Len(SongName) - 4) songpath = "<song path=""" & MyPath & """ artist=""" & Artist & """ title=""" & mysong & """/> " Print #f1, songpath Print #f2, SongName Sleep 100 SongName = Dir Wend Print #f1, "</songs>" For Each ctlAll In Me.Controls If TypeOf ctlAll Is CommandButton Then ctlAll.Enabled = True End If Next Close End Sub
CLOSE should be Close #F1
what happened to the formatted code that I made :(
So does it work correctly? That looks right. :wave:
Hi I have used the formatted code I just didn't want to mess it up which is usually what I normally do. I have made the changes you suggested. as below but I add a number of songs, Add some more and then the buttons just will not do anything. It allows me to add songs once or twice but then they cease to work.
Thanks
R
VB Code:
Private Sub Command2_Click() Dim f1 As Integer, xmlpath As String, songpath As String, mp3path As String, f2 As Integer, i As Integer, pos As Integer Dim Artist As String, SongName As String, artname As String, fname As String, MyPath As String Dim mysong As String Dim mytag As Boolean Dim ctlAll As Control For Each ctlAll In Me.Controls If TypeOf ctlAll Is CommandButton Then ctlAll.Enabled = False End If Next mp3path = "C:\Program Files\mgamerz\XML PLAYER\my_mp3s\" xmlpath = "C:\Program Files\mgamerz\XML PLAYER\" SongName = Dir(mp3path & "\*.mp3") fname = mp3path & SongName With Form4 While Not Len(SongName) = 0 If Not i = 0 Then Load .Label1(i): Load .Text1(i) .Label1(i).Top = .Label1(i - 1).Top + 350 .Text1(i).Top = .Text1(i - 1).Top + 350 .Label1(i).Visible = True: .Text1(i).Visible = True End If .Label1(i) = SongName mytag = GetMP3Tag(fname) .Text1(i) = RTrim(MP3Info.sArtist) SongName = Dir fname = mp3path & SongName i = i + 1 Wend .Height = .Text1(.Text1.Count - 1).Top + 1600 .TAG = mp3path MDIForm1.Caption = "Close This Form to Update XML Playlist" .Show End With f2 = FreeFile Open mp3path & "mp3.txt" For Output As #f2 f1 = FreeFile Open xmlpath & "audiolist.xml" For Output As #f1 Print #f1, "<?xml version=""1.0""?>" Print #f1, "<songs>" SongName = Dir(mp3path & "\*.mp3") i = 1 While Not Len(SongName) = 0 DoEvents ' <---------------------------- HERE \\ mytag = GetMP3Tag(mp3path & SongName) mytag = Replace(mytag, "\", "/") Do While InStr(mytag, " ") mytag = Replace(mytag, " ", "") Loop Artist = RTrim(MP3Info.sArtist) If Len(Artist) = 0 Then Artist = "UNKOWN" MyPath = Right(mp3path, (Len(mp3path) - Len(xmlpath))) & " " & SongName MyPath = Replace(MyPath, "\", "/") MyPath = Replace(MyPath, " ", "", 1, 1) mysong = Left$(SongName, Len(SongName) - 4) songpath = "<song path=""" & MyPath & """ artist=""" & Artist & """ title=""" & mysong & """/> " Print #f1, songpath Print #f2, SongName Sleep 100 SongName = Dir Wend Print #f1, "</songs>" For Each ctlAll In Me.Controls If TypeOf ctlAll Is CommandButton Then ctlAll.Enabled = True End If Next Close #f1 Close #f2 End Sub
I'm not sure. Add some Debug.Print statements to tell you where the program is executing "Reading Files" "Writing XML" so we can figure out where it is messing up. You don't have any other timers or anything that could be interrupting the code, do you? What about Error Traps? Do you have On Error Goto 0 to turn off error traps?
Ive never done that before is it difficult to do?
Just place a few lines in your code, like
VB Code:
On Error Goto 0 ' At the top of the module somewhere debug.print "Reading XML file" - - - debug.print "Writing file#1" - - - debug.print "Writing file#2" - - -
As the program is running, it will print the line it runs accross in the debug window of your ide (and not do anything when it's compiled)
Use the on error goto 0 to make sure that any error is not sending the code somewhere else while the buttons are disabled. You could also try not disabling the buttons and seeing if the buttons don't work after a few times through.
I cannot find any errors. Is it possible that you could have a look through it when you get time if I attach it as a zip file here? I may be missing something obvoius.
Thanks
R
It wouldnot run on my computer without all of the necessary files. If you have any error traps that aren't set to 0, then any error in your code would go to that error trap. if the buttons were disabled at the time, that would explain them not working. I don't think I could find anything by just looking through the code. You have to find an error, and determine where it is occurring by stepping through it, or printing out messages, or whatever you can do to help you.
I've done a lot of this code, and my code usually works many times as long as it works the first time.
Good Luck!
Thanks David for all your help throughout this, I think I have found the problem it's either the length of the file names or the amount of files I have been trying to add. I have managed to add 93 file so far. I have added them in 10's and they all have short names. So maybe thats what it was all along. I dont know if that makes sense but again thanks very much for all your help.
Cheers
?Rob
I think someone said that FSO had a limited number of files that it allowed. Now that you say that it rang a bell. Look for FSO.Files or something like that.
The buffer is too small for that many files. :wave: