Results 1 to 3 of 3

Thread: Threading?

  1. #1

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Threading?

    Hi to all wonderful people on VBForume, I have this problem in my project and hoping some one can help me with this. Firs thing first, the project is a backup application which transfers files to the server using Sftp component. I added "FileSystemWatcher" to the project to monitor the files. If the user modifies files then the files are added to the backup list for future backup. I also have BackgroundWorker which downloads files from the server.

    Now the problem: the problem is that when the download is running the files are transferred to a newly created folder and this is triggering the "FileSystemWatcher" events and they are kind of confliction each other. I can notice it because the "ProgressBar" control isn’t sowing the progress smoothly but it stops and continue. There are no errors and eventually the files are being transferred successfully.

    Now the question: why is this happening? I mean they are two deferent threads. Why the progress bare is being affected? I am setting its value in the "ProgressChanged" event so ther is no chross threading problems. Finally, what should I do to solve this? Thanks for reading and for any advice!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Threading?

    We'd need to see some code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Threading?

    Hi Jmc, after I posted it I realize that the files that are downloading should not be added to the list at all. But the user may modify some file while he is downloading from the server so the problem will be there.
    Now the code is big so I will post some sections of it. If you want to see some sections of it then let me know.

    Edit* Oh almost forget! The ProgressBar’s Style property is set to Marquee so I am not setting any property of it.

    I call it lilek this
    vb Code:
    1. Me.downloadFilesWorker.RunWorkerAsync(arg)
    the "arg" is just an object that contains the file names to be downloaded.

    This is the backgroudworker events:
    vb Code:
    1. Private Sub downloadFilesWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles downloadFilesWorker.DoWork
    2.         Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
    3.         Me.ServerDownloadFiles(worker, e)
    4.     End Sub
    5.     Private Sub downloadFilesWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles downloadFilesWorker.ProgressChanged
    6.         Me.FilesRemainNumStatusLabel.Text = CStr(e.ProgressPercentage)
    7.     End Sub
    8.  
    9.     Private Sub downloadFilesWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles downloadFilesWorker.RunWorkerCompleted
    10.         Me.StatusProgressBar.Visible = False
    11.         Me.FilesRemainStatusLabel.Visible = False
    12.         Me.FilesRemainNumStatusLabel.Visible = False
    13.         Me.ClientUploadFilesButton.Enabled = True
    14.         Me.ServerDownloadButton.Enabled = True
    15.         Me.ServerDeleteMenuItem.Enabled = True
    16.         Me.ServerCancelButton.Enabled = False
    17.         Me.StatusLabel.Text = Me.client.State.ToString
    18.         If e.Error IsNot Nothing Then
    19.             MessageBox.Show(e.Error.Message, "Error", _
    20.             MessageBoxButtons.OK, MessageBoxIcon.Error)
    21.         ElseIf e.Cancelled Then
    22.             MessageBox.Show("The process has been cancelled!", _
    23.             "Cancelled", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    24.         ElseIf CInt(e.Result) = 0 Then
    25.             MessageBox.Show("File restore completed successfully!", _
    26.             "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information)
    27.         ElseIf CInt(e.Result) > 0 Then
    28.             MessageBox.Show("File restore was unsuccessful!" & Environment.NewLine _
    29.             & CInt(e.Result) & " file(s) did not restore successfully.", _
    30.             "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    31.         End If
    32.         If Me.ClosingPending Then
    33.             Application.Exit()
    34.         End If
    35.     End Sub
    and this is the fileSystemWathcher event
    vb Code:
    1. Private Sub ClientFileSystemWatcher_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles ClientFileSystemWatcher.Created
    2.         Dim dInfo As New IO.DirectoryInfo(e.FullPath)
    3.         Dim fInfo As New IO.FileInfo(e.FullPath)
    4.         If dInfo.Exists Then
    5.             Dim tvNode As TreeNode
    6.             If My.Settings.CheckedFolders.Contains(dInfo.Parent.FullName) Then
    7.                 My.Settings.CheckedFolders.Add(e.FullPath)
    8.             End If
    9.             tvNode = Node.GetNode(Me.ClientTreeView.Nodes, dInfo.Parent.FullName)
    10.             If tvNode IsNot Nothing AndAlso Not tvNode.FirstNode.Text = String.Empty Then
    11.                 tvNode.Nodes.Add(e.FullPath, dInfo.Name).Checked = tvNode.Checked
    12.             End If
    13.         ElseIf fInfo.Exists Then
    14.             If My.Settings.CheckedFolders.Contains(fInfo.DirectoryName) Then
    15.                 If Not My.Settings.CheckedFiles.Contains(e.FullPath) Then
    16.                     My.Settings.CheckedFiles.Add(e.FullPath)
    17.                     My.Settings.ModifiedFiles.Add(e.FullPath)
    18.                 End If
    19.             End If
    20.         End If
    21.         If Me.ClientTreeView.SelectedNode IsNot Nothing Then
    22.             Me.ClientLoadListView(Me.ClientTreeView.SelectedNode.Name)
    23.         End If
    24.         Me.BackupFilesNumStatusLabel.Text = CStr(My.Settings.ModifiedFiles.Count)
    25.         Me.ClientTotalNumStatusLabel.Text = CStr(My.Settings.CheckedFiles.Count)
    26.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width