0

In a windows forms project i have subscribed to global keyboard event using win32 api to fire an event when i press win + alt + E, in the event handler i have this code:

    _rectangle = new ScreenBoundingRectangle();
    _rectangle.Location = Location;
    _rectangle.Visible = true;

i keep a variable to my rectangle, now based on some logic, i want to hide the rectangle so i set the visibility to false using this line of code:

    _rectangle.Visible = false;

However i get the famous cross threading exception, even if i try this :

this.Invoke(new MethodInvoker(() =>
    {
        _rectangle.Visible = false;
    }));

i still get the cross threading exception!

the _rectangle does not have invoke method, is there is any other way around this ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Stacker
  • 8,157
  • 18
  • 73
  • 135
  • What is the full type of `ScreenBoundingRectangle`? It doesn't seem to be a BCL class. – Jason Watkins May 15 '15 at 01:03
  • @JasonWatkins, VisualUIAVerify.Utils.ScreenBoundingRectangle, – Stacker May 15 '15 at 01:10
  • Fixed it, had to download the open source file for ScreenboundingRectangle and invoke the underline control used to make the Rectangle, i will post an answer in a little bit, not sure why its not there by design since this is official library! – Stacker May 15 '15 at 01:17
  • possible duplicate of [Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on](http://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the) – Alex May 15 '15 at 04:10
  • @Vash, not really this explain a problem with a library. – Stacker May 15 '15 at 12:24

1 Answers1

0

just for anyone who would use the VisualUIAVerify library and would face this issue:

download the open source and add this to the Visiable property of the ScreenBoundingRectangle

        public bool Visible
        {
            get { return this._visible; }
            set
            {
                this._visible = value;

                if (value)
                    SafeNativeMethods.ShowWindow(_form.Handle, 8);
                else
                {
                    //Here invoke _form:
                    this._form.Invoke(new MethodInvoker(() =>
                    {
                        _form.Hide();
                    }));

                }
            }
        }
Stacker
  • 8,157
  • 18
  • 73
  • 135