Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6817925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:06:52+00:00 2026-05-26T21:06:52+00:00

Possible Duplicate: “Cross-thread operation not valid” exception on inner controls I am a beginner

  • 0

Possible Duplicate:
“Cross-thread operation not valid” exception on inner controls

I am a beginner in C# and lately I run over a cross thread ex. Now I searched high and low to find an answer to my problem however, none seem to be same as mine so hopefully some could help. Here is mine problem explained.

1) I extend a Microsoft.VisualBasic.PowerPacks.OvalShape to add some more functionality to it so it meets my needs and here is what I extend to the class:

public class MyShape: Microsoft.VisualBasic.PowerPacks.OvalShape
{
    int imageNumber;

    public MyShape()
    {
        imageNumber = 0;
        this.BackgroundImage = global::_4InARow.Properties.Resources.grey;
    }

    public int getImageNumber()
    {
        return imageNumber;
    }

    public bool setImage(int imgNo)
    {
        imageNumber = imgNo;
        if (imgNo == 0) { this.BackgroundImage = global::_4InARow.Properties.Resources.grey; return true; }
        else if (imgNo == 1) { this.BackgroundImage = global::_4InARow.Properties.Resources.blue; return true; }
        else if (imgNo == 2) { this.BackgroundImage = global::_4InARow.Properties.Resources.red; return true; }
        else { return false; }
    }
}

2) In designer class I create several objects fom the class MyShape and later add them to a ShapeContainer, using main thread or UI thread.(highlight ShapeContainer)

3) in the main class called (FourInARow_Server) I add all the objects created in a jagged Array using arrayLoad() method. (class is listed below)

The Exception occurs when I asynchronously access the method changeColor(int x, int y, int color) using the worker thread(not the thread in which the objects were created).
However I can access the objects if for instance I say

 if(circles[1][1].getImageNumber() == 0)  
  {//do something};  

(this using worker thread and main thread works OK)

One other thing about the exception is that, when complaining that a worker thread called an an object created in the main thread, it is the ShapeContainer object that throws the exception rather than the MyShape object.

I am now looking for a solution to safely acces the changeColor method to change the color of each object using both threads.
Like I said I did look on-line for an answer but none were like what I am facing and I hope some one could help. Thanks!

public partial class FourInARow_Server : Form   
{  
    private MyShape[][] circles = new MyShape[7][];  
    private Socket newConnection;  
    private Thread newThread;  
    private int player;  
    private bool flag_MyTurn;  
    private bool flag_ConnectionAlive;  
    private bool flag_MoveAllowed;  
    private bool flag_EventBlocker; 

    public FourInARow_Server()
    {
        InitializeComponent();
        arrayLoad();
        player = 1;
        flag_MyTurn = true;
        flag_ConnectionAlive = false;
        flag_MoveAllowed = false;//variable needed to prevent user from loosing its turn if it clicks on a colomn that hasn,t got any more  available moves.
        flag_EventBlocker = true;
        this.labelSystemMessage.Text = "To begin play, please press \"Launch Server\" \n and wait for opponent to connect ";
    }

    private void arrayLoad()//Load all ovall shapes into an array
    {
        MyShape[] colOne = { x1y1, x1y2, x1y3, x1y4, x1y5, x1y6};
        MyShape[] colTwo = { x2y1, x2y2, x2y3, x2y4, x2y5, x2y6};
        MyShape[] colThree = { x3y1, x3y2, x3y3, x3y4, x3y5, x3y6};
        MyShape[] colFour = { x4y1, x4y2, x4y3, x4y4, x4y5, x4y6 };
        MyShape[] colFive = { x5y1, x5y2, x5y3, x5y4, x5y5, x5y6 };
        MyShape[] colSix = { x6y1, x6y2, x6y3, x6y4, x6y5, x6y6};
        MyShape[] colSeven = {x7y1, x7y2, x7y3, x7y4, x7y5, x7y6 };
        circles[0] = colOne; circles[1] = colTwo; circles[2] = colThree; circles[3] = colFour; circles[4] = colFive; circles[5] = colSix; circles[6] = colSeven;

    }

    private void changeColor(int x, int y, int color)
    {
        if (color == 0) { circles[x][y].setImage(0); }
        else if (color == 1) { circles[x][y].setImage(1); }
        else if (color == 2) { circles[x][y].setImage(2); }

        Application.DoEvents();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:06:52+00:00Added an answer on May 26, 2026 at 9:06 pm

    Use the following in the worker thread:

    this.Invoke((MethodInvoker) delegate {
        changeColor(x, y, color);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Weird “406 not acceptable” error following code generate a 406 Not Acceptable
Possible Duplicate: “C subset of C++” -> Where not ? examples ? I'm aware
Possible Duplicate: Is “for(;;)” faster than “while (TRUE)”? If not, why do people use
Possible Duplicate: g++ “is not a type” error The following does not compile: 1
Possible Duplicate: Weird “406 not acceptable” error I've been stuck on this ALL DAY!
Possible Duplicate: Are “(function ( ) { } ) ( )” and “(function (
Possible Duplicate: ASP.NET “special” tags What is the difference between <%# ... %> ,
Possible Duplicate: When to use “strictfp” keyword in java? What is the use of
Possible Duplicate: Real differences between “java -server” and “java -client”? I've seen the argument
Possible Duplicate: How to: Back button support “Ajax” I have a ASP.NET MVC implementation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.