VSTO Outlook addin - how to update UI from asynchronous process and also keep outlook responsive

Nikhilanjali Jayanthi 1 Reputation point
2020-08-27T10:27:10.02+00:00

I'm working on VSTO Outlook addin. On click of send (ItemSend event handler) of an email, attachments of the email are uploaded to Azure cloud storage. To track the progress I'm showing the upload progress using a progress bar. I'm facing an 2 issues here. Firstly, ProgressBar is blank and unresponsive. I don't see the progress at all. Secondly, outlook is completely blocked till upload is finished. But till the upload is in progress, user should be free to use outlook for ex. check other mails, send different mails and so on.

Calling method

//**Syncronization context is not null before calling
    var result = await ProgressForm.UploadFiles(files);
// **Perform action based on result. as of now control doesn't return back here

For uploading the files I've async method which does below operations.

public async Task<bool> UploadFiles(List<Files> filesList)
{
    try
    {
        //**Synchronization context shows null here**
        //Print((SynchronizationContext.Current is null) ? "sync context null" : SynchronizationContext.Current.ToString()));

        foreach (var file in filesList)
        {
            //using Microsoft.WindowsAzure.Storage.Blob cloudBlockBlob
            var cloudBlockBlob = new CloudBlockBlob(new Uri(file.Uri));
            int blockSize = 256 * 1024;              
            using (FileStream fileStream = new FileStream(file.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                int blockCount = (int)((float)fileSize / (float)blockSize) + 1;
                List<string> blockIDs = new List<string>();
                int blockNum = 0;
                int bytesRead = 0; 
                long bytesLeft = fileStream.Length; 

                while (bytesLeft > 0)
                {                           
                    blockNum++;
                    int bytesToRead;
                    if (bytesLeft >= blockSize)
                    {                               
                        bytesToRead = blockSize;
                    }
                    else
                    {                              
                        bytesToRead = (int)bytesLeft;
                    }
                    string blockId = GetBlockID(blockNum);
                    blockIDs.Add(blockId);
                    byte[] bytes = new byte[bytesToRead];
                    fileStream.Read(bytes, 0, bytesToRead);
                    string blockHash = MD5Hash(bytes);
                    //upload single block
                    await cloudBlockBlob.PutBlockAsync(blockId, new MemoryStream(bytes), blockHash);
                    bytesRead += bytesToRead;
                    bytesLeft -= bytesToRead;

                    //**update progress bar here**
                    progressBar1.Invoke(updateProgress); 
                   //**also tried using progress.Report(bytesRead); where progress is IProgress<int> but does not work
                }
                //**upload all blocks**
                await cloudBlockBlob.PutBlockListAsync(blockIDs);                        
            }
        }             
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}

Each time I upload one block using cloudBlockBlob.PutBlockAsync, I update the progress bar using IProgress<int> progress. Also used progressBar1.Invoke but no luck still. Upload is going file but after that application doesn't resume, it hangs forever. I'm new to VSTO and async concepts, so any leads are appreciated. Any ideas on how to achieve this?

Many thanks in advance.

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
35,923 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tianyu Sun-MSFT 27,106 Reputation points Microsoft Vendor
    2020-08-28T01:32:00.393+00:00

    Hello NikhilanjaliJayanthi,

    Visual Studio Tools for Office(VSTO) is not currently supported in the Microsoft Q&A forums, the supported products are listed over here: https://learn.microsoft.com/en-us/answers/products/ (more to be added later on).

    For the related questions about Visual Studio Tools for Office(VSTO) you can post in dedicated forums here: MSDN(Visual Studio Tools for Office).

    Sincerely,
    Tianyu

    0 comments No comments