C#高级表格“请稍候”

时间:2013-08-13 14:44:30

标签: c# multithreading winforms

我的表单将运行一些可能需要一段时间才能执行的代码。当操作在后台运行时,我想显示“请稍候”消息。

我希望在表单中提供该消息,我可以从其他表单中控制其可见性以及文本。

我也希望将它设置为在Program.cs文件中启动。

我的代码,到目前为止:

namespace KAN
{
    public partial class prosze_czekac : Form
    {
        public prosze_czekac()
        {
            InitializeComponent();
        }

        private delegate void OffVisible();
        public void Wylacz()
        {
            if (this.InvokeRequired)
                this.Invoke(new OffVisible(Wylacz));
            else
                this.Visible = false;
        }

        delegate void SetTextCallback(string text);
        public void ZmienTekst(string text)
        {
            if (this.InvokeRequired)
            {
                //SetTextCallback d = new SetTextCallback(this.ZmienTekst);
                Invoke(new SetTextCallback(this.ZmienTekst), text);
                //Invoke(d, new object[] { text });
            }
            else
            {
                this.Visible = true;
                this.Text = text;
                this.lblKomunikat.Text = text;
                this.Update();
            }
        }
    }
}

我不知道如何运行表单,如何创建实例以及编辑文本。所有这些以任何形式,任何线程。 以上代码是否正确以及如何正确使用它?

我如何准备好“请等待”我想在初始课程(Program.cs)中打开它。在任何表格设计中使用它。 示例代码,不知道是否正确:

namespace KAN
{
    static class Program
    {
        public static prosze_czekac PleaseWait;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>        
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Thread thread = new Thread(new ThreadStart(PleaseWait.Show());

            PleaseWait.ZmienTekst("Please wait... Running the program");

            // long operation

            PleaseWait.Wylacz();

            Application.Run(new main());
        }

    }
}

namespace KAN
{
    public partial class main: Form
    {
        public main()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            // examples of long task in another form
            for (int i = 0; i < 5; i++)
            {
                Program.PleaseWait.ZmienTekst((i + 1).ToString());
                System.Threading.Thread.Sleep(1000);
            }

            Program.PleaseWait.Wylacz();
        }
    }
}

我第一次提问时,请耐心等待。

PS “Wylacz”是“退出”(无效),意在“隐藏”,以便每次你不启动表格。 “prosze_czekac”是“请稍等”。

1 个答案:

答案 0 :(得分:2)

使用BackgroundWorker。下面的代码假定,表单中有一个按钮“button1”,它执行worker,它在另一个线程上启动长时间运行的任务:

BackgroundWorker _worker;

// button click starts the execution of the lung running task on another thread
private void button1_Click(object sender, EventArgs e)
{
    label1.Visible = true; // show the label "please wait"
    _worker.RunWorkerAsync();
}

private void Form1_Load(object sender, EventArgs e)
{
    // initialize worker
    _worker = new BackgroundWorker();
    _worker.DoWork += new DoWorkEventHandler(worker_DoWork);
    _worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_worker_RunWorkerCompleted);
}

// executes when long running task has finished
void _worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // hide the label
    label1.Visible = false;
}

// is called by 'RunWorkerAsync' and executes the long running task on a different thread
void worker_DoWork(object sender, DoWorkEventArgs e)
{
    // long running task (just an example)
    for (int i = 0; i < 1000000000; i++)
    {
    }
}