如何在空闲事件中退订DocumentSaved事件并退出该功能?

问题描述 投票:0回答:2

我对Revit中的事件有问题。即使我在空闲事件中取消订阅DocumentSaved事件,程序的控制也不会超出DocumentSaved Event函数的范围,因此会无限期地调用DocumentSaved Event。

这里是代码段-

        Application application = sender as Application;
        if (!e.Document.IsFamilyDocument)
        {
            //selectedChoice = 3;
            UIApplication uIApplication = new UIApplication(application);
            ProjectSpecificInfo projectSpecificInfo = new ProjectSpecificInfo();
            AddinEntryPoint.saveStopWatch.Stop();
            AddinEntryPoint.mainSchema.projectSpecificInfo.TryGetValue(e.Document.PathName, out projectSpecificInfo);
            projectSpecificInfo.saveFileDuration = AddinEntryPoint.saveStopWatch.Elapsed.TotalSeconds;
            //if (needToExportTheDataAndCallAPI.TryGetValue(e.Document.PathName, out bool needToExport))
            //{
            //    needToExportTheDataAndCallAPI[e.Document.PathName] = true;
            //}
            //else
            //{
            //    needToExportTheDataAndCallAPI.Add(e.Document.PathName, true);
            //}

            if (!AddinEntryPoint.mainSchema.projectSpecificInfo.ContainsKey(e.Document.PathName))
            {
                AddinEntryPoint.mainSchema.projectSpecificInfo.Add(e.Document.PathName, projectSpecificInfo);
            }
            else
            {
                projectSpecificInfo = AddinEntryPoint.mainSchema.projectSpecificInfo[e.Document.PathName];
            }
            projectSpecificInfo.saveFileDuration = AddinEntryPoint.saveStopWatch.Elapsed.TotalSeconds;
            //projectSpecificInfo = AddinEntryPoint.ChangeProjectSpecificValues(uIApplication.ActiveUIDocument, projectSpecificInfo);
            //projectSpecificInfo.RevitVersion = application.VersionName;
            //projectSpecificInfo.SubRevitVersion = application.SubVersionNumber;
            AddinEntryPoint.mainSchema.projectSpecificInfo[e.Document.PathName] = projectSpecificInfo;


            if (!AddinEntryPoint.mainSchema.userSpecificInfo.ContainsKey(Environment.MachineName))
            {
                AddinEntryPoint.mainSchema.userSpecificInfo.Add(Environment.MachineName, AddinEntryPoint.mainSchema.projectSpecificInfo);
            }
            else
            {
                AddinEntryPoint.mainSchema.projectSpecificInfo = AddinEntryPoint.mainSchema.userSpecificInfo[Environment.MachineName];
            }

            //Utils.PopulateVisualizationDataToMap(uIApplication.ActiveUIDocument);

            if (!e.Document.IsWorkshared)
                Utils.ExportVisualizationDataToJsonAndCallAPI();
        }
        e.Dispose();
        application.DocumentSaved -= Application_DocumentSaved;
    }
revit-api revit
2个回答
0
投票
您从sender参数中检索应用程序的调用可能会给您一个不同于最初订阅DocumentSaved的实例的实例。因此,取消订阅无效。我建议您将最初订阅了DocumentSaved的应用程序实例保存在全局变量中的某个位置,然后使用同一实例再次取消订阅。

0
投票
我尝试了您的建议,但该建议仍未解决。
© www.soinside.com 2019 - 2024. All rights reserved.