public void AddFiles(params string[] files) { foreach (var file in files) { var job = new SignJobViewModel(file); if (job.IsInvalid) { continue; } job.Close = new RelayCommand(x => Jobs.Remove(job), x=>!job.IsBusy); job.Sign = new RelayCommand(x => { Task.Factory .StartNew(() => { Thread.Sleep(50); job.State = SignJobState.Signing; DocumentSigner.For(job.FileType).Sign(job.InputFile, job.OutputFile, ActiveCert); }) .ContinueWith(task => { job.Error = task.Exception.InnerException.Message; job.State = SignJobState.Failed; }, TaskContinuationOptions.OnlyOnFaulted) .ContinueWith(task => job.State = SignJobState.Signed, TaskContinuationOptions.OnlyOnRanToCompletion); }, x => job.IsReady && ActiveCert != null); Jobs.Add(job); //if (job.IsReady) //{ // job.Sign.Execute(null); //} } }
public void AddFiles(params string[] files) { foreach (var file in files) { var job = new SignJobViewModel(file); if (job.IsInvalid) { //TODO:explain why, don't just fail so silently continue; } job.Close = new RelayCommand(x => Jobs.Remove(job)); job.Sign = new RelayCommand(x => { var cert = ActiveCert; if (cert != null) { var signTask = Task.Factory.StartNew(() => { job.State = SignJobState.Signing; job.OutputFile = GetOutputNameSafe(job.InputFileName); DocumentSigner.For(job.FileType) .Sign(job.InputFile, job.OutputFile, cert); }); signTask.ContinueWith(task => { job.Error = task.Exception.InnerException.Message; job.State = SignJobState.Failed; }, TaskContinuationOptions.OnlyOnFaulted); signTask.ContinueWith(task => { job.State = SignJobState.Signed; }, TaskContinuationOptions.OnlyOnRanToCompletion); } }); Jobs.Add(job); if (job.IsReady) { job.Sign.Execute(null); } } }