通过方法调用者调用表单时无法显示表单吗? [英] I can not show form when I call it by Method Invoker?

查看:74
本文介绍了通过方法调用者调用表单时无法显示表单吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void ShowForm2()
{
    // Declare my form
    Form2 a = new Form2();
    // Show form
    a.Show();
}

private void mnuSystem_Click(object sender, EventArgs e)
{
    _StatusProgressBar.ProgressBar.Show();

    // Method invoker
    MethodInvoker ink = new MethodInvoker(ShowForm2);
    IAsyncResult a = ink.BeginInvoke(null, null);

    _StatusProgressBar.ProgressBar.Value = 1;
    while (!a.IsCompleted)
    {
        if (_StatusProgressBar.ProgressBar.Value == 100)
        {
            _StatusProgressBar.ProgressBar.Value = 1;
        }
        else
        {
            _StatusProgressBar.ProgressBar.Value += 1;
            System.Threading.Thread.Sleep(10);
        }
    }
    ink.EndInvoke(a);
    _StatusProgressBar.ProgressBar.Hide();
}


当我像上面那样调用Form2时,我无法显示它.
请帮帮我.
请告诉我为什么它错了吗?


I can not show Form2 when I call it like above.
Please help me.
Please tell me why it''s wrong?

推荐答案

这是一堆上帝知道的东西.您的代码说应该从单独的线程调用Invoker,但是同一代码还说您正在更新UI控件(ProgressBar),这意味着它只能在UI线程上,这将消除对Invoker的需求.那么,是什么呢?
This is a pile of God knows what. Your code says that the Invoker should be called from a seperate thread, but the same code also says that you''re updating UI controls (ProgressBar), meaning it can only be on the UI thread, which would eliminate the need for the Invoker. So, which is it?


如果您已经在UI线程上尝试在UI线程上调用某些东西,并且在下级代码完成之前不继续操作,则该程序将会永远阻塞,因为主代码阻止了UI线程为下级代​​码释放.
If you try to invoke something on the UI thread while you are already on the UI thread and you don''t continue until that subordinate code completes, the program will block forever since the main code is preventing the UI thread from being freed up for the subordinate code.


这篇关于通过方法调用者调用表单时无法显示表单吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆