I'm working on an Outlook add-in. After composing a new email, on click of send (ItemSend event handler), add in is uploading attachments to Azure cloud. To track the progress I'm using a progress bar. I'm facing an 2 issues here. Firstly, Progress bar
is grey and unresponsive. I don't see the progress at all. Secondly, outlook is completely blocked till upload is finished. But the requirement is, till the upload is in progress, user should be free able to check/send other mails.
Calling method:
ProgressForm.Show()
//**Syncronization context is not null before calling
var result = await ProgressForm.UploadFiles(files);
//also tried Task.Run(() =>ProgressForm.UploadFiles(files))
// **Perform action based on result. as of now control doesn't return back here
Below async method uploads files
public async Task<bool> UploadFiles(List<UploadFilesListModel> filesList)
{
try
{
//**Synchronization context is not 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;
}
}
I'm new to VSTO and async concepts, any leads are appreciated. Thank You