I'm trying transfer Image file
for each 3 seconds from source to destination directory, when I click on the "Start" button. And "Stop" button is for stopping the file transfer.
If I use the Start button for the third time, the event is not firing; so, the files are not transferring.
The progress I've done is in the below:
Event is not firing
// It not works!How can i fix this? Thank you in advance.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Threading;
namespace GridTest
{
/// <summary>
/// Interaction logic for Window3.xaml
/// </summary>
public partial class Window3 : Window
{
DispatcherTimer dispatcherTimer = new DispatcherTimer();
private static List<string> SourceFiles = new List<string>();
private static readonly string SourceDir = @"C:\TestFiles\Images\";
private static readonly string DestinationDir = @"C:\Files\Images\3_5x5\";
private static int TransferCount = 0;
public Window3()
{
InitializeComponent();
this.Loaded += Window3_Loaded;
}
void Window3_Loaded(object sender, RoutedEventArgs e)
{
dispatcherTimer.Interval = new TimeSpan(0, 0, 3);
dispatcherTimer.Tick += dt_Tick;
}
void dt_Tick(object sender, EventArgs e)
{
if (TransferCount < SourceFiles.Count)
{
var fileName = System.IO.Path.GetFileName(SourceFiles[TransferCount]);
var destFile = System.IO.Path.Combine(DestinationDir, fileName);
System.IO.File.Copy(SourceFiles[TransferCount], destFile,true);
System.IO.File.Delete(SourceFiles[TransferCount]);
Console.WriteLine(string.Format("Total Files: {0} Number of files transferred: {1}", SourceFiles.Count, TransferCount + 1));
TransferCount += 1;
}
else
{
Console.WriteLine(string.Format("Total number of files transferred: {0}. Transfer Completed", TransferCount + 1));
(sender as DispatcherTimer).Stop();
(sender as DispatcherTimer).Tick -= dt_Tick;
}
}
private void Start_Click(object sender, RoutedEventArgs e)
{
if (!System.IO.Directory.Exists(DestinationDir))
{
System.IO.Directory.CreateDirectory(DestinationDir);
}
if (System.IO.Directory.Exists(SourceDir))
{
SourceFiles = Directory.GetFiles(SourceDir).ToList();
}
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
dispatcherTimer.Stop();
}
}
}
If you call Stop, your timer event should not fire until you set IsEnabled property to true.
So I suggest to do a bit of refactoring
void Window3_Loaded(object sender, RoutedEventArgs e)
{
LoadSourceFiles();
StartTimer();
}
void StartTimer()
{
dispatcherTimer.Interval = new TimeSpan(0, 0, 3);
dispatcherTimer.Tick += dt_Tick;
dispatcherTimer.IsEnabled = true;
}
void LoadSourceFiles()
{
if (!System.IO.Directory.Exists(DestinationDir))
{
System.IO.Directory.CreateDirectory(DestinationDir);
}
if (System.IO.Directory.Exists(SourceDir))
{
SourceFiles = Directory.GetFiles(SourceDir).ToList();
}
}
and call this method every time you press the Start button
private void Start_Click(object sender, RoutedEventArgs e)
{
StartTimer();
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
dispatcherTimer.IsEnabled = false;
}
I suggest also to reset the global variable that keeps track of the files transfered to zero when inside the Tick event you discover that every file has been transfered (or disable the Start button)
....
else
{
Console.WriteLine(string.Format("Total number of files transferred: {0}. Transfer Completed", TransferCount + 1));
(sender as DispatcherTimer).Stop();
(sender as DispatcherTimer).Tick -= dt_Tick;
TransferCount = 0;
}
Otherwise if you restart the timer in this situation it will stop immediately
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments