8 Replies - 15704 Views - Last Post: 29 September 2010 - 04:49 PM Rate Topic: -----

#1 Guest_Guest*


Reputation:

set form property cross thread

Posted 28 September 2010 - 09:03 AM

i have a property that is set by a combobox and comparing it to an enum. but then i have a backgroundworker running code that uses the value of the property. but when the button is pushed to start the backgroundworker i get an error stating InvalidOperation and stating operation not valid on a thread other then what the control was made on. but shouldn't the property be set by the actual control? i dont set it via code.

how can i fix this?

property code:
        //enum and property to set hash type
        public enum Hash
        {
            MD5 = 0,
            SHA1 = 1,
            SHA256 = 2,
        }

        public Hash HashType
        {
            get
            {
                switch (cbxType.SelectedItem.ToString())
                {
                    case "MD5":
                        return Hash.MD5;

                    case "SHA1":
                        return Hash.SHA1;

                    case "SHA256":
                        return Hash.SHA256;

                    default:
                        return Hash.MD5;
                }
            }
            set
            {
                switch (value)
                {
                    case Hash.MD5:
                        cbxType.SelectedItem = 0;
                        break;

                    case Hash.SHA1:
                        cbxType.SelectedItem = 1;
                        break;

                    case Hash.SHA256:
                        cbxType.SelectedItem = 2;
                        break;
                }
            }
        }


backgroundworker code:
                //determine selected hash and send the method the file path
                switch (HashType)
                {
                    case Hash.MD5:
                        txtOutput.Text = MD5Engine.FileHash(InputData);
                        break;

                    case Hash.SHA1:
                        txtOutput.Text = SHA1Engine.FileHash(InputData);
                        break;

                    case Hash.SHA256:
                        txtOutput.Text = SHA256Engine.FileHash(InputData);
                        break;
                }



Is This A Good Question/Topic? 0

Replies To: set form property cross thread

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: set form property cross thread

Posted 28 September 2010 - 09:23 AM

The problem is the backgroundworker is a different thread than the GUI thread that created the txtOutput textbox.

What you want to do is raise the backgroundworker progress event. Then have the GUI thread have a handler method to make the updates.


Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2

The background worker should know NOTHING of gui. It does its job, and occassionally yells "I'm x percent done"
Was This Post Helpful? 1
  • +
  • -

#3 Guest_Guest*


Reputation:

Re: set form property cross thread

Posted 28 September 2010 - 09:31 AM

View PosttlhIn, on 28 September 2010 - 08:23 AM, said:

The problem is the backgroundworker is a different thread than the GUI thread that created the txtOutput textbox.

What you want to do is raise the backgroundworker progress event. Then have the GUI thread have a handler method to make the updates.


Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2

The background worker should know NOTHING of gui. It does its job, and occassionally yells "I'm x percent done"

i fixed the combobox issue by doing this:
                //determine selected hash and send the method the file path
                switch ((Hash)e.Argument)
                {
                    case Hash.MD5:
                        OutputData = MD5Engine.FileHash(InputData);
                        break;

                    case Hash.SHA1:
                        OutputData = SHA1Engine.FileHash(InputData);
                        break;

                    case Hash.SHA256:
                        OutputData = SHA256Engine.FileHash(InputData);
                        break;
                }


but now i have an issue with the textbox so i will check your links to see if i get it otherwise i will post back. did i solve the combobox issue correctly? it needs to know the value of the combobox BEFORE it can run.
Was This Post Helpful? 0

#4 Guest_Guest*


Reputation:

Re: set form property cross thread

Posted 28 September 2010 - 09:49 AM

ok after reading your custom event tutorial i have no idea how to get it to work with my textbox data since it gets set from a method as you can see in my backgroundworker code.
Was This Post Helpful? 0

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: set form property cross thread

Posted 28 September 2010 - 10:17 AM

When you raise a generic event (without creating a custom arguments class) it takes with it two things: An object which is generally the sender and eventargs.

The *right* way to do it is to make custom event args for a string, or bitmap or whatever.
The *cheat* way to do it is to use the Framework generic event, but put your data as the object.
The object doesn't *have* to be the sender. It can be your picture, or your string, or your custom WidgetObj class.
Then you cast it back on the receiving side handler.

So have your worker raise one of a couple events:

public event NewMD5;
public event NewSHA1;
public event NewSHA256;

Have your form respond as needed to one of these events. If your worker raises a NewMD5 event with the hashed MD5 as the object, you have everything you need. Its just like a button raising a click, where the object is the button itself. In this case the object isn't going to be your background worker class that did the figuring because you don't care who the sender was. The object will be the hash it created.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Guest*


Reputation:

Re: set form property cross thread

Posted 29 September 2010 - 12:29 PM

i am still having massive trouble with this. i do not understand how to setup the events and get them to set the property in my worker method.
Was This Post Helpful? 0

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: set form property cross thread

Posted 29 September 2010 - 12:32 PM

View PostGuest, on 29 September 2010 - 11:29 AM, said:

i am still having massive trouble with this. i do not understand how to setup the events and get them to set the property in my worker method.


Thanks for the status update.... ???
Was This Post Helpful? 0
  • +
  • -

#8 Guest_Guest*


Reputation:

Re: set form property cross thread

Posted 29 September 2010 - 03:07 PM

View PosttlhIn, on 29 September 2010 - 11:32 AM, said:

View PostGuest, on 29 September 2010 - 11:29 AM, said:

i am still having massive trouble with this. i do not understand how to setup the events and get them to set the property in my worker method.


Thanks for the status update.... ???

it posted too quickly i didnt notice. i was also wondering if there was any other links you might have for doing what i need to. i tried to attach a method to an event but then it doesnt update my property like i need it to.
Was This Post Helpful? 0

#9 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: set form property cross thread

Posted 29 September 2010 - 04:49 PM

View PostGuest, on 29 September 2010 - 02:07 PM, said:

i tried to attach a method to an event but then it doesnt update my property like i need it to.


Let's see the code you have now and we'll try to fix it. This stuff really isn't all that tough. People just get all ate up when they hear 'event' and they try to make it tougher than it has to be.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1