在Winform中使用同步过程处理2个进度条的最佳方法是什么? [英] What is the best way to handle 2 progress bar with synchronous process in Winform?

查看:100
本文介绍了在Winform中使用同步过程处理2个进度条的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我必须通过自动化过程安装10个MSi设置(从XML文件中读取设置路径)&从UI(Winform)我必须通过2个进度条显示进度。 ProgressBar1显示进度为1比1 MSi设置& ProgressBar2显示整体10 MSi设置进度(我的意思是如果安装了1 MSi设置,那么ProgressBar2应显示10%)。



主要问题是我们如何管理第一个进度条1进度?因为这是系统过程。那么我如何管理progressbar1& ;;传递价值。





Suppose I have to install 10 MSi setup through automated process(setup path read from XML file) & from UI(Winform) I have to show progress through 2 progressbar. Where ProgressBar1 show progress of 1 by 1 MSi setup & ProgressBar2 show overall 10 MSi setup progress (I mean if 1 MSi setup will installed then ProgressBar2 should show 10%).

The main problem is how we manage first progressbar1 progress? Because it's system process. So how I manage progress of progressbar1 & pass value.


private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{

    //Intall selected products one by one
    foreach (var productpath in _pim.SelectedProducts)
    {
        using (var mutex = new System.Threading.Mutex(false, "mutex01"))
        {
            mutex.WaitOne();
            try
            {
                    // Start installation process of a selected product
                    Process.Start(productpath);

                    // Todo : calculation of a product installation %
                    backgroundWorker1.ReportProgress(1);
            }
            catch (System.Threading.AbandonedMutexException ex) // Current thread owns the mutex, and must release it.
            {
                Console.WriteLine("Exception on return from WaitOne." + "\r\n\tMessage: {0}", ex.Message);
            }
            finally
            {
                mutex.ReleaseMutex();
                _installedproductcount += 1;
            }
        }
    }
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
    label3.Text = e.ProgressPercentage.ToString() + "%";
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    progressBar1.Value = 0;
    label3.Text = "0";
}

推荐答案

如果我正确理解您的问题,进度条1将监控单个设置的进度。为此,您需要通过API与Windows Installer进行通信。看看这篇文章:包装Windows Installer 2.0 API [ ^ ]。文章本身有点旧,如果安装程序,您可能需要修改代码以使用更新的版本。例如,参考 https://msdn.microsoft.com/en-us /library/aa370573(v=vs.85).aspx [ ^ ]和 https://msdn.microsoft.com/en-us/library/aa368786(v = vs.85).aspx [ ^ ]。



从进度条2的角度来看,每次安装将占整体进度的10%,因此这应该相当容易处理。
If I understand your question correctly, the progress bar 1 would monitor the progress of a single setup. In order to do that you would need to communicate with the Windows Installer through an API. Have a look at this article: Wrapping the Windows Installer 2.0 API[^]. The article itself is a bit old sou you probably need to modify the code to use newer version if installer. Refer for example to https://msdn.microsoft.com/en-us/library/aa370573(v=vs.85).aspx[^] and https://msdn.microsoft.com/en-us/library/aa368786(v=vs.85).aspx[^].

From progress bar 2 point of view each install would represent 10% of the overall progress so this should be fairly easy to handle.


这篇关于在Winform中使用同步过程处理2个进度条的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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