Пример #1
0
        private void bw_AsyncUploadDoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                AsyncUploadHelper upload = (AsyncUploadHelper)e.Argument;

                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(upload.URL);
                request.Proxy       = this.Options.ProxySettings;
                request.Method      = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential(this.Options.Account.UserName, this.Options.Account.Password);
                request.KeepAlive   = false;
                request.UsePassive  = !this.Options.Account.IsActive;

                using (upload.Stream)
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        ProgressManager progress = new ProgressManager(upload.Stream.Length);

                        byte[] buffer = new byte[BufferSize];
                        int    bytesRead;

                        while ((bytesRead = upload.Stream.Read(buffer, 0, BufferSize)) > 0)
                        {
                            requestStream.Write(buffer, 0, bytesRead);
                            progress.ChangeProgress(bytesRead);
                            upload.BackgroundWorker.ReportProgress((int)progress.Percentage, progress);
                        }
                    }
            }
            catch (Exception ex)
            {
                DebugHelper.WriteException(ex);
            }
        }
        private bool TransferData(Stream dataStream, Stream requestStream)
        {
            dataStream.Position = 0;
            ProgressManager progress = new ProgressManager(dataStream.Length);
            int             length   = (int)Math.Min(BufferSize, dataStream.Length);

            byte[] buffer = new byte[length];
            int    bytesRead;

            while ((bytesRead = dataStream.Read(buffer, 0, length)) > 0)
            {
                if (stopUpload)
                {
                    return(false);
                }

                requestStream.Write(buffer, 0, bytesRead);

                if (progress.ChangeProgress(bytesRead))
                {
                    OnProgressChanged(progress);
                }
            }

            return(true);
        }
 private void OnTransferProgressChanged(object sender, TransferProgressEventArgs e)
 {
     if (ProgressChanged != null)
     {
         progress.ChangeProgress(e.BytesTransferred);
         ProgressChanged(progress);
     }
 }
Пример #4
0
 private void OnTransferProgressChanged(SftpUploadAsyncResult e)
 {
     if (ProgressChanged != null)
     {
         progress.ChangeProgress((int)e.UploadedBytes, true);
         ProgressChanged(progress);
     }
 }
Пример #5
0
        /// <summary>
        /// Загружает данные с рабочего каталога.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadDataFromFiles_OnClick(object sender, RoutedEventArgs e)
        {
            object lockObj = new object();

            BindingOperations.EnableCollectionSynchronization(BankProv.BankObj.LegalPersonsDepartment.Clients, lockObj);
            BindingOperations.EnableCollectionSynchronization(BankProv.BankObj.PhysicalPersonsDepartment.Clients, lockObj);
            Action <object> action = o =>
            {
                Data   data         = o as Data;
                string json         = File.ReadAllText(data.File);
                var    bankProvider = JsonConvert.DeserializeObject <BankProvider>(json, new JsonSerializerSettings
                {
                    TypeNameHandling  = TypeNameHandling.Auto,
                    NullValueHandling = NullValueHandling.Ignore
                });
                lock (lockObj)
                {
                    BankProv += bankProvider;
                }
            };

            string[] files = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.clients");

            // Готовим потоки.
            int           threadsCount = Math.Min(files.Length, Environment.ProcessorCount - 1);
            List <Thread> threads      = new List <Thread>();

            for (int i = 0; i < threadsCount; i++)
            {
                var parameterizedThreadStart = new ParameterizedThreadStart(action);
                threads.Add(new Thread(parameterizedThreadStart));
            }

            ProgressManager progressManager = new ProgressManager();

            progressManager.BeginWaiting();
            progressManager.SetProgressMaxValue(threads.Count);
            for (int i = 0; i < threads.Count; i++)
            {
                threads[i].Start(new Data(files[i]));
            }
            for (int i = 0; i < threads.Count; i++)
            {
                threads[i].Join();
                progressManager.ChangeProgress(i + 1);
            }
            Thread.Sleep(100);
            progressManager.EndWaiting();
        }
Пример #6
0
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ProgressManager pm = new ProgressManager();

            pm.BeginWaiting();
            pm.SetProgressMaxValue(10);

            for (int i = 0; i < 10; i++)
            {
                pm.ChangeStatus("Loading " + i.ToString() + " from 10");
                pm.ChangeProgress(i);
                Thread.Sleep(1000);
            }

            pm.EndWaiting();
        }
Пример #7
0
        public bool DownloadFile(string url, string savePath)
        {
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
                request.Proxy       = this.Options.ProxySettings;
                request.Method      = WebRequestMethods.Ftp.DownloadFile;
                request.Credentials = new NetworkCredential(this.Options.Account.UserName, this.Options.Account.Password);
                request.KeepAlive   = false;
                request.UsePassive  = !this.Options.Account.IsActive;

                using (FileStream fileStream = new FileStream(savePath, FileMode.Create))
                    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                        using (Stream stream = response.GetResponseStream())
                        {
                            ProgressManager progress = new ProgressManager(stream.Length);

                            byte[] buffer = new byte[BufferSize];
                            int    bytesRead;

                            while ((bytesRead = stream.Read(buffer, 0, BufferSize)) > 0)
                            {
                                fileStream.Write(buffer, 0, bytesRead);
                                progress.ChangeProgress(bytesRead);
                                OnProgressChanged(progress);
                            }
                        }

                WriteOutput(string.Format("DownloadFile: {0} -> {1}", url, savePath));
                return(true);
            }
            catch (Exception ex)
            {
                WriteOutput(string.Format("Error: {0} - DownloadFile: {1} -> {2}", ex.Message, url, savePath));
            }
            return(false);
        }
Пример #8
0
        /// <summary>
        /// Генерирует большое количество данных.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GenerateDataButton_OnClick(object sender, RoutedEventArgs e)
        {
            Random random = new Random();
            ObservableCollection <Client> newClients = new ObservableCollection <Client>();
            int             n = 10000;
            ProgressManager progressManager = new ProgressManager();

            progressManager.BeginWaiting();
            progressManager.ChangeStatus("В отдельном потоке выполняем генерацию записей...");
            progressManager.SetProgressMaxValue(n);

            for (int i = 0; i < n; i++)
            {
                ClientBase client = Convert.ToBoolean(random.Next(1, 10) % 2 == 0) ?
                                    (ClientBase) new Client(Guid.NewGuid().ToString()) :
                                    new VipClient(Guid.NewGuid().ToString());


                newClients.Add(new Client(Guid.NewGuid().ToString()));
                BankProv.BankObj.AddNewClient(client, random.Next(1, 10) % 2 != 0);
                progressManager.ChangeProgress(i);
            }
            progressManager.EndWaiting();
        }
Пример #9
0
        public bool Upload(Stream stream, string url)
        {
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
                request.Proxy       = Options.ProxySettings;
                request.Method      = WebRequestMethods.Ftp.UploadFile;
                request.Credentials = new NetworkCredential(Options.Account.UserName, Options.Account.Password);
                request.KeepAlive   = false;
                request.UsePassive  = !Options.Account.IsActive;

                using (stream)
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        ProgressManager progress = new ProgressManager(stream.Length);

                        byte[] buffer = new byte[BufferSize];
                        int    bytesRead;

                        while ((bytesRead = stream.Read(buffer, 0, BufferSize)) > 0)
                        {
                            requestStream.Write(buffer, 0, bytesRead);
                            progress.ChangeProgress(bytesRead);
                            OnProgressChanged(progress);
                        }
                    }

                WriteOutput("Upload: " + url);
                return(true);
            }
            catch (Exception ex)
            {
                WriteOutput(string.Format("Error: {0} - Upload: {1}", ex.Message, url));
            }
            return(false);
        }