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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code stopped working correctly after changed in vs 2008

Status
Not open for further replies.

Sorwen

Technical User
Nov 30, 2002
1,641
US
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?

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 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!
 

Try putting 'system.threading.thread.sleep 1000' in the location where you have the message boxes that you said the pause makes it work.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
I tired it outside, but didn't know how to do it inside the thread itself. I'll give that a try. Like you said it will likely work since the msgbox did. Still, I wonder why it doesn't work as is on normal, but does in the IDE. It is like the thread is suddenly trying to run the copy on the main thread rather than the one it is in. I didn't think to mention that the program "freeze up" in the same manner it would if it was doing all its processing on the main thread.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Thanks! It doesn't work as it should, but it does copy the files now. So at least I can get everything that uses it back to working correctly. Still, I'll have to play around with it until it works as it should. Now that I think about it the last time I had something strange like this it was the messages I was sending to the form that was the problem. If I remember right I had to event all of that. Guess I'll give that a try and see what happens.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top