Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Invoking methods with parameters in Multithreading apps

Status
Not open for further replies.

mattp14

Programmer
Apr 8, 2005
35
US
I've got a program that does a search using directory services, and the problem i'm having is returning the results. I moved the search to a background thread (using System.Threading.Thread) so that it can be cancelled, gui remains responsive, etc.

I've defined an event called itemFound that gets fired back to the main form each time this search turns up some results.

The problem i'm having occurs when i want to add that found item to an ArrayList stored in the main form. Later, when I try to sort that array, i get an InvalidOperationException - At least one member must implement IComparable error.

I think the problem lies in that i'm not invoking the methods of the main form correctly... stop me if you think something else might be the problem.

I don't know of a way to invoke a method that takes parameters... and i think that could help also if someone knows how to do that.

Here's the code that gets executed when the search thread raises an itemFound event. This code lies in my main form.

Code:
private String itemString = "";

private void itemFound(String s) 
{
     itemString = s;
     this.Invoke(new MethodInvoker(this.doAddItem));
}

and that of course invokes this method:

Code:
        private void doAddItem()
        {
            if (itemString != "User not found")
            {
                parsedItem = parseResults(itemString);
                items.Add(parsedItem);
                //this.Invoke(new MethodInvoker(this.addItem));
                listBox1.Items.Add(parsedItem[0] + "  -  " + parsedItem[1]);
                //this.Invoke(new MethodInvoker(this.lstAddItem));
                
                if (items.Count == 1)
                    lblResults.Text = "1 Item Found";
                else
                    lblResults.Text = items.Count.ToString() + " Items Found";
            }
            else
                lblResults.Text = "0 Items Found";
        }


Any thoughts? Thanks in advance... i know that was kind of a long read!!!
 
Well i was able to figure out my problem... turns out it didnt have anything to do with invoking or multithreading.

But it would still be nice to know how to invoke methods with parameters if someone knows how!

Thanks!!
 
Umm, you can't. Unlike Java, which allows you to pass in one parameter of type object, .NET doesn't have this ability.

You'll need a singleton class (or something that acts like a singleton) that the newly created thread can call to retrieve it's parameters. Don't forget to surround the calls with the lock keyword.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Not sure if this pertains just thought it interesting. I used a delegate with an argument of object and used invoke.
Code:
		public delegate void UpdateListBoxCallback(object[] objColors);

		// Updates the listbox.
		private void UpdateListBox(object[] objColors)
		{
			for (int i = 0; i < objColors.Length; ++i)
			{
				string color = (string)objColors[i];
				this.listboxColors.Items.Add(color);
			}		
		}

//add colors on button click
		private void btnAddColors_Click(object sender, System.EventArgs e)
		{
			this.listboxColors.Invoke(new UpdateListBoxCallback(this.UpdateListBox), new object[]{new object[]{"Black","Orange","Brown"}});

		}

Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top