Thread crossing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Laurent Navarro

    Thread crossing

    Hello,


    I created an application with the FrameWork 1.1 containing differents
    forms and threads runing. There was no problem, I could click on a picture
    on my Main Form to call the Show() function of another form without problem.

    Then I installed Visual Studio 2005 and the FrameWork 2.0. I launched my
    application and I had lots of error saying I was crossing the thread calls
    when clicking on the picture of my Main Form or calling the BeginInit()
    function of a custom control containing a TreeView.

    I read the MSDN and found the
    System.Windows. Forms.CheckForI llegalCrossThre adCalls parameter and set it to
    false. It is now working but I know that it is just a trick hiding some
    mistakes in my source code.

    My question is: do I have to create delegates each time I want to use a
    function of a form or a control from another form or thread ? If I want to
    call the Show() function of a form from my Main Form, do I have to create on
    the child form a delegate and a new NewShow() function like this (for
    example):

    public classe MyForm : System.Windows. Forms.Form
    {
    private delegate void show(void);

    public void NewShow()
    {
    if (this.InvokeReq uired)
    {
    this.Invoke(new show(base.Show( void)));
    }
    else
    {
    base.Show();
    }
    }
    }

    It is just an example, the syntaxe may be incorrect but the idea is
    here. Prehaps did I miss something and I hope someone will be able to help
    me !

    Thank you

    Laurent


  • Willy Denoyette [MVP]

    #2
    Re: Thread crossing


    "Laurent Navarro" <yop@yop.comwro te in message
    news:uh76X9$2GH A.1568@TK2MSFTN GP03.phx.gbl...
    | Hello,
    |
    |
    | I created an application with the FrameWork 1.1 containing differents
    | forms and threads runing. There was no problem, I could click on a picture
    | on my Main Form to call the Show() function of another form without
    problem.
    |
    | Then I installed Visual Studio 2005 and the FrameWork 2.0. I launched
    my
    | application and I had lots of error saying I was crossing the thread calls
    | when clicking on the picture of my Main Form or calling the BeginInit()
    | function of a custom control containing a TreeView.
    |
    | I read the MSDN and found the
    | System.Windows. Forms.CheckForI llegalCrossThre adCalls parameter and set it
    to
    | false. It is now working but I know that it is just a trick hiding some
    | mistakes in my source code.
    |
    | My question is: do I have to create delegates each time I want to use a
    | function of a form or a control from another form or thread ? If I want to
    | call the Show() function of a form from my Main Form, do I have to create
    on
    | the child form a delegate and a new NewShow() function like this (for
    | example):
    |
    | public classe MyForm : System.Windows. Forms.Form
    | {
    | private delegate void show(void);
    |
    | public void NewShow()
    | {
    | if (this.InvokeReq uired)
    | {
    | this.Invoke(new show(base.Show( void)));
    | }
    | else
    | {
    | base.Show();
    | }
    | }
    | }
    |
    | It is just an example, the syntaxe may be incorrect but the idea is
    | here. Prehaps did I miss something and I hope someone will be able to help
    | me !
    |
    | Thank you
    |
    | Laurent
    |
    |

    That means your application was buggy from the start, you should never touch
    the UI from another thread than the creator of the UI elements (form,
    controls). The V2 framework implements a debug probe to watch for this
    common error.
    This means that you need to call Control.Invoke or BeginInvoke whenever you
    need to touch (update or read a property) from another thread.

    Willy.


    Comment

    • savia755@latinmail.com

      #3
      Re: Thread crossing


      Willy Denoyette [MVP] wrote:
      >
      That means your application was buggy from the start, you should never touch
      the UI from another thread than the creator of the UI elements (form,
      controls). The V2 framework implements a debug probe to watch for this
      common error.
      This means that you need to call Control.Invoke or BeginInvoke whenever you
      need to touch (update or read a property) from another thread.
      >
      I think I find the bug of my application by this advice. Very useful
      though

      Comment

      Working...