// The EndGetResponseCallback method // completes a call to BeginGetResponse. private void EndGetResponseCallback(IAsyncResult ar) { FtpState state = (FtpState)ar.AsyncState; FtpWebResponse response = null; try { response = (FtpWebResponse)state.Request.EndGetResponse(ar); response.Close(); state.StatusDescription = response.StatusDescription; // Signal the main application thread that // the operation is complete. state.OperationComplete.Set(); state.Request.GetResponse(); Ctrl.Invoke(new Action(delegate { if (UploadCompleted != null) { UploadCompleted.Invoke(this, EventArgs.Empty); } })); } // Return exceptions to the main application thread. catch (Exception e) { if (UploadError != null) { ThreadExceptionEventArgs arg = new ThreadExceptionEventArgs(e); UploadError.Invoke(this, arg); } } }
private void OnUploadError(UploadOperation e, ICompletedUpload upload, Exception ex) { UploadError?.Invoke(this, new TransferOperationErrorEventArgs( e.Guid, upload.Name, upload.ContentType, ex)); }
private void EndGetStreamCallback(IAsyncResult ar) { FtpState state = (FtpState)ar.AsyncState; Stream requestStream = null; // End the asynchronous call to get the request stream. try { requestStream = state.Request.EndGetRequestStream(ar); // Copy the file contents to the request stream. const int bufferLength = 2048; byte[] buffer = new byte[bufferLength]; int count = 0; int readBytes = 0; FileStream stream = File.OpenRead(state.FileName); do { readBytes = stream.Read(buffer, 0, bufferLength); requestStream.Write(buffer, 0, readBytes); count += readBytes; Ctrl.Invoke(new Action(delegate() { if (ProgressChanged != null) { ProgressChanged.Invoke(this, new ProgressEventArgs(stream.Length, count, readBytes)); } })); }while (readBytes != 0); stream.Close(); Console.WriteLine("Writing {0} bytes to the stream.", count); // IMPORTANT: Close the request stream before sending the request. requestStream.Close(); // Asynchronously get the response to the upload request. state.Request.BeginGetResponse( new AsyncCallback(EndGetResponseCallback), state ); } // Return exceptions to the main application thread. catch (Exception e) { if (UploadError != null) { ThreadExceptionEventArgs arg = new ThreadExceptionEventArgs(e); UploadError.Invoke(this, arg); } } }
public async Task <UploadStatus> Upload(string[] logLines, UploadMetaData data) { var result = LogValidator.Validate(logLines); if (!result.IsValid) { UploadError?.Invoke(new UploadErrorEventArgs(0, data, result.Reason)); return(new UploadStatus(new InvalidLogException(result.Reason))); } var log = string.Join(Environment.NewLine, logLines); var item = new UploaderItem(log.GetHashCode()); if (_inProgress.Contains(item)) { Log.Debug($"{item.Hash} already in progress. Waiting for it to complete..."); _inProgress.Add(item); return(await item.Status); } _inProgress.Add(item); Log.Debug($"Uploading {item.Hash}..."); UploadStatus status; UploadInitiated?.Invoke(new UploadStatusChangedEventArgs(item.Hash, data)); try { status = await TryUpload(logLines, data); } catch (Exception ex) { Log.Error(ex); status = new UploadStatus(ex); } Log.Debug($"{item.Hash} complete. Success={status.Success}"); UploadComplete?.Invoke(new UploadCompleteEventArgs(item.Hash, data, status)); foreach (var waiting in _inProgress.Where(x => x.Hash == item.Hash)) { waiting.Complete(status); } _inProgress.RemoveAll(x => x.Hash == item.Hash); return(status); }
private bool ProcessResult(Task <Tuple <string, MyFile> > task) { bool success = false; Tuple <string, MyFile> result = task.Result; if (string.IsNullOrEmpty(result.Item1)) { result.Item2.Status = MyFile.CompletionStatus.Complete; FileUploadComplete?.Invoke(this, result.Item2); success = true; } else { result.Item2.Status = MyFile.CompletionStatus.Error; UploadError?.Invoke(this, result.Item1); } return(success); }
private void OnUploadError(UploadErrorEventArgs e) { UploadError?.Invoke(this, e); }