简体   繁体   English

WinForms中的事件是否异步触发?

[英]Are events in WinForms fired asynchronously?

I recognize this may be a duplicate post, but I want to make sure I ask this question clearly and get an answer based on my wording. 我知道这可能是重复的帖子,但是我想确保我清楚地提出这个问题,并根据我的措辞得到答案。

I have a collection of forms which inherit from a common visual element: MainVisualForm. 我有一组从常见的可视元素继承的表单:MainVisualForm。 This element provides me a way to know when the form is advancing of stepping backwards. 这个元素为我提供了一种知道表单何时向后退的方式。 What form comes next in the sequence is dependent on user action. 序列的下一个形式取决于用户的操作。

I currently have this code for one such event as I am testing: 我目前正在测试的一个此类事件的代码如下:

form.OnNextForm += (f, ev) =>
            {
                Parameters.Vehicle = ((VehicleForm)f).SelectedVehicle;
                //FormStack.Push(Parameters.Vehicle == Vehicle.SUV
                //                ? new KeyValuePair<Type, IFormActionBehvaior>(typeof(EntertainmentForm), null)
                //                : new KeyValuePair<Type, IFormActionBehvaior>(typeof(ColorForm), null));
            };

This assignment is followed immediately by ShowDialog() which blocks the user until the Dialog form is closed. 此分配后紧接着是ShowDialog(),它会阻止用户,直到关闭Dialog窗体。

The question is: After the form closes does .NET wait for the EventHandler to complete before running the code which directly follows ShowDialog() or is the handler handled by a different thread? 问题是:窗体关闭后,.NET在运行紧跟ShowDialog()之后的代码之前,是否等待EventHandler完成,还是该处理程序由其他线程处理?

Thanks very much in advance 首先十分感谢

Winforms runs in a single thread - in fact you can't even access it from a second thread without running into trouble. Winforms在单个线程中运行-实际上,您甚至无法从第二个线程访问它而不会遇到麻烦。 Unless you create a thread yourself (or a BackgroundWorker or anything else that constitutes a thread), you'll only ever have one thread. 除非您自己创建一个线程(或BackgroundWorker或其他任何组成线程的线程),否则您将永远只有一个线程。

简单来说,.NET Winforms在单个线程下工作。

It waits for the event to complete. 它等待事件完成。 Events are really just method calls to methods defined somewhere else (aka delegates). 事件实际上只是对其他地方(也称为委托)定义的方法的方法调用。 After all those complete, the next bit of code after ShowDialog() will run. 完成所有这些操作后,将运行ShowDialog()之后的下一部分代码。

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

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