English 中文(简体)
使用显示器(形)在另一个侧面上形成形式?
原标题:Use ShowDialog(form) where form is created on another thread?

我的表格像方言一样使用,以确保方言得到正确的父母使用以下方法:

this.ShowDialog(myForm);

问题在于,这种(我的方言表)和我的For都是在大街上制造的,在叫SudDialog时,会thrown倒一个相互接近的例外。

this.InvokeRequired = false
myForm.InvokeRequired = true

我如何处理这一问题? 我是否必须确保两者都是在同一个天线上建立的?

最佳回答

你们必须保证,所有形式都是在国祥之地上建立的。

因此,不是在你的背景线上打造表格,而是向法国宇宙航行局传达信息,告诉它要制作和展示表格。

问题回答

具有自我解释变量的样本代码:

var logicToInvokeInsideUIThread = new MethodInvoker(() =>
{
    // ...

    ShowDialog(this);

    // ...
};

if (InvokeRequired)
{
    Invoke(logicToInvokeInsideUIThread);
}
else
{
    logicToInvokeInsideUIThread.Invoke();
}

我认为,这是处理此类案件的更好的法典逻辑。 有时需要<代码>Invoke()。

克里斯琴提出了正确的办法,但只要你现在在场,你就可以尝试:

public void ShowMe()
{
 if (_myForm.InvokeRequired)
                _myForm.Invoke(new MethodInvoker(ShowMe));
            else
                this.ShowDialog(_myForm); 
}

If the above doesnt work then pass the SynchronizationContext from your parent form to the class that is performing ShowDialog and perform Invoke on that.

与此相关的是,你可能想对你的设计有rel。 Seems pretty convoluted.

这对我来说是行之有效的——该法典是“家长”形式,它正在主线上运行,儿童形式从另一个胎面起,父母的形式作为直线传到另一个胎面,因为它以前在主线上已经开放:

在主要读物的父母表中:

public delegate void ShowChildConsumer(Form ChildForm);
public void ShowChild(Form ChildForm)
{
     if (this.InvokeRequired)
        {
            var d = new ShowChildConsumer(ShowChild);
            this.Invoke(d, new object[] { ChildForm });
        }
        else
            ChildForm.ShowDialog(this);
    }

在另一层:

frmChildForm childform = new frmChildForm();
parentform.ShowChild(childform);
result = childform.DialogResult;
childform.Dispose(); 

令人感兴趣的是,即使《展览会》被称作主线,我还是能够创造和从其他面读到形式!

帮助的希望





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签