C#Delegate返回NULL

时间:2012-03-23 02:01:36

标签: c# delegates

我正在努力学习代表而不确定我做错了什么。所以我有三个类,Form1,Class1和Class2。 Form1调用Class1方法为Class1成员设置字符串,然后Form1启动workerbackground线程以启动Class2。但是当Class2调用Form1委托方法时,它将调用Class1方法来获取字符串数据,但它返回Class2为null。

所以这是它返回null的地方,>> string str = form1Obj.gDelegateClass1GetData(();

这是Form1:

public delegate string DelegateClass1GetData();
public delegate void DelegateClass1SetData(string data);

public partial class Form1 : Form
{
    public DelegateClass1GetData gDelegateClass1GetData;
    public DelegateClass1SetData gDelegateClass1SetData;

    public Class1 class1Obj;
    public BackgroundWorker bw_Class2;
    public Class2 class2Obj;


    public Form1()
    {
        InitializeComponent();

        class1Obj = new Class1();
        class2Obj = new Class2(this);


        gDelegateClass1GetData = new DelegateClass1GetData(this.Class1GetData);
        gDelegateClass1SetData = new DelegateClass1SetData(this.Class1SetData);
    }


    private void button1_Click(object sender, EventArgs e)
    {
        class1Obj.setClass1Data("Hello");

        bw_Class2 = new BackgroundWorker();
        bw_Class2.WorkerSupportsCancellation = true;

        bw_Class2.DoWork += new DoWorkEventHandler(ThreadClass2_DoWork);
        bw_Class2.RunWorkerAsync();
    }


    public string Class1GetData()
    {
        if (InvokeRequired)
            Invoke(new DelegateClass1GetData(Class1GetData));
        else
        {
            return class1Obj.GetClass1Data();
        }

        return null;
    }


    public void Class1SetData(string data)
    {
        if (InvokeRequired)
            Invoke(new DelegateClass1SetData(Class1SetData), data);
        else
        {
            class1Obj.setClass1Data(data);
        }
    }


    public void ThreadClass2_DoWork(object sender, DoWorkEventArgs e)
    {
        string str1 = class1Obj.GetClass1Data();
        class2Obj.AddString();
    }

}

这是Class1:

public class Class1
{
    public string gClass1Data;

    public Class1()
    {
        gClass1Data = null;
    }


    public void setClass1Data(string data)
    {
        gClass1Data = data;
    }


    public string GetClass1Data()
    {
        return gClass1Data;
    }
}

这个Class2:

public class Class2
{

    public Form1 form1Obj;

    public Class2(Form1 obj)
    {
        form1Obj = obj;
    }


    public void AddString()
    {
        string str = form1Obj.gDelegateClass1GetData();
        str = str + " Delegate";

        form1Obj.gDelegateClass1SetData(str);
        string str2 = form1Obj.gDelegateClass1GetData();
    }
}

1 个答案:

答案 0 :(得分:2)

问题出在Form1.Class1GetData方法中。当您在form1Obj.gDelegateClass1GetData()中致电Class2.AddString()时,实际上是致电Form1.Class1GetDataInvokeRequired会返回true,您必须再次致电Form1.Class1GetData。这一次,InvokeRequired返回false,然后调用class1Obj.GetClass1Data(),并返回“Hello”。它实际上返回到Form1.Class1GetData的第一次调用,但最后返回null。因此,您获得nullstring str = form1Obj.gDelegateClass1GetData(();。尝试将其修复为

public string Class1GetData()
{
    if (InvokeRequired)
        return (string) Invoke(new DelegateClass1GetData(Class1GetData)); // return the first call to Class1GetData
    else
    {
        return class1Obj.GetClass1Data();
    }

    //return null; // Don't return null.
}