简体   繁体   English

在WPF App.xaml.cs文件中更改Main方法

[英]Changing Main method in WPF App.xaml.cs file

Currently i changed my Main method in my WPF project like that: 目前,我在WPF项目中更改了Main方法,如下所示:

public static void Main() 
{
    // Call Log4net configuration to configure all Appenders
    log4net.Config.XmlConfigurator.Configure();
    // Check if Application is already running, if it is running - Kill
    if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Length > 1) System.Diagnostics.Process.GetCurrentProcess().Kill();

    Controller.App app = new Controller.App();        
    app.InitializeComponent();
    app.Run();
}

I added a method call to configure my Log4Net framework and i check if an instance of my application is running already. 我添加了一个方法调用来配置Log4Net框架,并检查我的应用程序实例是否已在运行。

Previously i had this methods in my MainWindow.CS file -> MainWindow constructor. 以前,我在MainWindow.CS文件-> MainWindow构造函数中有此方法。

Now im curious if it is common/good practice to do verifications like this two method calls in the Main method of the application or if it should be done in the MainWindow.CS as i did before. 现在我很好奇,是否常见/优良作法是在应用程序的Main方法中执行像这两个方法调用一样的验证,还是应该像以前一样在MainWindow.CS中进行验证。 Are there any concerns to do this in the Main method. 在Main方法中是否有任何需要这样做的问题。 so far everything works fine in my application. 到目前为止,在我的应用程序中一切正常。

Yes this is perfectly valid and I've seen this more than once. 是的,这是完全正确的,而且我已经不止一次看到了。

Setting up your logging infrastructure in the main allows you to log any issue that could occur when running the application, eg if you have an error in your App.xaml . 在主目录中设置日志记录基础结构可让您记录运行应用程序时可能发生的任何问题,例如,如果App.xaml有错误。

Moreover you avoid launching the WPF application for nothing if you must close it immediately, though in the general case should not be an issue. 此外,即使必须立即关闭WPF应用程序,也不必立即启动它,尽管通常情况下这不是问题。

As for the way you detect if your application is already running and stop it, well this is quite "original". 至于检测您的应用程序是否已经运行并停止它的方式,这很“原始”。 :) The standard way of doing this is by using a global system mutex and if present then jumping to the end of the Main . :)这样做的标准方法是使用全局系统互斥量 ,如果存在,则跳转到Main的末尾。

I've found that rather than messing with the underlying internals of a WPF Application by changing Main() it's better to rather override the OnStartup() method in the App.xaml.cs . 我发现,与其通过更改Main()来弄乱WPF应用程序的基础内部,不如覆盖App.xaml.csOnStartup()方法更好。

I call log4net's Configure() there and perform a number of other application specific configuration operations there before allowing the main window to be shown. 我在这里调用log4net的Configure() ,并在允许显示主窗口之前在其中执行许多其他特定于应用程序的配置操作。

I've found that this avoids issues with the underlying wiring that makes up a WPF application and leads to less issues in the long run. 我发现,这避免了构成WPF应用程序的底层布线的问题,并且从长远来看可以减少问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM