Search code examples
wpftaskdropbox

WPF window freezing after sending file to dropbox


My window is freezing after clicking "Send" button. It send file and then it should release GUI, but it doesn't. See my code:

    private void btn_send_Click(object sender, RoutedEventArgs e)
    {
        if (SBox.UploadFile("user", "service", 1, path.Text))
            MessageBox.Show("Success");
        else
            MessageBox.Show("Fail, try again");
    }

Then code from Sbox class:

    public static bool UploadFile(string user, string service, int orderId, string filepath)
    {
        DateTime now = DateTime.Now;
        dbx = new DropboxClient(login_key);

        string path = "/Files/" + user + "/" + service + "/order - " + orderId + "/received";
        string fileName = now.Year + "" + now.ToString("MM") + "" + now.ToString("dd") + "" + Path.GetExtension(filepath);

        Task upl = Upload(dbx, path, fileName, File.ReadAllBytes(filepath)); //send file
        upl.Wait();

        return true;
    }

and task code:

    static async Task Upload(DropboxClient dbx, string folder, string file, byte[] content)
    {
        using (var mem = new MemoryStream(content))
        {
            var updated = await dbx.Files.UploadAsync(
                folder + "/" + file,
                WriteMode.Overwrite.Instance,
                body: mem);
            Console.WriteLine("Saved {0}/{1} rev {2}", folder, file, updated.Rev);
        }
    }

as I said, file is send to correct folder but GUI is freezing. Console app works perfectly, writing output when success.


Solution

  • I did play with it a bit and did some research. Here is my final working code Button:

        private async void btn_send_Click(object sender, RoutedEventArgs e)
        {
            SBox sbox = new SBox();
            progress.Text = "Uploading file, please wait...";
            bool t = await sbox.UploadFromGUI("user", "service", 1, path.Text);
            progress.Text = "";
            if (t == true)
                MessageBox.Show("Success!", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
            else
                MessageBox.Show("Failed", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    

    and method in SBox class:

        public async Task<bool> UploadFromGUI(string user, string service, int orderId, string filepath)
        {
            DateTime now = DateTime.Now;
            dbx = new DropboxClient(login_key);
    
            string path = "/files/" + user + "/" + service + "/order - " + orderId + "/received";
            string fileName = now.Year + "" + now.ToString("MM") + "" + now.ToString("dd") + "" + Path.GetExtension(filepath);
    
            try
            {
                using (var mem = new MemoryStream(File.ReadAllBytes(filepath)))
                {
                    var updated = await dbx.Files.UploadAsync(
                        path + "/" + fileName,
                        WriteMode.Overwrite.Instance,
                        body: mem);
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }