未知处理时间的进度条
我正在开发启动/停止/重新启动 Windows 服务的 winform(c#)。我想放置一个进度条,直到操作完成。我是 .net 编程新手。请帮助我实现这一目标。
I am developing winform(c#) that start/stop/restart windows services. I want to put a progress bar till the action is completed. I am new to .net programming. Kindly help me on to achieve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您不知道需要多长时间时,您就无法展示有意义的进展。您无法知道,服务启动需要 1 到 30 秒。您所能做的就是向用户显示“我没有死,正在努力”的指示符。 ProgressBar 支持,将 Style 属性设置为“Marquee”。
您还需要在工作线程中启动该服务,以避免 UI 冻结。最好使用BackgroundWorker 来完成。让它看起来类似于这样:
You cannot show meaningful progress when you don't know how long it takes. You can't know, services take anywhere from 1 to 30 seconds to start. All you can do is show a "I'm not dead, working on it" indicator to the user. ProgressBar supports that, set the Style property to "Marquee".
You'll also need to start the service in a worker thread to avoid your UI from freezing. That's best done with a BackgroundWorker. Make it look similar to this:
您必须将启动/停止/重新启动进度分成小部分,并在部分完成后设置进度条。
对于即时更新,您需要进入正在执行的方法以获取有关其状态的反馈。
You must divide up the start/stop/restart progress in small parts and after the part is finished you set the progress bar.
For an instant update you need to get into the methods you are executing to get feedback about its status.
您的意思是您想要启动/重新启动/停止多个服务,并希望进度条指示“您已处理要启动/重新启动/停止的服务列表的程度”吗?你可以这样做:
如果你打算可视化服务的启动过程:我想你不能做得很好。 Windows 中的服务管理单元似乎要做的是:
ServiceController.WaitForStatus
,超时 进度条值减一并转到2。直到检测到超时(您需要找到合理的秒数来等待服务进入所需的状态)这似乎是唯一的方法。
Do you mean that you want to start/restart/stop more than one service and want the progress bar to indicate "how far you've processed the list of services to be started/restarted/stopped"? You could do something like:
If you are planning to visualize the starting process of a service: I guess you can't do it nicely. What the Services snap-in in Windows seems to do is:
ServiceController.WaitForStatus
with a 1sec timeout to see whether the service has entered the respective stateThis seems to be the only way.