سؤال

I am trying to update a treeview but I am receiving an error that I can not convert an Obj to a string.

I am creating a scanner of sorts that uses multiple threads, once each item is checked I want to invoke a delegate to update a treeview with the new information. I am able to easily invoke it to update the information if I only invoke the method with an object but as soon as I try to with a string as well I get an error.

Here is how I am doing it:

public delegate void StringDelegate(string input, string address);

        public void UpdateScan(string input, string ip)
        {
            TreeNode treeNode = new TreeNode(input);

            if (!(outputTree.Nodes.ContainsKey(input)))
            {
                //Add our parent node
                outputTree.Nodes.Add(treeNode);
                //Add our child node
                treeNode.Nodes.Add(ip);
            } 
            else
            {
                //Add only child node
                treeNode.Nodes.Add(ip);
            }

        }

public void scanItem()
{

//Scan code

//Result Code
string outPut = "Pretend Result";

                //Invoke our callback
                object[] obj = new object[1];
                obj[0] = outPut;
                outputTree.BeginInvoke(new StringDelegate(UpdateScan), obj, Ip.ToString());

}

Can anyone show me where I am going wrong?

هل كانت مفيدة؟

المحلول

You are explicitly adding an object to the call.

Change

object[] obj = new object[1];
obj[0] = outPut;
outputTree.BeginInvoke(new StringDelegate(UpdateScan), obj, Ip.ToString());

to

outputTree.BeginInvoke(new StringDelegate(UpdateScan), outPut, Ip.ToString());
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top