VSTO Outlook插件-上传大文件会阻止Outlook直到完成处理

问题描述

我正在使用Outlook加载项。撰写新电子邮件时,用户可以选择庞大的附件,然后在 点击发送 (ItemSend事件处理程序)上添加正在将文件上传到Azure云存储。为了跟踪进度,我正在使用进度条显示上传进度。我在这里面临2个问题。首先,进度栏为灰色且无响应。我完全看不到进度。其次, 外观被完全阻止 ,直到上传完成。但要求是,在上传进行之前,用户应可以自由使用Outlook for ex。检查其他邮件,发送其他邮件,等等。

调用方法

    ProgressForm.Show()
//**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

上传文件我有一个异步方法,该方法会执行以下操作

    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,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;
        }
    }

每次我使用cloudBlockBlob.PutBlockAsync上传一个区块时,我都会使用IProgress<int> progress更新进度栏。 但这无法正常工作,我看不到更新进度,Outlook也被阻止,并且无法在Outlook中执行任何其他操作。 我是VSTO和异步概念的新手,因此感谢所有潜在客户。 关于如何实现这一目标的任何想法?

非常感谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)