Tasks.WhenAll(tasks) is not catching any exception

I am new to C# and Windows Forms.

I am using syncfusion C# library to encrypt / decrypt Word files.

I am using Async / Await tasks to decrypt files in bulk.

I have no problem to encrypt / decrypt one file or to encrypt multiple files.

I encountered the problem when trying to decrypt multiple word files or in bulk.

When decrypting one file with a wrong password, I get an exception and everything works fine.

But during bulk files decryption using await Task.WhenAll(tasks) inside try-catch block, I am not getting any exception while from the first task (first file to decrypt) I should get an exception because the password is wrong.

During entire week I tried most of the provided solutions everywhere but not successful and I don’t know what I have to do.

Thank you very much!

The code should throw an exception when the first task is not successful but it’s not.

GetPathOrExtention gte = new GetPathOrExtention();

private void DecryptDOCXFiles(string fileName)
{
    FileStream inputStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    WordDocument document = new WordDocument(inputStream, FormatType.Docx, TbDecPwd1.Text);

    FileStream outputStream = new FileStream(gte.GetDirPath(fileName) + "\" + gte.GetfileName(fileName).Replace("Encrypted-", ""), FileMode.Create, FileAccess.Write);
    document.Save(outputStream, FormatType.Docx);
    inputStream.Close();
    outputStream.Close();
}

private async Task DecryptWordFiles()
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "docx files (*.docx)|*.docx|All files (*.*)|*.*";
    ofd.Multiselect = true;
    var tasks = new List<Task>();

    if (TbDecPwd1.Text != "" && ofd.ShowDialog() == DialogResult.OK)
    {
        foreach (string file in ofd.FileNames)
        {
            if (gte.GetFileExtension(file) != ".docx")
            {
                LblProceeding.ResetText();
                MessageBox.Show("Files version is not supported!" + 'n' + 'n' + "Only Word version 2019 and above are supported!", "P-GEN", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (gte.GetFileExtension(file) == ".docx")
            {
                LblProceeding.Text = "Proceeding... Do not exit the software!";
                var task = Task.Run(() => DecryptDOCXFiles(file)).ContinueWith(p => progressBar1.PerformStep());
                tasks.Add(task);
            }
        }
    }
    try
        {
            await Task.WhenAll(tasks);  
            LblProceeding.Text = "Done!";
            MessageBox.Show("Files Successfully Decrypted!", "P-GEN", MessageBoxButtons.OK, MessageBoxIcon.Information);
            LblProceeding.ResetText();
            progressBar1.Visible = false;     
        }

    catch (AggregateException)
    {
        LblProceeding.ResetText();
        MessageBox.Show("Ensure the password is correct!" + 'n' + 'n' + "Ensure the files you want to encrypt are not opened in another software!", "P-GEN", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }

    catch (Exception)
    {
        LblProceeding.ResetText();
        MessageBox.Show("Ensure the password is correct!" + 'n' + 'n' + "Ensure the files you want to encrypt are not opened in another software!", "P-GEN", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
    }           
}

private void BtnWordFilesDec_Click(object sender, EventArgs e)
{
    _ = DecryptWordFiles();
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật