简体   繁体   English

如何在C#WinForms应用程序中设计工作流程?

[英]How to design the workflow in a C# WinForms application?

this might be a very general question - but I'm quite new to C#. 这可能是一个非常普遍的问题 - 但我对C#很新。 Can you please explain this to me or give me a good link to an example or explanation. 你可以向我解释这个问题,或者给我一个例子或解释的良好链接。

I want to design a C# Winform application. 我想设计一个C#Winform应用程序。 The application has a main form, which collects user input when needed. 该应用程序有一个主窗体,可在需要时收集用户输入。 Together with the UI a complex an long running calculation algorithm is developed. 与UI一起开发了一种复杂的长期运行计算算法。 Start of calculation is triggered from a button on the main form. 从主窗体上的按钮触发计算开始。 (CaculateClass.Start()) (CaculateClass.Start())

Question 1: At some point in time deep in the method call stack of the CaculateClass the CaculateClass detects that it needs further input from the user to continue the calculation. 问题1:在CaculateClass的方法调用堆栈深处的某个时间点,CaculateClass检测到它需要来自用户的进一步输入以继续计算。

In ancient C++ console application times one would have done something like this: cout << "Ask question"; 在古老的C ++控制台应用程序时代,人们会做到这样的事情:cout <<“Ask question”; cin >> answer; cin >>回答;

How is this done in C# with winforms, to pass control to the UI from somewhere deep in the call stack, get input and return to the place where the calculation was interupted? 如何在带有winforms的C#中完成,将控制权从调用堆栈深处的某个位置传递给UI,获取输入并返回到计算中断的位置? Or has the CaculateClass to be designed somehow completely different? 或者CaculateClass的设计是否完全不同?

Question 2 To avoid freezing of the UI a lot of articles recommend to put such long running calculations into another thread - eg by using BackgroundWorker() 问题2为避免冻结UI,许多文章建议将这种长时间运行的计算放入另一个线程 - 例如使用BackgroundWorker()

If I let the CaculateClass.Start() be calculated by a BackgroundWorker -> How does the collecting user input work then? 如果我让背景工作者计算CaculateClass.Start() - >收集用户输入如何工作呢?

Thanks for any help, CS 谢谢你的帮助,CS

How is this done in C# with winforms, to pass control to the UI from somewhere deep in the call stack, get input and return to the place where the calculation was interupted? 如何在带有winforms的C#中完成,将控制权从调用堆栈深处的某个位置传递给UI,获取输入并返回到计算中断的位置? Or has the CaculateClass to be designed somehow completely different? 或者CaculateClass的设计是否完全不同?

It's probably not very language specific, but you'd want to decouple things. 它可能不是特定于语言,但你想要解耦。 Get your inputs from a the form, act on it in a delegated class for example. 从表单中获取输入,例如在委派的类中对其进行操作。 Once your delegated class is ready and has results to show, you update your UI. 在您的委托类准备就绪并显示结果后,您将更新UI。

Question 2 To avoid freezing of the UI a lot of articles recommend to put such long running calculations into another thread - eg by using BackgroundWorker() 问题2为避免冻结UI,许多文章建议将这种长时间运行的计算放入另一个线程 - 例如使用BackgroundWorker()

As for the responsiveness, yes a background thread would work fine there. 至于响应性,是的,后台线程在那里可以正常工作。 Collecting input does not differ, you just pass the inputs to an object and that object calculates stuff for you. 收集输入没有区别,您只需将输入传递给对象,该对象就会为您计算内容。 It a matter of a design choice to do that on the same thread or a different thread. 在同一个线程或不同的线程上进行设计选择是一个问题。 The difference comes when posting the results back to the main thread. 将结果发布回主线程时会出现差异。 Since you should not (can not) change your user controls on another thread than the main thread, you would need to Invoke the results back to the Main thread. 由于您不应该(不能)在主线程之外的其他线程上更改用户控件,因此您需要将结果Invoke回主线程。 More on this topic can be found on MSDN . 有关此主题的更多信息,请访问MSDN

A general remark on a general question :) 关于一般问题的一般评论:)

Try to keep the UI stuff as thin as possible. 尽量保持UI内容尽可能薄。 It's responsible to visualize things, and get user intput. 它负责可视化事物并获得用户输入。 Google for MVC (model view controller) - a well known GUI design pattern. Google for MVC (模型视图控制器) - 一种众所周知的GUI设计模式。 Or have a look at MVVM (model view view model) - a somewhat more modern GUI design pattern. 或者看看MVVM (模型视图视图模型) - 一种更现代的GUI设计模式。 Both have a lot of best practices and ways to keep your design decoupled. 两者都有很多最佳实践和方法可以使您的设计脱钩。

You should have a look at Continuation Tasks: http://msdn.microsoft.com/en-us/library/ee372288.aspx 您应该看一下Continuation Tasks: http//msdn.microsoft.com/en-us/library/ee372288.aspx

Tasks are the new programming paradigm for performing background work, they are preferred over BackgroundWorker. 任务是执行后台工作的新编程范例,它们优先于BackgroundWorker。

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

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