cross thread problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: cross thread problem

  1. #1
    Join Date
    Nov 2005
    Posts
    102

    cross thread problem

    I have a cross thread error when i try to run my app. Here's basically how my class files interact with each other:

    I have 3 files:
    main.cs
    gui.cs
    control.cs

    In control.cs is a function that checks for user input. When i press the L on my keyboard then control.cs see's this and calls a function in main.cs which then calls a function in gui.cs. In gui.cs i try to add a control back onto the main.cs form. But that's where i get the cross thread error.

    Like this:
    control.cs -> main.cs -> gui.cs

    Then inside of gui.cs i have something like:

    main.Controls.Add ( myWindow );

    Which appearently isn't allowed to do it like that. Anyone any idea what i could try to solve this??

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: cross thread problem

    You cannot access a control from a thread that did not create it in the first place. You will have to check for InvokeRequired. If that is true, you need to invoke the method which adds/manipulates the control on the thread that created it.

  3. #3
    Join Date
    Nov 2005
    Posts
    102

    Re: cross thread problem

    Thanks, it's fixed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center