Error Making Method Async
-
My app has a FileSystemWatcher. Once a file is dropped into the target folder it is then Ftp'd to the server. I'm trying to make it async but I'm getting errors. I want to make the method async so that multiple files can be uploaded without blocking.
class Program
{
private static string _address;
private static string _userName;
private static string _password;
static void Main(string[] args)
{
}
// A FileSystemWatcher passes the file name into this method
public static async void FileReceived(string fileName)
{
Task t = await Task.Run(() => FTPFile(fileName)); // <=== ERROR HERE
await t.ContinueWith((t1) =>
{
FileUploaded(t1.Result);
});
}
// FTP the file to the server
private static string FTPFile(string fileName)
{
string remoteFile = Path.GetFileName(fileName);
SFTP sftp = new SFTP(_address, _userName, _password);
sftp.UploadFileAsync(fileName, remoteFile);
return fileName;
}
private static async void FileUploaded(string fileName)
{
// Once here, the file has been transfered.
}
}On the Task line I get the error:
Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task'
I'm not sure how to fix this. Can someone help? Thanks
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
My app has a FileSystemWatcher. Once a file is dropped into the target folder it is then Ftp'd to the server. I'm trying to make it async but I'm getting errors. I want to make the method async so that multiple files can be uploaded without blocking.
class Program
{
private static string _address;
private static string _userName;
private static string _password;
static void Main(string[] args)
{
}
// A FileSystemWatcher passes the file name into this method
public static async void FileReceived(string fileName)
{
Task t = await Task.Run(() => FTPFile(fileName)); // <=== ERROR HERE
await t.ContinueWith((t1) =>
{
FileUploaded(t1.Result);
});
}
// FTP the file to the server
private static string FTPFile(string fileName)
{
string remoteFile = Path.GetFileName(fileName);
SFTP sftp = new SFTP(_address, _userName, _password);
sftp.UploadFileAsync(fileName, remoteFile);
return fileName;
}
private static async void FileUploaded(string fileName)
{
// Once here, the file has been transfered.
}
}On the Task line I get the error:
Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task'
I'm not sure how to fix this. Can someone help? Thanks
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
You should start the
Task
and thenawait
it.public static async void FileReceived(string fileName)
{
Task<string> t = Task.Run(() => FTPFile(fileName));
await t.ContinueWith((t1) =>
{
FileUploaded(t1.Result);
});
} -
My app has a FileSystemWatcher. Once a file is dropped into the target folder it is then Ftp'd to the server. I'm trying to make it async but I'm getting errors. I want to make the method async so that multiple files can be uploaded without blocking.
class Program
{
private static string _address;
private static string _userName;
private static string _password;
static void Main(string[] args)
{
}
// A FileSystemWatcher passes the file name into this method
public static async void FileReceived(string fileName)
{
Task t = await Task.Run(() => FTPFile(fileName)); // <=== ERROR HERE
await t.ContinueWith((t1) =>
{
FileUploaded(t1.Result);
});
}
// FTP the file to the server
private static string FTPFile(string fileName)
{
string remoteFile = Path.GetFileName(fileName);
SFTP sftp = new SFTP(_address, _userName, _password);
sftp.UploadFileAsync(fileName, remoteFile);
return fileName;
}
private static async void FileUploaded(string fileName)
{
// Once here, the file has been transfered.
}
}On the Task line I get the error:
Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task'
I'm not sure how to fix this. Can someone help? Thanks
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Your FTPFile method returns a string. You're trying to put it in a Task of string.
This space for rent
-
My app has a FileSystemWatcher. Once a file is dropped into the target folder it is then Ftp'd to the server. I'm trying to make it async but I'm getting errors. I want to make the method async so that multiple files can be uploaded without blocking.
class Program
{
private static string _address;
private static string _userName;
private static string _password;
static void Main(string[] args)
{
}
// A FileSystemWatcher passes the file name into this method
public static async void FileReceived(string fileName)
{
Task t = await Task.Run(() => FTPFile(fileName)); // <=== ERROR HERE
await t.ContinueWith((t1) =>
{
FileUploaded(t1.Result);
});
}
// FTP the file to the server
private static string FTPFile(string fileName)
{
string remoteFile = Path.GetFileName(fileName);
SFTP sftp = new SFTP(_address, _userName, _password);
sftp.UploadFileAsync(fileName, remoteFile);
return fileName;
}
private static async void FileUploaded(string fileName)
{
// Once here, the file has been transfered.
}
}On the Task line I get the error:
Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task'
I'm not sure how to fix this. Can someone help? Thanks
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
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 aTask
, or does it raise anUploadFileCompleted
event? If it raises an event (EAP pattern), you can wrap that in aTask
-returning method, and make yourFTPFile
methodasync
as well. How to: Wrap EAP Patterns in a Task[^] Tasks and the Event-based Asynchronous Pattern | Parallel Programming with .NET[^]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;