C#函数执行两次,但一次执行

时间:2020-09-09 06:34:09

标签: c# wpf

我正在使用AutoUpdater.Net,并在应用程序启动时开始检查(此方法运行良好)。再次从MainWindow。

这是我称之为的功能:

public void CheckUpdate()
{
    if (IsConnectedToInternet())
    {
        Assembly assembly = Assembly.GetExecutingAssembly();
        FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
        Update.InstalledVersion = new Version(fvi.FileVersion);
        Update.Synchronous = true;
        //comment the line below to show the AutoUpdater.NET forms
        Update.CheckForUpdateEvent += AutoUpdaterOnCheckForUpdateEvent;
        Update.Start("http:/myurl/");
    }
    else
        IminaDialog.ShowMessage("Update error", "It seems you're not connected to internet.", ModalWindowButtons.Ok);
}

函数AutoUpdateOnCheckForUpdateEvent执行两次。到达方法的最后一行,然后直接重新启动方法。来自加载或来自MainWindow的调用堆栈仍然相同。我从父类的MainWindow调用Instance。 我希望有人能帮助您并告诉我更多信息。

编辑

这是两次执行AutoUpdateOnCheckForUpdateEvent(每次CheckUpdate执行一次)的呼叫:

        public static WpfCommand CommandCheckUpdate
    {
        get 
        {
            if (_commandCheckUpdate == null) 
            {
                _commandCheckUpdate = new WpfCommand();
                _commandCheckUpdate.Executed += (sender, e) =>
                {
                    FrameworkController.Instance.CheckUpdate();
                };
            }
            return _commandCheckUpdate;
        }
    }

如果我仅呼叫FrameworkController.Instance,则CheckUpdate不会执行。 这就是在加载时(在FrameworkController内部)调用的方法:

public void Start(string[] mainArgs)
    {
        InitializeLog();

        if (!License.RegisterLicense(null))
            return;

        DefaultMenuItems.PopulateMenuService(MenuService.Instance);
        IminaUtils.Initialize(); // Initialize helper methods

        CheckUpdate();

        _firstConfigurationLoading = Configuration.Load();

        MainWindowViewModel.ClearDiagnostic();
        if (RightRepository.Instance.CurrentUser == null)
            MainWindowViewModel.OpenLogin();

        if (Configuration.ShutdownApp)
            return;

        ConnectConfiguration(mainArgs);
    }

0 个答案:

没有答案