本文介绍了仅当调用Application.Run()WPF应用程序时,代码才会启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在class A中调用的方法,它是在class B中定义的:

I have a method which is invoked in class A and it is defined in class B:

class B{
      [STAThread]
        public static void ScanForAxisCameras() {
                DNSSDService service = new DNSSDService();
                DNSSDEventManager eventManager = new DNSSDEventManager();
                eventManager.ServiceFound += new _IDNSSDEvents_ServiceFoundEventHandler(eventManager_ServiceFound);
                DNSSDService browse = service.Browse(0, 0, "_axis-video._tcp", null, eventManager);
                Application.Run();//if not invoked everything above does not start
            }
}


class A{ ...before invoking..... B.ScanForAxisCameras(); ....after invoking....}

仅当我调用Application.Run()时,class B中的代码才能启动"/起作用. 但这会导致class A ....after invoking....方法中的所有代码都不起作用.如何处理它,使其不会冻结应用程序?

The code in class B "starts"/works only if I invoke Application.Run(). But it causes that all the code in class A ....after invoking.... method does not work. How to handle it so it will not freeze the application?

编辑:class Aclass MainWindow.xaml.cs.这是WPF应用程序.

the class A is class MainWindow.xaml.cs. It is WPF application.

public partial class MainWindow : Window {

        public MainWindow() {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
            createGUI();
        }

        private void createGUI() {

            LocalNetworkScanner.ScanForAxisCameras();//when there is no Application.Run()  ScanForAxisCameras() does not work.
}
}

推荐答案

在其上调用ScanForAxisCameras()的WPF UI线程已经具有消息循环.我相信您的代码存在的问题是,您在ScanForAxisCameras 具有本地范围中创建的所有对象:

The WPF UI thread on which you call ScanForAxisCameras() already has a message loop. I believe the problem with your code is that all objects you create inside ScanForAxisCameras have the local scope:

public static void ScanForAxisCameras() {
        DNSSDService service = new DNSSDService();
        DNSSDEventManager eventManager = new DNSSDEventManager();
        eventManager.ServiceFound += new _IDNSSDEvents_ServiceFoundEventHandler(eventManager_ServiceFound);
        DNSSDService browse = service.Browse(0, 0, "_axis-video._tcp", null, eventManager);
        Application.Run();//if not invoked everything above does not start
}

在没有Application.Run()的情况下,ScanForAxisCameras完成后,您的对象(serviceeventManagerbrowse)可能会被破坏并最终确定.因此,您正在寻找的事件(例如ServiceFound)甚至可能没有被解雇的机会.

Without Application.Run(), your objects (service, eventManager, browse) may be getting destroyed and finalized as soon as ScanForAxisCameras finishes. So, the events you're looking for (like ServiceFound) may not even have a chance to get fired.

如果调用Application.Run(),则ScanForAxisCameras不会退出(至少要等到Application.Run()本身退出时).这样可以使您的对象保持活动状态和功能.

If you call Application.Run(), then ScanForAxisCameras doesn't exit (at least not until Application.Run() itself exits). That keeps your objects alive and functional.

尝试重构代码,以将对这些对象的引用保留在类的成员字段中(或在静态变量FWIW中).我相信应该可以解决问题.

Try refactoring your code to keep the references to these objects in member fields of your class (or in static variables, FWIW). I believe that should fix the problem.

附带说明,在这种情况下,[STAThread]属性没有任何意义(除非您将ScanForAxisCameras用作新线程的入口点-显然,这是并非如此).

On a side note, the [STAThread] attribute doesn't make sense in that context (unless you use ScanForAxisCameras as an entry point for a new thread - apparently, that's not the case here).

这篇关于仅当调用Application.Run()WPF应用程序时,代码才会启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:44