0

alredy a question out about this. but here comes another question little diffrent, and i cant find any awnsers to it!

String text = this.GuiThread(() => this.comboBox1.Text); 

 if (text == "this")
 {
   //do somthing spectacular!
 }

text is an empty variable or so..

Use of unassigned local variable. this is the ERROR i get..and iv tested all examples i can find here and at msdn.

im also having :

  public static class ControlExtensions
    {
        public static void GuiThread(this Control ctrl, Action action)
        {
            if (ctrl.InvokeRequired)
            {
                ctrl.BeginInvoke(action);
            }
            else
            {
                action.Invoke();
            }
        }
    }

ideas?

1 Answer 1

1

This code cannot compile. The GuiThread returns void, you are trying to assign that to a string. How you can get an exception is unguessable. It needs to at least look like this:

public static class ControlExtensions {
    public static T GuiThread<T>(this Control ctrl, Func<T> action) {
        if (ctrl.InvokeRequired) {
            return (T)ctrl.Invoke(action);
        }
        else {
            return action();
        }
    }
}

Don't write code like this, the actual ComboBox text you'll read is pretty random since it can be obtain while the user is modifying it. Give a thread the arguments it needs when you start it. The BackgroundWorker class keeps you out of trouble.

4
  • that code just gave me 100k errors on my other GUI outputs this.GuiThread(() => this.listView1.Sorting = SortOrder.None); is how i do it in rest of code
    – 1244
    Apr 25, 2012 at 15:49
  • Did you consider renaming the function and keeping the original? Apr 25, 2012 at 17:36
  • it did work then aperantly. alltough i cant quite understand what it does, and what it adds to my function.
    – 1244
    Apr 25, 2012 at 20:31
  • Don't use code you don't understand. Rip it all out and leave the threading for another day, when you're ready for it. Apr 25, 2012 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.