I'm trying to write a program in C# that downloads a file from my DropBox account but I keep getting an error with var task = Task.Run((Func<Task>)Download("", "largetest.mpk", folderName)); The error: Cannot convert type 'System.Threading.Tasks.Task' to 'System.Func<System.Threading.Tasks.Task>

I've been trying to figure it out for multiple hours and haven't had any luck. Any suggestions?

 private void button1_Click(object sender, EventArgs e)
     {
     string folderName = @"c:\dropboxTest\test.exe";

    var task = Task.Run((Func<Task>)Download("", "largetest.mpk", folderName));
    task.Wait();

    }

    async Task Download(string folder, string targetfile, string localPath)
        {
            var dbx = new DropboxClient(Form1.api);
            var response = await dbx.Files.DownloadAsync(folder + "/" + targetfile);
            ulong fileSize = response.Response.Size;
            const int bufferSize = 1024 * 1024;

            var buffer = new byte[bufferSize];
            string folderName = @"C:\dropboxTest\teasdfst.exe";
            using (var stream = await response.GetContentAsStreamAsync())
            {
                using (var localfile = new FileStream(folderName, FileMode.OpenOrCreate))
                {
                    var length = stream.Read(buffer, 0, bufferSize);

                    while (length > 0)
                    {
                        localfile.Write(buffer, 0, length);
                       // Console.WriteLine(localfile.);
                        var percentage = 100 * (ulong)localfile.Length / fileSize;
                        // Update progress bar with the percentage.
                        // progressBar.Value = (int)percentage
                        //Console.WriteLine(percentage);

                        length = stream.Read(buffer, 0, bufferSize);
                    }
                }
            }
        }

Solved:

void Download(string folder, string targetfile, string localPath)
{
    var dbx = new DropboxClient(Form1.api);
    var response = await dbx.Files.DownloadAsync(folder + "/" + targetfile);
    ulong fileSize = response.Response.Size;
    const int bufferSize = 1024 * 1024;
    var buffer = new byte[bufferSize];
    string folderName = @"C:\dropboxTest\teasdfst.exe";
    using (var stream = await response.GetContentAsStreamAsync())
    {
        using (var localfile = new FileStream(folderName, FileMode.OpenOrCreate))
        {
            var length = stream.Read(buffer, 0, bufferSize);
            while (length > 0)
            {
                localfile.Write(buffer, 0, length);
               // Console.WriteLine(localfile.);
                var percentage = 100 * (ulong)localfile.Length / fileSize;
                // Update progress bar with the percentage.
                // progressBar.Value = (int)percentage
                //Console.WriteLine(percentage);
                length = stream.Read(buffer, 0, bufferSize);
            }
        }
    }
}
private void button1_Click(object sender, EventArgs e)
{
    string folderName = @"c:\dropboxTest\test.exe";
    var task = Task.Run(() => Download("", "largetest.mpk", folderName));
    task.Wait(); // remove this if you don't want to block UI thread.
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.