0
我正在使用預編碼的AsynchronousCommand引用類來調用我的方法,我想使用相同的引用來取消該方法,因爲此引用類已經有一些方法可以取消。 您可以在以下網站也看到這個類: http://pastebin.com/00eStgP6取消異步方法的按鈕(wpf - databinding命令)
public class AsynchronousCommand : CommandReference, INotifyPropertyChanged
{
public AsynchronousCommand(Action action, bool canExecute = true)
: base(action, canExecute)
{
Initialise();
}
public AsynchronousCommand(Action<object> parameterizedAction, bool canExecute = true)
: base(parameterizedAction, canExecute)
{
Initialise();
}
private void Initialise()
{
cancelCommand = new CommandReference(
() =>
{
IsCancellationRequested = true;
}, true);
}
public override void DoExecute(object param)
{
if (IsExecuting)
return;
CancelCommandEventArgs args = new CancelCommandEventArgs() { Parameter = param, Cancel = false };
InvokeExecuting(args);
if (args.Cancel)
return;
IsExecuting = true;
callingDispatcher = Dispatcher.CurrentDispatcher;
ThreadPool.QueueUserWorkItem(
(state) =>
{
InvokeAction(param);
ReportProgress(
() =>
{
IsExecuting = false;
if (IsCancellationRequested)
InvokeCancelled(new CommandEventArgs() { Parameter = param });
else
InvokeExecuted(new CommandEventArgs() { Parameter = param });
IsCancellationRequested = false;
}
);
}
);
}
private void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler propertyChanged = PropertyChanged;
if (propertyChanged != null)
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public void ReportProgress(Action action)
{
if (IsExecuting)
{
if (callingDispatcher.CheckAccess())
action();
else
callingDispatcher.BeginInvoke(((Action)(() => { action(); })));
}
}
public bool CancelIfRequested()
{
if (IsCancellationRequested == false)
return false;
return true;
}
protected void InvokeCancelled(CommandEventArgs args)
{
CommandEventHandler cancelled = Cancelled;
if (cancelled != null)
cancelled(this, args);
}
protected Dispatcher callingDispatcher;
private bool isExecuting = false;
private bool isCancellationRequested;
private CommandReference cancelCommand;
public event PropertyChangedEventHandler PropertyChanged;
public event CommandEventHandler Cancelled;
public bool IsExecuting
{
get
{
return isExecuting;
}
set
{
if (isExecuting != value)
{
isExecuting = value;
NotifyPropertyChanged("IsExecuting");
}
}
}
public bool IsCancellationRequested
{
get
{
return isCancellationRequested;
}
set
{
if (isCancellationRequested != value)
{
isCancellationRequested = value;
NotifyPropertyChanged("IsCancellationRequested");
}
}
}
public CommandReference CancelCommand
{
get { return cancelCommand; }
}
}
我在視圖結合的命令。
Command="{Binding ComandoProcessarArquivo}
而且在我的ViewModel:
private AsynchronousCommand _comandoProcessarArquivo;
public ICommand ComandoProcessarArquivo
{
get { return _comandoProcessarArquivo ?? (_comandoProcessarArquivo = new AsynchronousCommand(new Action(() => ProcessarArquivo()))); }
}
private void ProcessarArquivo()
{
new ProcessarArquivo().Iniciar(ArquivoOrigem, ArquivoDestino, AtualizarEtapaProcesso);
}
所以,我試圖創建2個多小時的取消按鈕,可以請別人給我淡淡的道:|
謝謝!
Andrew,我正在開發公司的軟件,他們使用這個課程,但我可以自由改變。我已經嘗試過,像你說的那樣執行cancelcommand,但它沒有工作:(我會盡快查看nito.asyncex,你有任何鏈接?謝謝你隊友:P –