的WinForms:实施问题要问我有独立的UI我BLL层的运行?我有、要问、独立、问题

由网友(ζ成全了舍得)分享简介:我试图做一个Windows窗体应用程序中的MVP风格 - 没有做了很多与线程之前 - 我得到的所有困惑I am trying to do a Windows Forms application in an MVP style and - not having done much with threading befo...

我试图做一个Windows窗体应用程序中的MVP风格 - 没有做了很多与线程之前 - 我得到的所有困惑

I am trying to do a Windows Forms application in an MVP style and - not having done much with threading before - am getting all confused.

我的UI是一套非常简单的形式。每个形式实现了接口,并包含对这些生活在业务逻辑层,反之亦然调停类。 因此,作为简化框图如下:

My UI is a set of very simple forms. Each of the forms implements an interface and contains a reference to a mediator class which lives in the Business Logic Layer and vice versa. So as simplified diagram looks like this:

CheckInForm : ICheckIn                      <-------> CheckInMediator : ICheckInMediator
----------------------------------------------------------------------------------------
CheckInForm.Show()                          <--------
                                            --------> AttemptCheckIn(CheckInInfo)
CheckInForm.DisplayCheckInInfo(DisplayInfo) <-------- 
                                            --------> CompleteCheckIn(AdditionalCheckInInfo)
  PleaseWaitDialog.Show()                   <--------
  PleaseWaitDialog.Close()                  <--------
CheckInForm.Close()                         <--------

正如你所看到的,调解员类控制用户界面,告诉它时显示的数据,启动,关闭等他们甚至表示,当应出现一个模式对话框,当它应该关闭(即上述PleaseWaitDialog)的用户界面做唯一的事情就是在屏幕上显示的数据和继电器的输入回调解员。

As you can see, the mediator classes control the UI, telling it when to display data, start up, close, etc. They even signify when a modal dialog should appear and when it should close (ie the PleaseWaitDialog above) The only thing the UI does is show data on the screen and relay input back to the mediator.

这个架构是好的,解耦,并已超容易测试和原型。现在,我把他们放在一起但是我开始遇到线程问题。例如,如果我想我的PleaseWaitDialog显示为模式窗体(使用的ShowDialog())结束,直到调解员控制的计时器5秒(请记住,这是一个简化的)我会得到一个跨线程错误的CheckInForm如果我叫PleaseWaitDialog.Close()从计时器的回调。与此类似,如果我有一个模式对话框街区与UI交互的用户我不希望阻止在业务层的活动,除非我指定,否则(如用一个确认对话框)。

This architecture is nice and decoupled and has been super-easy to test and prototype. Now that I'm putting it all together however I'm starting to run into threading issues. For example, if I want my PleaseWaitDialog to appear as a modal form (using ShowDialog()) over the CheckInForm until a timer controlled by the mediator counts out 5 seconds (remember, this is a simplification) I will get a cross-threading error if I call PleaseWaitDialog.Close() from the timer's callback. In a similar vein, if I have a modal dialog block the user from interacting with the UI I don't want that to block activity in the business layer unless I specify otherwise (such as with a confirmation dialog).

我想我会喜欢做的事是运行在一个完全独立的线程的调解员和业务逻辑在主线程和UI,我的第一个问题是这是否有道理办?

What I think I would like to do is to run the mediators and business logic on the main thread and the UI on a completely separate thread and my first question is does this make sense to do?

我的第二个问题是,我怎么做这样的事情在一个单独的线程类中运行?而且我怎么会有两个沟通?我通过.NET线程的阅读使我的方式,但我有一个最后期限,有些例子如何有一个班在主线程产生一个线程包含的UI,让他们交谈的对象彼此能真正的帮助。

My second question is, how do I do something like have a class run in a separate thread? And how do I have the two communicate? I am making my way through the reading on .NET threading but I have a deadline and some examples for how to have a class on the main thread spawn a thread containing the UI and have them objects talk to each other could really help.

推荐答案

你有没有调查过的 BackgroundWorker的类?这是伟大的做了很多简化处理的背景类型的程序,并给出了可以listned做有你的GUI显示进度事件。

Have you looked into the BackgroundWorker class? It's great for doing a lot of the simplified processing in the background type procedures and gives events that can be listned to do have your GUI display progress.

阅读全文

相关推荐

最新文章