在Compact Framework中的子窗体的showdialog中调用父窗体的方法导致ThreadAbortException [英] Calling a Method of parent form in showdialog of child form in Compact Framework Causing ThreadAbortException

查看:101
本文介绍了在Compact Framework中的子窗体的showdialog中调用父窗体的方法导致ThreadAbortException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从父表单调用表单的ShowDialog,我在子表单中填充一些数据我希望在父表单中调用一个方法。



我的父表单中的方法更新了我的表单中的控件值。



这导致我一个线程异常



比如说



I am calling a ShowDialog of a form from my parent form and i am Populating some data in the child form Through which i want to call a method in my parent form.

The method in my parent form updates controls values in my form.

this is causing me a threadabort exception

Say like

ChildForm Cform=new ChildForm();
   Cform.ShowDialog();





和ChildForm





and in ChildForm

ParentForm PForm=new Parentform();
    PForm.Somemethod();//method in my parentForm







在某些方法中我我正在通过调用来更新表单中控件的值



我正在调用每个Control但仍然得到* ThreadAbort异常*



**注意:我使用的是Compact Framework **



先谢谢




In somemethod I am updating the values of the controls in the form by invoking

I am invoking each Control but still I am getting the *ThreadAbort Exception*

**Note: I am using Compact Framework**

Thanks in Advance

推荐答案

您不想创建 ParentForm。你想要的是获得刚创建ChildForm的ParentForm的实例。



有几种方法可以实现这一点。这是最有用的一个(恕我直言)。

ChildForm.cs:
You don't want to create a new ParentForm. What you want is to get an instance of the ParentForm that just created the ChildForm.

There are several ways to achive this. Here's the most useful one (IMHO).
ChildForm.cs:
public class ChildForm : Form
{
    // Prepare parent that threre may be some action.
    public event EventHandler ChildWantedSomething;

    // Instead of new ParentForm().SomeMethod()
    EventHandler eh = ChildWantedSomething;
    if( eh != null)
    {
        eh(this, new EventArgs());
    }
}

ParentForm.cs:

ParentForm.cs:

public class ParentForm : Form
{
    ChildForm CForm = new ChildForm();
    CForm.ChildWantedSomething += SomeMethod;
}

当然,要使这个简单的变体起作用, SomeMethod 需要有一个由 EventHandler [ ^ ]。如果那是不可能的,你可以为事件订阅另一种方法,只为你想要的那个方法组成。

或者你可以使用与你需要的签名匹配的EventHandler之外的自定义委托。





由于不完全清楚的原因,这里有一个我不喜欢上面那个版本的版本,没有事件。

ChildForm.cs:

Of course, for this simple variant to work, SomeMethod needs to have the signature dictated by EventHandler[^]. If that's not possible, you can either subscribe another method to the event, made up solely to cal the one you want.
Or you can use a custom delegate other than EventHandler that matches the signature you need.


For reasons not totally clear, here a version that I don't like as much as the one above, without events.
ChildForm.cs:

public class ChildForm : Form
{
    // Store instance of parent form
    private ParentForm _parentForm = null;

    // Constructor taking ParentForm parameter
    public ChildForm(ParentForm parent)
    {
        _parentForm = parent;
    }

    // Instead of new ParentForm().SomeMethod()
    _parentForm.SomeMethod();
}

这个版本将两种形式紧密地联系在一起,这很糟糕。我希望,无论谁把无事件限制放在你身上,都要知道他对你施加了什么。如果没有,很可能在以后,当这个解决方案的缺点暴露出来时,你会因为做得不好而受到指责。



祝你好运。

[/ Edit]

This version links both forms tightly together, which is bad. I hope, whoever put that no-events-restraint on you is aware of what he is imposing on you. If not, chances are that later on, when the drawbacks of this solution are revealed, you get blamed for not doing it right.

Good luck.
[/Edit]


这篇关于在Compact Framework中的子窗体的showdialog中调用父窗体的方法导致ThreadAbortException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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