当我点击“开始”按钮时,我正在尝试从源到目标目录将每3秒转移Image file
。 “停止”按钮用于停止文件传输。
如果我第三次使用“开始”按钮,则事件不会触发;所以,文件没有传输。
我所做的进展如下:
Event is not firing
//无效! 我该如何解决这个问题?提前谢谢。
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();
}
}
}
答案 0 :(得分:2)
如果您调用Stop,则在将IsEnabled属性设置为true之前,不应触发计时器事件 所以我建议做一些重构
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();
}
}
每次按下“开始”按钮
时调用此方法 private void Start_Click(object sender, RoutedEventArgs e)
{
StartTimer();
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
dispatcherTimer.IsEnabled = false;
}
我建议还要重置全局变量,以便在Tick事件内部发现每个文件已被传输(或禁用“开始”按钮)时跟踪传输到零的文件。
....
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;
}
否则,如果在这种情况下重新启动计时器,它将立即停止
答案 1 :(得分:2)
不是真正的答案,但这是一个不使用计时器的重新考虑的版本。相反,它使用TaskEx.Delay
和async/await
,未经测试(针对带有vs2012 +和Microsoft.Bcl.Async
的.NET 4.0):
using System;
using System.Collections.Generic;
using System.Windows;
using System.Threading;
using System.Threading.Tasks;
namespace GridTest
{
/// <summary>
/// Interaction logic for Window3.xaml
/// </summary>
public partial class Window3 : Window
{
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\";
public Window3()
{
InitializeComponent();
this.Loaded += Window3_Loaded;
}
void Window3_Loaded(object sender, RoutedEventArgs e)
{
}
async Task DoWorkAsync(CancellationToken token)
{
int transferCount;
for (transferCount = 0; transferCount < SourceFiles.Count; transferCount++)
{
token.ThrowIfCancellationRequested();
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;
await TaskEx.Delay(3000, token); // 3s delay
}
Console.WriteLine(string.Format("Total number of files transferred: {0}. Transfer Completed", transferCount + 1));
}
CancellationTokenSource _cts;
Task _task;
private void Start_Click(object sender, RoutedEventArgs e)
{
if (_cts != null)
_cts.Cancel();
_cts = new CancellationTokenSource();
_task = TaskEx.Run(() => DoWorkAsync(_cts.Token), _cts.Token);
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
if (_cts != null)
_cts.Cancel();
}
}
}
答案 2 :(得分:1)