Click here to Skip to main content
15,887,676 members
Home / Discussions / C#
   

C#

 
AnswerRe: Column sum of multiple columns of dynamic gridview using Javascript Pin
Pete O'Hanlon27-Sep-16 23:20
mvePete O'Hanlon27-Sep-16 23:20 
QuestionDelegates with Events Pin
Member 1116162526-Sep-16 18:01
Member 1116162526-Sep-16 18:01 
AnswerRe: Delegates with Events Pin
Pete O'Hanlon26-Sep-16 19:36
mvePete O'Hanlon26-Sep-16 19:36 
GeneralRe: Delegates with Events Pin
Rahul VB27-Sep-16 23:04
professionalRahul VB27-Sep-16 23:04 
QuestionError Making Method Async Pin
Kevin Marois24-Sep-16 12:13
professionalKevin Marois24-Sep-16 12:13 
AnswerRe: Error Making Method Async Pin
George Swan24-Sep-16 12:43
mveGeorge Swan24-Sep-16 12:43 
AnswerRe: Error Making Method Async Pin
Pete O'Hanlon24-Sep-16 21:31
mvePete O'Hanlon24-Sep-16 21:31 
SuggestionRe: Error Making Method Async Pin
Richard Deeming26-Sep-16 1:52
mveRichard Deeming26-Sep-16 1:52 
You should avoid async void methods wherever possible:
Avoid async void methods - You’ve Been Haacked[^]
Async/Await - Best Practices in Asynchronous Programming[^]

Also, sftp.UploadFileAsync implies that the method returns before the upload has completed. Does this method return a Task, or does it raise an UploadFileCompleted event? If it raises an event (EAP pattern), you can wrap that in a Task-returning method, and make your FTPFile method async as well.

How to: Wrap EAP Patterns in a Task[^]
Tasks and the Event-based Asynchronous Pattern | Parallel Programming with .NET[^]

C#
public static class SFTPExtensions
{
    public static Task UploadFileTaskAsync(this SFTP sftp, string fileName, string remoteFile)
    {
        var tcs = new TaskCompletionSource<bool>();
        
        UploadFileCompletedEventHandler handler = null;
        handler = (sender, args) =>
        {
            if (args.Cancelled)
            {
                tcs.TrySetCancelled();
            }
            else if (args.Error != null)
            {
                tcs.TrySetException(args.Error);
            }
            else
            {
                tcs.TrySetResult(true);
            }
            
            sftp.UploadFileCompleted -= handler;
        };
        
        sftp.UploadFileCompleted += handler;
        
        try
        {
            sftp.UploadFileAsync(fileName, remoteFile);
        }
        catch (Exception ex)
        {
            sftp.UploadFileCompleted -= handler;
            tcs.TrySetException(ex);
        }
        
        return tcs.Task;
    }
}

C#
public static async Task FileReceived(string fileName)
{
    string result = await FTPFile(fileName).ConfigureAwait(false);
    await FileUploaded(result).ConfigureAwait(false);
}

private static async Task<string> FTPFile(string fileName)
{
    string remoteFile = Path.GetFileName(fileName);
    SFTP sftp = new SFTP(_address, _userName, _password);
    await sftp.UploadFileTaskAsync(fileName, remoteFile).ConfigureAwait(false);
    return fileName;
}

private static async Task FileUploaded(string fileName)
{
    ...
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


QuestionCancelRequest not work! Pin
Programmer 124-Sep-16 7:13
Programmer 124-Sep-16 7:13 
AnswerRe: CancelRequest not work! Pin
Eddy Vluggen24-Sep-16 11:48
professionalEddy Vluggen24-Sep-16 11:48 
GeneralRe: CancelRequest not work! Pin
Programmer 124-Sep-16 19:31
Programmer 124-Sep-16 19:31 
GeneralRe: CancelRequest not work! Pin
Eddy Vluggen26-Sep-16 4:53
professionalEddy Vluggen26-Sep-16 4:53 
QuestionStrange problem in Exception handling Pin
Programmer 123-Sep-16 21:44
Programmer 123-Sep-16 21:44 
AnswerRe: Strange problem in Exception handling Pin
OriginalGriff23-Sep-16 22:36
mveOriginalGriff23-Sep-16 22:36 
GeneralRe: Strange problem in Exception handling Pin
Programmer 124-Sep-16 0:28
Programmer 124-Sep-16 0:28 
Questionhow to display alert in system tray using c# Pin
Member 308047023-Sep-16 2:18
Member 308047023-Sep-16 2:18 
AnswerRe: how to display alert in system tray using c# Pin
Pete O'Hanlon23-Sep-16 2:23
mvePete O'Hanlon23-Sep-16 2:23 
AnswerRe: how to display alert in system tray using c# Pin
Eddy Vluggen23-Sep-16 6:22
professionalEddy Vluggen23-Sep-16 6:22 
QuestionCreate own language translator in C# Pin
Member 1051770422-Sep-16 21:47
Member 1051770422-Sep-16 21:47 
AnswerRe: Create own language translator in C# Pin
Pete O'Hanlon22-Sep-16 21:57
mvePete O'Hanlon22-Sep-16 21:57 
GeneralRe: Create own language translator in C# Pin
Member 1051770423-Sep-16 0:55
Member 1051770423-Sep-16 0:55 
GeneralRe: Create own language translator in C# Pin
Pete O'Hanlon23-Sep-16 1:04
mvePete O'Hanlon23-Sep-16 1:04 
GeneralRe: Create own language translator in C# Pin
Member 1051770423-Sep-16 1:42
Member 1051770423-Sep-16 1:42 
GeneralRe: Create own language translator in C# Pin
Pete O'Hanlon23-Sep-16 1:54
mvePete O'Hanlon23-Sep-16 1:54 
GeneralRe: Create own language translator in C# Pin
Member 1051770425-Sep-16 19:42
Member 1051770425-Sep-16 19:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.