如何显示等待进度条或“处理....“ [英] How to display waiting progress bar or " processing.... "

查看:128
本文介绍了如何显示等待进度条或“处理....“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请向我解释,我想在显示等待进度条的同时做一些过程,但我很困惑在哪里以及如何做到这一点?请让我知道以下代码实际上是如何工作的,以便我可以实现它。



i想要显示等待消息,除非所需的过程已经完成。



in下面的代码我创建了两个表单,第一个表单作为一些虚拟数据处理,另一个表单只包含一个标签和一个进度条。当我单击第一个表单上的按钮时,第二个表单将显示为等待对话框。它的工作正常,但我只是想知道下面的代码是如何工作的,这样我就可以为我的目的修改它。



我尝试了什么:



Kindly Explain it to me, i want to do some process while showing waiting progress bar but i am confused where and how to do it? Please let me know how the following code actually works, so that i could implement it.

i want to display a waiting message to the use unless the required process has been completed.

in the below code i have created two forms, the first form as some dummy data processing and the other form contains only a label and a progress bar. when i click the button on the first form the second from appears as a waiting dialogue box. its working properly but i just want to know how the below code is working so that i could modify it for my purpose.

What I have tried:

public Form1()
        {
            InitializeComponent();
        }
        void SaveData()
        {
            for (int i = 0; i <= 5; i++)
            {
               // Thread.Sleep(500);// simulator
            }
            
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            using (Waitform objWaitForm = new Waitform(SaveData))
            {
                objWaitForm.ShowDialog(this);
            }
        }

//End From1///

 public Action Worker { get; set; }
        public Waitform(Action worker)
        {
            InitializeComponent();
            if (worker == null)
                throw new ArgumentNullException();
            Worker = worker;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Task.Factory.StartNew(Worker).ContinueWith(t => { this.Close(); }, TaskScheduler.FromCurrentSynchronizationContext());
        }

///End Waitform//

推荐答案

如果您正在考虑使用Tasks进行异步操作,下面是一个如何操作的示例。



1.进度表。有一个标签提供进度消息 - 您可以将其换成进度条。代码隐藏处理更新:

If you're looking at using Tasks for asynchronous operations, here is an example of how to do it.

1. Progress Form. Has a label to provide the progress messages - you can swap this out for a progress bar. Code-behind to handle the updates:
public partial class ProgressForm: Form
{
    public ProgressForm()
    {
        InitializeComponent();
    }

    public void SetMessage(string text)
    {
        labMessage.Text = text;
    }
}



2.具有长期运行任务的调用表单:


2. The calling form with the long running task:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private async void button1_Click(object sender, EventArgs e)
    {
        ShowProgressWindow();

        await UnitOfWork().ConfigureAwait(false);

        CloseProgressWindow();

    }

    private void CloseProgressWindow()
    {
        if (InvokeRequired)
        {
            Invoke((MethodInvoker)(CloseProgressWindow));
        }
        else
        {
            progress.Hide();
            button1.Enabled = true;
        }
    }

    private void ShowProgressWindow()
    {
        // disable button at start of process
        button1.Enabled = false;
        progress.Show(this);
    }

    private readonly ProgressForm progress = new ProgressForm();

    private readonly List<string> UpdateMessages = new List<string>
    {
        "Loading the fodder",
        "Watering the seeds",
        "Feeding the goats",
        "Clipping the flowers",
        "Smelling the roses",
        "Closing the doors",
        "Saving the changes",
        "Done!"
    };

    private Task<bool> UnitOfWork()
    {
        var tcs = new TaskCompletionSource<bool>();

        Task.Run(async () =>
        {
            for (int i = 0; i < 8; i++)
            {
                ProgressUpdate(UpdateMessages[i]);
                // Do long running synchronous work here...
                await Task.Delay(1000).ConfigureAwait(false); // simulate a 1 second task
            }

            // signal success!
            tcs.SetResult(true);

        }).ConfigureAwait(false);

        return tcs.Task;
    }

    private void ProgressUpdate(string updateMessage)
    {
        if (InvokeRequired)
            Invoke((MethodInvoker)(() => ProgressUpdate(updateMessage)));
        else
            progress.SetMessage(updateMessage);
    }
}


这篇关于如何显示等待进度条或“处理....“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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