Note that there are some explanatory texts on larger screens.

plurals
  1. POC# How do I call methods on objects created by the main thread from a worker thread?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/974317/cross-thread-operation-not-valid-exception-on-inner-controls">&ldquo;Cross-thread operation not valid&rdquo; exception on inner controls</a> </p> </blockquote> <p>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. </p> <p>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:</p> <pre><code>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; } } } </code></pre> <p>2) In designer class I create several objects fom the <code>class MyShape</code> and later add them to a ShapeContainer, using <code>main thread</code> or <code>UI thread.(highlight ShapeContainer)</code></p> <p>3) in the main class called <code>(FourInARow_Server)</code> I add all the objects created in a jagged Array using <code>arrayLoad()</code> method. (class is listed below)</p> <p>The Exception occurs when I asynchronously access the method <code>changeColor(int x, int y, int color)</code> using the worker thread(not the thread in which the objects were created). However I can access the objects if for instance I say</p> <pre><code> if(circles[1][1].getImageNumber() == 0) {//do something}; </code></pre> <p>(this using worker thread and main thread works OK) </p> <p>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. </p> <p>I am now looking for a solution to safely acces the changeColor method to change the color of each object using both threads.<br> 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!</p> <pre><code>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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload