
[英]Displaying a Modal Window from a C# COM component that uses async called by a C++ MFC application
[英]C# Async call from C++ MFC app blocks gui
同事。 我有使用 C# 插件扩展的旧 MFC 应用程序 (VC6)。 一切运作良好。 但是现在我想在 C# 插件中使用异步函数,但是我有一个问题。 Windows 检测到 GUI 块的问题。 这是一张图片:
C# 代码是 FileSystemWatcher 和 Timer 对:所以我监视一些文件,如果文件未出现在定义的超时内,则返回。 如果我在没有 async\await 对的情况下使用 C# 代码,则一切正常。 有趣的是:当我使用异步 C# 代码在后台也可以正常工作。 因此,如果将创建文件并且我按下“重试”按钮,那么我将看到 C# 成功执行(带有消息的窗口,数据库结果)。
这是我的 C# function:
private static Task<(string FileName, string PublishLogCode)> CallStoredProcedure(string repoId, string guid)
{
var tcs = new TaskCompletionSource<(string FileName, string PublishLogCode)>();
var globalReportFolder = SP_CONFIG.GetLimsConfigStr("GLOBAL_REPORT_FOLDER");
var runonce = new System.Timers.Timer() { Interval = 600000, AutoReset = false, Enabled = false }; // 30 seconds = 30000 //10 min = 600000
runonce.Elapsed += (s, a) =>
tcs.SetResult((FileName: "", PublishLogCode: ""));
runonce.Start();
if (!Directory.Exists(globalReportFolder)) //fileIOPermission.AllFiles == FileIOPermissionAccess.NoAccess)
return Task.FromResult((FileName: $"<Error> Folder '{globalReportFolder}' not found", PublishLogCode: ""));
try
{
var watcher = new FileSystemWatcher();
watcher.InternalBufferSize = 65535; // to process many files simultaneously
watcher.Path = globalReportFolder;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
//** filter files matching LabId
watcher.Filter = $"{repoId}*.pdf";
watcher.EnableRaisingEvents = true;
watcher.Created += (s, e) =>
{
runonce.Stop();
var reportFileName = $"{globalReportFolder}{e.Name}";
tcs.SetResult(reportFileName.Contains(repoId) ? (FileName: reportFileName, PublishLogCode: InsertPublishLog(repoId, "COFA")) : ("", ""));
watcher.Dispose();
};
}
catch (Exception e)
{
SP_CONFIG.WriteAuditLog($"GlobalReportRunner.StartFolderWatch", $"{e.Message}");
tcs.SetException(e);
}
return tcs.Task;
}
这是 function 调用:
var (reportFileName, publishLogCode) = await CallStoredProcedure(xp?.RepoId, xp?.Guid).ConfigureAwait(continueOnCapturedContext: false);
我试图将 function 主体包装到一个线程中:它也在测试环境中工作,但在真正的 c++ 应用程序中不起作用。
这个问题不是很紧急,因为我有工作“非任务”代码,这个代码不是那么优雅,但它正在工作。 然而,有趣的是要弄清楚:为什么?
所以,感谢任何建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.