加载winform以进行通知的问题

时间:2014-02-12 21:02:40

标签: c# winforms forms

我创建了一个小型C#应用程序,用于从事件日志中读取一些数据,然后显示一些结果。

程序执行基本的SQL查询以获取其初始数据(如果查询数天的数据可能需要一些时间),然后在显示结果之前进行一些处理。

我要做的是当按下“提交”按钮时,会显示一条消息,指出需要一些时间来检索和处理数据。因此,当按下提交按钮时,我创建一个带有消息的表单并显示它。

这是来自提交按钮和相关方法的代码:

private void btnSubmit_Click(object sender, EventArgs e)
{
    DisplayCustomMessageBox("Please Wait");

    ProcessRequest();

    HideCustomMessageBox();
}


private void DisplayCustomMessageBox(string title)
{
    CustomMessageBox = new frm_Message { Text = title };
    CustomMessageBox.SetText("Please wait ");
    CustomMessageBox.Show();
    this.Enabled = false;
}

private void HideCustomMessageBox()
{
    this.Enabled = true;
    CustomMessageBox.Close();
}

发生的事情是我有表格显示但表格中的文字从不显示。如果我注释掉HideCustomMessageBox方法,则在ProcessRequest方法完成之前,表单将显示没有文本。然后表单将最终显示文本。

我认为它存在某种时间问题,但我不确定如何解决它。

提前致谢。

2 个答案:

答案 0 :(得分:1)

这里有一些线程代码可以帮助您入门。

private void btnSubmit_Click(object sender, EventArgs e)
{
    DisplayCustomMessageBox("Please Wait");

    Thread t = new Thread(()=>
    {
        ProcessRequest();
        this.BeginInvoke(new Eventhandler((s,ee)=>{
            HideCustomMessageBox();
        }));
    });
    t.Start();
}

答案 1 :(得分:0)

我会使用模式对话框(Form.ShowDialog)和Task.Run在后​​台线程上运行ProcessRequestAsync/await在实施此功能时非常方便。

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsForms_21739538
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            // test the btnSubmit_Click
            this.Load += async (s, e) =>
            {
                await Task.Delay(2000);
                btnSubmit_Click(this, EventArgs.Empty);
            };
        }

        private async void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                // show the "wait" dialog asynchronously
                var dialog = await DisplayCustomMessageBox("Please Wait");

                // do the work on a pool thread
                await Task.Run(() =>
                    ProcessRequest());

                // close the dialog    
                dialog.Item1.Close();

                // make sure the dialog has shut down
                await dialog.Item2;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // show a modal dialog asynchrnously
        private async Task<Tuple<Form, Task<DialogResult>>> DisplayCustomMessageBox(string title)
        {
            //CustomMessageBox = new frm_Message { Text = title };
            var CustomMessageBox = new Form();

            CustomMessageBox.Text = "Please wait ";

            var tcs = new TaskCompletionSource<bool>();

            CustomMessageBox.Load += (s, e) =>
                tcs.TrySetResult(true);

            var dialogTask = Task.Factory.StartNew(
                ()=> CustomMessageBox.ShowDialog(),
                CancellationToken.None,
                TaskCreationOptions.None,
                TaskScheduler.FromCurrentSynchronizationContext());

            // await the dialog initialization
            await tcs.Task;

            return Tuple.Create(CustomMessageBox, dialogTask);
        }

        void ProcessRequest()
        {
            // simulate some work
            Thread.Sleep(2000);
        }
    }
}