2

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

我想在一个表单中包含该消息,我可以从其他表单控制它的可见性以及它的文本。

我还希望将其设置为在 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”是“exit”(无效),意思是“隐藏”,这样每次你都不启动表单。“prosze_czekac”是“请稍候”。

4

1 回答 1

2

使用 BackgroundWorker。以下代码假设,您的表单中有一个按钮“button1”,它执行工作程序,该工作程序在不同的线程上启动长时间运行的任务:

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++)
    {
    }
}
于 2013-08-13T14:59:13.497 回答