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.