1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
public partial class MainWindow : Window, INotifyPropertyChanged
{
private FileSystemWatcher _watcher;
public event PropertyChangedEventHandler PropertyChanged;
private bool _isBusy = false;
public bool IsBusy
{
get { return _isBusy; }
set
{
_isBusy = value;
RaisePropertyChanged("IsBusy");
}
}
private DelegateCommand _launchProcessCommand;
public ICommand LaunchProcessCommand
{
get
{
if (_launchProcessCommand == null)
{
_launchProcessCommand =
new DelegateCommand(
DoWork,
() => File.Exists("free.txt"));
}
return _launchProcessCommand;
}
}
protected void RaisePropertyChanged(string prop)
{
if (string.IsNullOrWhiteSpace(prop) != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
//Get currentPath
string path;
path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
_watcher = new FileSystemWatcher(path);
_watcher.Path = path;
_watcher.Renamed += (s, e) =>
{
_launchProcessCommand.RaiseCanExecuteChanged();
};
_watcher.NotifyFilter = NotifyFilters.FileName;
_watcher.Filter = "*.txt";
_watcher.EnableRaisingEvents = true;
}
private void DoWork()
{
IsBusy = true;
LockProcess();
Task.Factory.StartNew(() =>
{
Thread.Sleep(4000);
//DO LONG PROCESS
ReleaseProcess();
Dispatcher.BeginInvoke(new Action(() =>
{
IsBusy = false;
}), null);
});
}
private void LockProcess()
{
if (File.Exists("free.txt"))
System.IO.File.Move(@"free.txt", @"lock.txt");
_launchProcessCommand.RaiseCanExecuteChanged();
}
private void ReleaseProcess()
{
if (File.Exists("lock.txt"))
System.IO.File.Move(@"lock.txt", @"free.txt");
_launchProcessCommand.RaiseCanExecuteChanged();
}
} |
Partager