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

时间:2019-11-19 08:20:11

标签: revit-api revit

关于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;
    }

2 个答案:

答案 0 :(得分:0)

您从sender参数检索应用程序的调用可能会给您一个不同于最初预订DocumentSaved的实例的实例。因此,取消订阅无效。我建议您将最初订阅DocumentSaved的应用程序实例保存在全局变量中的某个位置,然后使用同一实例再次取消订阅。

答案 1 :(得分:0)

我尝试了您的建议,但该建议仍未解决。