【问题标题】:C# Advanced form "please wait"C# 高级表单“请稍候”
【发布时间】:2013-08-13 14:44:29
【问题描述】:

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

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

我还希望将其设置为在 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”是“请稍等”。

【问题讨论】:

  • Dolbra Jean Acronis :)
  • 好的,您已经说明了您的意图,并向我们展示了您迄今为止尝试过的代码。对此竖起大拇指。但是上面的代码怎么不适合你呢?它不完整吗?它会给你一个错误吗?我想我在这里代表每个人说话,因为我说我们不会将其复制/粘贴到 IDE 中,只是为了寻找甚至可能不存在的错误。
  • 在一个完全不相关的说明中,prosze_czekac 让我觉得你喜欢在线销售 prozac
  • 那么请编辑您的问题以包括您的疑问。因为现在这个问题已经接近投票结束了,很少有人会阅读 cmets 来查看您的问题更详细的信息。
  • @Renan Prosze czekac 表示请稍候

标签: c# multithreading winforms


【解决方案1】:

使用 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++)
    {
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 2015-01-17
    • 2012-09-12
    • 2021-06-21
    相关资源
    最近更新 更多