Passing data transfer data continuously until file is dragged to window using WPF

I created a project to transfer a file from client to server. I have transferred a file and got the files transferred like the filename (something.avi) and the percentage (10%) of the file transferred as shown below. Whenever I transfer a file, I use below event handler to get information about the file transferred details.

private static void SessionFileTransferProgress(object sender, FileTransferProgressEventArgs e)
{
    // New line for every new file
    if ((_lastFileName != null) && (_lastFileName != e.FileName))
    {
        Console.WriteLine();
    }

    // Print transfer progress
    Console.Write("\r{0} ({1:P0})", e.FileName, e.FileProgress);

    // Remember a name of the last file reported
    _lastFileName = e.FileName;
}
private static string _lastFileName;

      

I need to bind the passed data in a window. I made a binding on file transfer. But I need to link every second file passed in the window using WPF. Because I need to show the progress of the file transfer.

+2


source to share


3 answers


The WinSCP.NET Session.FileTransferProgress

event fires continuously.

So, all you have to do is update your control in your event handler.



When the event fires on a background thread, you need to use Invoke

.

See Updating the GUI (WPF) using a different thread .

+2


source


I found a solution using @Martin Prikryl .. Please find below code

progressBar.Dispatcher.Invoke(() => progressBar.Value = (int)(e.FileProgress * 100), DispatcherPriority.Background);

      



This is for a progress bar moving as the trasnfer file passes. I will post as soon as the progress of the display as a percentage is made.

progressBar is the name of the Xaml element in wpf.

+2


source


I found a code for the progress of transferring a display file with a percentage. Please find below Xaml and C # code for wpf window.

Xaml to display percentage in window using wpf.

<TextBlock x:Name="percentage" Text=""  Height="27" Width="50" FontSize="20"/>

      

C # code to bind file transfer progress in percent.

this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
{
   this.percentage.Text = ((e.FileProgress * 100).ToString() + "%");
}));

      

+1


source







All Articles