Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带返回值的C#委托方法_C#_Multithreading_Delegates_Return - Fatal编程技术网

带返回值的C#委托方法

带返回值的C#委托方法,c#,multithreading,delegates,return,C#,Multithreading,Delegates,Return,我正在制作一个运行两个线程的C#程序。主UI线程和我自己制作的独立网络线程 我发现,如果我需要从网络线程更改UI中的某些内容,我需要调用deligate方法,该方法工作正常: // Declared as a global variable public delegate void ListBoxFirst(); //Then call this from the network thread listBox1.BeginInvoke(new ListBoxFirst(InitListBox)

我正在制作一个运行两个线程的C#程序。主UI线程和我自己制作的独立网络线程

我发现,如果我需要从网络线程更改UI中的某些内容,我需要调用deligate方法,该方法工作正常:

// Declared as a global variable
public delegate void ListBoxFirst();

//Then call this from the network thread
listBox1.BeginInvoke(new ListBoxFirst(InitListBox));

//Which points to this
public void InitListBox() {
     listBox1.SelectedIndex = 0;
}
现在我需要能够从网络线程中读取UI值(listbox.selectedindex),但是如果我以同样的方式尝试,它会显示错误“无法隐式地将type'system.iasyncresult'转换为'int'”(当然是使用“int”而不是“void”,以及listbox1.begininvoke之前的“int a=”)。我在谷歌上搜索了很多次,但我对C#还很陌生,所以我真的迷路了


任何帮助都非常有用

您需要调用
EndInvoke()
来获得结果

一些代码:

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            listBox1.Items.AddRange(new[] { "AS","Ram"});           
        }

        protected override void OnLoad(EventArgs e)
        {
            listBox1.BeginInvoke(new Action(GetResult));
        }

        private void GetResult()
        {
            if (InvokeRequired)
            {
                Invoke(new Action(GetResult));
            }
            listBox1.SelectedIndex = 0;
        }
    }

您需要调用
EndInvoke()
来获得结果

一些代码:

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            listBox1.Items.AddRange(new[] { "AS","Ram"});           
        }

        protected override void OnLoad(EventArgs e)
        {
            listBox1.BeginInvoke(new Action(GetResult));
        }

        private void GetResult()
        {
            if (InvokeRequired)
            {
                Invoke(new Action(GetResult));
            }
            listBox1.SelectedIndex = 0;
        }
    }
我想出来了:

public int ReadListBoxIndex()
    {
        int count = 0;
        listBox1.Invoke(new MethodInvoker(delegate
        {
            count = listBox1.SelectedIndex;
        }));
        return count;
    }
打电话给常客

int count = ReadListBoxIndex();
我想出来了:

public int ReadListBoxIndex()
    {
        int count = 0;
        listBox1.Invoke(new MethodInvoker(delegate
        {
            count = listBox1.SelectedIndex;
        }));
        return count;
    }
打电话给常客

int count = ReadListBoxIndex();

这同样有效,其中
tn
是要添加到跨线程的树节点,
tnret
是返回的树节点


\u treeView.Invoke(新操作(()=>tnret=tn.Nodes.Add(name,name))

这也适用,其中
tn
是要添加到跨线程的树节点,
tnret
是返回的树节点


\u treeView.Invoke(新操作(()=>tnret=tn.Nodes.Add(name,name))

正在异步运行。您不能返回read任何返回值,因为它尚未完成。这是异步运行的。你不能返回任何返回值,因为它还没有完成。我想你误解了我的问题。我确实知道如何将索引设置为0。我需要的是阅读价值观,而不是设定价值观。我想你误解了我的问题。我确实知道如何将索引设置为0。我需要的是读取值,而不是设置值。