未知处理时间的进度条

发布于 2024-11-15 13:19:14 字数 88 浏览 3 评论 0原文

我正在开发启动/停止/重新启动 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乖乖兔^ω^ 2024-11-22 13:19:14

当您不知道需要多长时间时,您就无法展示有意义的进展。您无法知道,服务启动需要 1 到 30 秒。您所能做的就是向用户显示“我没有死,正在努力”的指示符。 ProgressBar 支持,将 Style 属性设置为“Marquee”。

您还需要在工作线程中启动该服务,以避免 UI 冻结。最好使用BackgroundWorker 来完成。让它看起来类似于这样:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        ServiceProgressBar.Style = ProgressBarStyle.Marquee;
        ServiceProgressBar.Visible = false;
    }

    private void StartButton_Click(object sender, EventArgs e) {
        this.StartButton.Enabled = false;
        this.ServiceProgressBar.Visible = true;
        this.backgroundWorker1.RunWorkerAsync("foo");
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
        var ctl = new ServiceController((string)e.Argument);
        ctl.Start();
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
        this.StartButton.Enabled = true;
        this.ServiceProgressBar.Visible = false;
        if (e.Error != null) {
            MessageBox.Show(e.Error.ToString(), "Could not start service");
        }
    }

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:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        ServiceProgressBar.Style = ProgressBarStyle.Marquee;
        ServiceProgressBar.Visible = false;
    }

    private void StartButton_Click(object sender, EventArgs e) {
        this.StartButton.Enabled = false;
        this.ServiceProgressBar.Visible = true;
        this.backgroundWorker1.RunWorkerAsync("foo");
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
        var ctl = new ServiceController((string)e.Argument);
        ctl.Start();
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
        this.StartButton.Enabled = true;
        this.ServiceProgressBar.Visible = false;
        if (e.Error != null) {
            MessageBox.Show(e.Error.ToString(), "Could not start service");
        }
    }
奢欲 2024-11-22 13:19:14

您必须将启动/停止/重新启动进度分成小部分,并在部分完成后设置进度条。
对于即时更新,您需要进入正在执行的方法以获取有关其状态的反馈。

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.

小ぇ时光︴ 2024-11-22 13:19:14

您的意思是您想要启动/重新启动/停止多个服务,并希望进度条指示“您已处理要启动/重新启动/停止的服务列表的程度”吗?你可以这样做:

progressBar.Maximum = listOfServicesToStart.Count;
progressBar.Value = 0;

for (int i = 0; i < listOfServicesToStart.Count; i++)
{
    // Start service listOfServicesToStart[i]
    progressBar.Value = i;
    Application.DoEvents();
}

如果你打算可视化服务的启动过程:我想你不能做得很好。 Windows 中的服务管理单元似乎要做的是:

  1. 它尝试启动/重新启动/停止服务
  2. 时间为 1 秒,以查看服务是否已进入相应状态
  3. 它调用 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:

progressBar.Maximum = listOfServicesToStart.Count;
progressBar.Value = 0;

for (int i = 0; i < listOfServicesToStart.Count; i++)
{
    // Start service listOfServicesToStart[i]
    progressBar.Value = i;
    Application.DoEvents();
}

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:

  1. It tries to start/restart/stop the service
  2. It calls ServiceController.WaitForStatus with a 1sec timeout to see whether the service has entered the respective state
  3. Increases the progress bar value by one and goes to 2. until a timeout is detected (you need to find a reasonable number of seconds to wait for the service to enter the desired state)

This seems to be the only way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文