private T ExecuteAsync <T>(WsPath path, Func <Task <T> > asyncFunc)
 {
     using (ThreadKeeper exec = new ThreadKeeper())
     {
         try
         {
             return(exec.ExecAsync(async(cancellationToken) =>
             {
                 await EnsureLogin();
                 return await asyncFunc();
             }));
         }
         catch (Exception ex)
         {
             try
             {
                 if (ex.InnerException != null)
                 {
                     throw ex.InnerException;
                 }
                 throw;
             }
             catch (FileNotFoundException)
             {
                 ShowError($"Path {path} not found.", null, false);
                 return(default(T));
             }
             catch (DirectoryNotFoundException)
             {
                 ShowError($"Folder {path} not found.", null, false);
                 return(default(T));
             }
         }
     }
 }
示例#2
0
        public virtual FileSystemExitCode PutFile(string localName, RemotePath remoteName, CopyFlags copyFlags)
        {
            try {
                // My ThreadKeeper class is needed here because calls to ProgressProc must be made from this thread and not from some random async one.
                using (var exec = new ThreadKeeper()) {
                    void Progress(int percentDone)
                    {
                        exec.RunInMainThread(() => {
                            if (ProgressProc(localName, remoteName, percentDone))
                            {
                                exec.Cancel();
                            }
                        });
                    }

                    var ret = exec.ExecAsync(asyncFunc: (token) => PutFileAsync(localName, remoteName, copyFlags, Progress, token));

                    return(ret);
                }
            }
            catch (TaskCanceledException) {
                return(FileSystemExitCode.UserAbort);
            }
            catch (OperationCanceledException) {
                return(FileSystemExitCode.UserAbort);
            }
            catch (AggregateException e) {
                if (HasCanceledException(e))
                {
                    return(FileSystemExitCode.UserAbort);
                }

                throw;
            }
        }
 public bool UnRegisterAccount(WsAccountRepository accountRepository)
 {
     if (accountRepository.UnRegisterAccount(_account))
     {
         if (_apiClient.IsLoggedIn)
         {
             using (ThreadKeeper exec = new ThreadKeeper())
             {
                 exec.ExecAsync((cancellationToken) => _apiClient.Logout());
             }
         }
         return(true);
     }
     return(false);
 }
 public static bool TryRegisterAccount(WsAccountRepository accountRepository, TcUIProvider uiProvider, WsAccountLoginInfo userCredential, out WsAccountAccessor accountAccessor)
 {
     WsAccountRepository.SuccessAccountRegistrationInfo successRegistration;
     using (ThreadKeeper exec = new ThreadKeeper())
     {
         successRegistration = exec.ExecAsync((cancellationToken) => accountRepository.TryRegisterAccount(userCredential));
     }
     if (successRegistration != null)
     {
         accountAccessor = new WsAccountAccessor(successRegistration.Account, uiProvider, successRegistration.ConnectedApiClient);
         return(true);
     }
     accountAccessor = null;
     return(false);
 }
        public FileSystemExitCode CopyFile(WsPath sourceFilePath, WsPath targetPath, bool overwrite, IProgress <int> progress)
        {
            try
            {
                using (ThreadKeeper exec = new ThreadKeeper())
                {
                    return(exec.ExecAsync(async(cancellationToken) =>
                    {
                        await EnsureLogin();
                        WsFile sourceFile = await _apiClient.FindFile(sourceFilePath.GetFilePath());
                        if (sourceFile != null)
                        {
                            if (overwrite == false)
                            {
                                if (await _apiClient.FindFile(targetPath.GetFilePath()) != null)
                                {
                                    return FileSystemExitCode.FileExists;
                                }
                            }
                        }

                        if (sourceFile == null)
                        {
                            return FileSystemExitCode.FileNotFound;
                        }
                        WsFolder targetFolder = await _apiClient.FindFolder(targetPath.Parent.GetFolderPath());
                        if (targetFolder == null)
                        {
                            return FileSystemExitCode.FileNotFound;
                        }

                        await sourceFile.Copy(targetFolder, cancellationToken, new ThreadKeeperCancellableProgress(exec, progress));
                        return FileSystemExitCode.OK;
                    }));
                }
            }
            catch (TaskCanceledException)
            {
                return(FileSystemExitCode.UserAbort);
            }
            catch (Exception ex)
            {
                ShowError("Copy file error", ex, false);
                return(FileSystemExitCode.WriteError);
            }
        }
示例#6
0
 private static ThreadKeeper GetCurrentThreadKeeper() => _exec ?? (_exec = new ThreadKeeper());
 public ThreadKeeperCancellableProgress(ThreadKeeper exec, IProgress <int> parrentProgress)
 {
     _exec            = exec;
     _parrentProgress = parrentProgress;
 }