This code use to work fine when I originally coded it in vs 2005 a year or two ago, but now that I had to make a minor changes to it in 2008 it no longer works. The code is a little messy because I've tried dozen of things and nothing works right.
The purpose of this program is to handle the updating of any dll files. The calling program check if it needs to update its file against those contained in a remote directory. If an update is needed it call this program which handles the actual copying of files. Once that is done this calls the original program using any command line parameters that were originally used.
It has worked fine for quite awhile, but recently I had to update something minor (don't really remember what it was). After originally testing it had worked great, but when a program called it this morning it suddenly does not work. The program opens and says it is updating the file before the copy is complete it tries to open the calling program, but fails with no error given and the file is not the updated copy.
Once I added that it called the copying thread from the OnLoadComplete event I created it started working fine in VS2008 as debug or release, but outside the IDE it will not work correctly. Also I found when testing that it was displaying the correct from and to location with a msgbox it pausing for the message box makes it work correctly outside VS. I've tried adding Application.DoEvents to several areas, but that didn't make any difference. Any ideas what I'm missing that I've done wrong?
I have a totally different way of copying files now, but I rather not go through the hassle of updating this program to do it that way.
-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
The purpose of this program is to handle the updating of any dll files. The calling program check if it needs to update its file against those contained in a remote directory. If an update is needed it call this program which handles the actual copying of files. Once that is done this calls the original program using any command line parameters that were originally used.
It has worked fine for quite awhile, but recently I had to update something minor (don't really remember what it was). After originally testing it had worked great, but when a program called it this morning it suddenly does not work. The program opens and says it is updating the file before the copy is complete it tries to open the calling program, but fails with no error given and the file is not the updated copy.
Once I added that it called the copying thread from the OnLoadComplete event I created it started working fine in VS2008 as debug or release, but outside the IDE it will not work correctly. Also I found when testing that it was displaying the correct from and to location with a msgbox it pausing for the message box makes it work correctly outside VS. I've tried adding Application.DoEvents to several areas, but that didn't make any difference. Any ideas what I'm missing that I've done wrong?
Code:
Imports System.IO
Imports System.Collections.Specialized
Imports System.Threading
Public Class main_frm
Dim ProgramLocation As String = ""
Dim cmdLineArgs As New StringCollection
Private Sub main_frm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cmdLine As String
Dim FoundAt As Integer
cmdLine = Microsoft.VisualBasic.Command
If cmdLine <> "" Then
If cmdLine.IndexOf("/") > 0 Then
cmdLineArgs.AddRange(cmdLine.Split("/"))
Else
cmdLineArgs.Add(cmdLine)
End If
If cmdLineArgs.Count > 0 Then
FoundAt = cmdLineArgs(0).LastIndexOf("\")
ProgramLocation = cmdLineArgs(0).Substring(0, FoundAt)
If ProgramLocation.EndsWith("\") <> True Then
ProgramLocation = ProgramLocation & "\"
End If
RaiseEvent OnLoadComplete()
Else
MsgBox("Calling program faild to pass identity.")
End If
Else
MsgBox("Command line empty.")
End If
End Sub
Private Event OnLoadComplete()
Private Sub main_frm_LoadComplete() Handles Me.OnLoadComplete
Dim th As New Thread(AddressOf UpdateDll)
th.Start()
End Sub
Private Sub UpdateDll()
If File.Exists(ProgramLocation & "update.txt") Then
Dim sr As New StreamReader(ProgramLocation & "update.txt")
Dim cLine As New StringCollection
Dim strFile As String
sMessage("Create update list")
Do While sr.EndOfStream <> True
cLine.Add(sr.ReadLine)
Loop
sr.Close()
Dim obj(1) As Object
obj(0) = "max"
obj(1) = cLine.Count
Me.BeginInvoke(New tMinMaxProgress(AddressOf MinMaxProgress), obj)
Me.BeginInvoke(New tVisibleNotVisible(AddressOf VisibleNotVisible))
sMessage("")
Dim sw As New StreamWriter(ProgramLocation & "updates.txt", True)
For Each strFile In cLine
Dim Note As String
Dim ProgramName As String
ProgramName = IO.Path.GetFileName(strFile) 'strFile.Substring(strFile.LastIndexOf("\") + 1, (Len(strFile) - strFile.LastIndexOf("\")) - 1)
Note = "From: " & strFile & vbCrLf & "To: " & ProgramLocation & ProgramName
'If Me.InvokeRequired Then
Me.BeginInvoke(New tUpdateFNLabel(AddressOf UpdateFNLabel), Note)
'My.Computer.FileSystem.CopyFile(strFile, ProgramLocation & ProgramName, True)
System.IO.File.Copy(strFile, ProgramLocation & ProgramName, True)
Me.BeginInvoke(New tUpdateProgress(AddressOf UpdateProgress))
'Else
'Filename_lbl.Text = strFile
'My.Computer.FileSystem.CopyFile(strFile, ProgramLocation & ProgramName, True)
'UpdateProgress_pb.Value = UpdateProgress_pb.Value + 1
'End If
sw.WriteLine("[" & Date.Now & "] " & strFile)
Next
sw.Close()
sMessage("Updates complete")
'File.Delete(ProgramLocation & "update.txt")
If Me.InvokeRequired Then
Me.BeginInvoke(New tReopenCallingProgram(AddressOf ReOpenCallingProgram))
'Else
' ReOpenCallingProgram()
End If
Else
MsgBox("Update file not found at " & ProgramLocation & ". Unable to update.")
End If
End Sub
Private Sub sMessage(ByVal MessageText As String)
If Me.InvokeRequired Then
Me.BeginInvoke(New tSendText(AddressOf SendText), MessageText)
Else
SendText(MessageText)
End If
End Sub
Private Delegate Sub tSendText(ByVal Text As String)
Private Sub SendText(ByVal Text As String)
TSSL1.Text = Text
End Sub
Private Delegate Sub tVisibleNotVisible()
Private Sub VisibleNotVisible()
UpdateProgress_pb.Visible = Not UpdateProgress_pb.Visible
End Sub
'Needed to set min/max for progress bar from a thread
Private Delegate Sub tMinMaxProgress(ByVal MinMax As String, ByVal value As Integer)
Private Sub MinMaxProgress(ByVal MinMax As String, ByVal value As Integer)
If MinMax.ToLower = "min" Then
UpdateProgress_pb.Minimum = value
ElseIf MinMax.ToLower = "max" Then
UpdateProgress_pb.Maximum = value
End If
End Sub
'Needed to update the progress bar from a thread
Private Delegate Sub tUpdateProgress()
Private Sub UpdateProgress()
UpdateProgress_pb.Value = UpdateProgress_pb.Value + 1
End Sub
'Needed to update the Filename lable from a thread
Private Delegate Sub tUpdateFNLabel(ByVal Text As String)
Private Sub UpdateFNLabel(ByVal Text As String)
Filename_lbl.Text = Text
End Sub
'Needed to ReOpen the program that started the update from a thread
Private Delegate Sub tReopenCallingProgram()
Private Sub ReOpenCallingProgram()
If cmdLineArgs.Count > 1 Then
Dim cmdStr As String = ""
Dim i As Integer
For i = 1 To cmdLineArgs.Count - 1
cmdStr = cmdStr & "/" & cmdLineArgs(i)
Next
System.Diagnostics.Process.Start(cmdLineArgs(0).Trim, cmdStr)
Else
System.Diagnostics.Process.Start(cmdLineArgs(0).Trim)
End If
Application.Exit()
End Sub
End Class
-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!