当进度条已满时,如何在visual c ++中打开新表单 [英] How open new form in visual c++ when progress bar is full

查看:68
本文介绍了当进度条已满时,如何在visual c ++中打开新表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当进度条已满时,如何从Form1打开Form2进度栏

谢谢

How do I make that progress bar from Form1 open Form2 when progress bar is full
Thanks

推荐答案

你不应该制作进度条控件打开Form2,如果这是你的意思。这不是它的责任 - 进度条显示进度,没有别的。考虑你想在某个时候用其他东西替换Progress Bar然后你还需要重新实现Form2的开放(也许你不会替换它,但是把它作为解释为什么你应该考虑分离关注点)。



相反:



你的应用程序显然有一些漫长的过程,这就是你的原因显示进度条。在该过程完成的代码中,插入(或更好:触发 * )关闭/隐藏Form1和/或进度条并打开Form2的代码。



* :带有活动+活动处理程序



编辑:拼写错误
You shouldn't make the Progress Bar Control open Form2, if that is what you mean. It's not its responsibility - a Progress Bar shows progress, nothing else. Consider you want to replace the Progress Bar by something else sometime then you would also have to re-implement the opening of Form2 (maybe you wouldn't replace it ever but take it as an explanation why you should think about separation of concerns).

Instead:

You obviously have some lengthy process in your application, which is why you're showing the Progress Bar. At the point in the code where that process is finished, insert (or better: trigger *) the code for closing/hiding Form1 and/or the Progress Bar and opening Form2.

* : with an event + event handler

edit: typo


除了解决方案1.



即使触发了UI事件的窗口操作是非常可能的,它意味着逻辑错误的事件路由,混淆原因和后果。进度条,如果按照其预期设计使用,实际上意味着一些冗长的操作;从技术上讲,它应该意味着你有一些非UI线程正在完成它的工作。让我们将此线程和此线程上的所有事件(例如更新其进度百分比和完成时)表示为 Th 。让我们考虑所讨论事件之间的因果图:

In addition to Solution 1.

Even though triggering the window operation by a UI event is quite possible, it means logically wrong routing of events, mixing up causes and consequences. Progress bar, if used according to its intended design, really means some lengthy operation; technically it should mean that you have some non-UI thread doing its job. Let's denote this thread and all events on this thread (such as update of its progress percentage and completion) as "Th". Let's consider cause-consequence graph between the events in question:
Bad:
Th ────> progress bar ─────> show form

Good:
Th ────> progress bar
│
└──────> show form



解决方案1完美地解释了为什么我描述为坏的情况非常糟糕。更一般地说,您尝试以不遵循其逻辑的方式在技术上表达应用程序的行为。即使结果正式等效,它也会在维护中转化为相当大的困难。



现在,非UI线程应如何通知UI进行显示窗口(使用 Form.Show )并更新 ProgressBar.Value ?您不能从非UI线程调用与UI相关的任何内容。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



-SA


Solution 1 perfectly explains why the case I depicted as "Bad" is really bad. More generally, you try to technically express the behavior of the application in a way not following its logic. Even if the results are formally equivalent, it is translated into considerable hardship in maintenance.

Now, how the non-UI thread should notify UI to make is showing a window (using Form.Show) and updating the ProgressBar.Value? You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于当进度条已满时,如何在visual c ++中打开新表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆