“请等待”窗口在单独的线程中运行

发布于 2025-02-07 06:46:23 字数 669 浏览 2 评论 0原文

多线程是我试图理解的概念。我只是尝试创建一个“工作请等待”窗口,其中包含有关进度的短信更新。

我似乎无法将消息发送回该线程以更新文本消息。

这是我启动线程的方式:

frmProcessing Fp;

Thread FpThread = new Thread(() => {
    Fp= new frmProcessing();
    Fp.Tital("Building Database");
    Fp.Working("Clearing Old Data...");
    Fp.ShowDialog();
});

FpThread.SetApartmentState(ApartmentState.STA);
FpThread.Start();   

尝试从线程中运行此函数,从而预先构造更新操作(原始线程):

Fp.Working("new message");

我尝试了:

Fp.Invoke(new Action(() => {
    Fp.Working("new message");
}));

我得到此错误:

'fp'在这里不是无效的。
CS0165:使用未分配的本地变量'fp'

Multi-threading is a concept I am attempting to understand. I Am simply trying to create a "Working Please Wait" window with a Text message update as to the progress.

I cannot seem to Send a Message back to that thread to update the text message.

Here is how I am Starting my Thread:

frmProcessing Fp;

Thread FpThread = new Thread(() => {
    Fp= new frmProcessing();
    Fp.Tital("Building Database");
    Fp.Working("Clearing Old Data...");
    Fp.ShowDialog();
});

FpThread.SetApartmentState(ApartmentState.STA);
FpThread.Start();   

Attempting to run this function from the Thread preforming the updating action(original thread):

Fp.Working("new message");

I attempted this:

Fp.Invoke(new Action(() => {
    Fp.Working("new message");
}));

I get this error:

'Fp' is not null here.
CS0165:Use of unassigned local variable 'fp'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霊感 2025-02-14 06:46:23

任何发现这篇文章的人。
解决了我的问题是3个部分。
将新信息设置为空。

frmProcessing Fp;

现在,

frmProcessing Fp=null;

使用BeginInVoke()在单独线程中运行的表单上函数。不是来自试图访问表单的功能。

public void Working(string Working) // function on form running in new thread
        {

          BeginInvoke(new Action(() =>  lblProcess.Text = Working));
        
        }

可以添加更多代码进行测试,以查看该消息是否来自另一个线程,以更加完整。

访问新线程中表单的方法和功能需要一些时间开始,因此在我的情况下,我延迟访问表单方法约1秒钟,

threading.sleep(1000);

可能有更好的方法来完成此任务。但这对我有用。

Anyone Finding this Post.
What fixed my problem was 3 parts.
setting the NewForm to Null.

frmProcessing Fp;

now

frmProcessing Fp=null;

using BeginInvoke() functions on the Form running in the separate thread. Not from the function trying to access the form.

public void Working(string Working) // function on form running in new thread
        {

          BeginInvoke(new Action(() =>  lblProcess.Text = Working));
        
        }

More Code can be added to test to see if the message is coming from another thread, to be more complete.

Access to the methods and functions of the form in the new thread takes some time to start, so in my case I delayed accessing the forms methods for about 1 second by

threading.sleep(1000);

There might be Better ways to accomplish this task. But it works for me.

Oo萌小芽oO 2025-02-14 06:46:23

根据我们的对话,这是实现这一结果的众多方法之一。 (换句话说,这就是我个人做这种事情的方式。)

public partial class MainForm : Form
{
    private void buttonModal_Click(object sender, EventArgs e)
    {
        var modal = new Progress();

        // Start a worker task to perform the
        // SQL updates and reset the application.
        Task.Run(() => 
        {
            for (int i = 0; i < 100; i++)
            {
                // Block on the thread doing the updating.
                // What you do here is up to you. This just
                // simulates work that takes some amount of time.
                Task.Delay(25).Wait();

                modal.SetProgress(i);
            }
            Invoke((MethodInvoker)delegate{ modal.Dispose(); });
        });

        // The modal dialog will prevent user interaction,
        // effectively locking user out until the update completes.
        modal.ShowDialog();
    }
}

”没有标题栏的进度对话框

在哪里...

public partial class Progress : Form
{
    public void SetProgress(int i)
    {
        if (IsHandleCreated)    // Avoid potential race condition
        {
            Invoke((MethodInvoker)delegate { progressBar1.Value = i; });
        }
    }

    // This is probably in Progress.Designer.cs file
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            // A debug message to confirm that this dialog is getting disposed.
            Debug.WriteLine("Progress dialog is now Disposing");
            if (components != null)
            {
                components.Dispose();
            }
        }
        base.Dispose(disposing);
    }
}

Based on our conversation, here's one of many ways achieve that outcome. (In other words, this is how I personally do this kind of thing.)

public partial class MainForm : Form
{
    private void buttonModal_Click(object sender, EventArgs e)
    {
        var modal = new Progress();

        // Start a worker task to perform the
        // SQL updates and reset the application.
        Task.Run(() => 
        {
            for (int i = 0; i < 100; i++)
            {
                // Block on the thread doing the updating.
                // What you do here is up to you. This just
                // simulates work that takes some amount of time.
                Task.Delay(25).Wait();

                modal.SetProgress(i);
            }
            Invoke((MethodInvoker)delegate{ modal.Dispose(); });
        });

        // The modal dialog will prevent user interaction,
        // effectively locking user out until the update completes.
        modal.ShowDialog();
    }
}

Progress dialog with no title bar

where...

public partial class Progress : Form
{
    public void SetProgress(int i)
    {
        if (IsHandleCreated)    // Avoid potential race condition
        {
            Invoke((MethodInvoker)delegate { progressBar1.Value = i; });
        }
    }

    // This is probably in Progress.Designer.cs file
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            // A debug message to confirm that this dialog is getting disposed.
            Debug.WriteLine("Progress dialog is now Disposing");
            if (components != null)
            {
                components.Dispose();
            }
        }
        base.Dispose(disposing);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文