Jump to content

problem with multithreading

- - - - -

  • Please log in to reply
2 replies to this topic

#1
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
I'm writing a simulation for school (thrown balls under varying parameters)
this code is executed in a separate thread that creates a statistic by running the same simulation several times while slightly altering a value each time:

Monitor.Enter(TrajectoryBitmap);

for (int y = 0; y < TrajectoryBitmapHeight - 1; y++)

    TrajectoryBitmap.SetPixel(0, y, Color.Black);

for (int x = 1; x < TrajectoryBitmapWidth; x++)

{

     TrajectoryBitmap.SetPixel(x, TrajectoryBitmapHeight - 1, Color.Black);

     for (int y = 0; y < TrajectoryBitmapHeight - 1; y++)

         TrajectoryBitmap.SetPixel(x, y, Color.White);

}

Monitor.Exit(TrajectoryBitmap);

Why does this return an InvalidOperationException (object already used elsewhere)? Shouldn't the Monitor prevent that, or am I using it wrong?

On an unrelated note, why do I have to use Invoke() and delegates to get the SelectedIndex of a ComboBox in another thread, but can access the Text of a TextBox without one?


Help with the problem and/or an answer for the question would be much appreciated.

#2
wraith

wraith

    Newbie

  • Members
  • Pip
  • 5 posts
I must admit i'm only a beginner - but try using lock(TrajectoryBitmap) at the top and then {} for the code below.

As for the unrelated note - I'm really not sure - all I know is that when manipulating the GUI controls from another thread requires invocation - perhaps the text in a textbox is somehow databound underneath by the framework itself(accessing datasources for databound items from another thread is ok).

Hope my noobish answer helped :)

#3
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
Using lock() instead of Monitor doesn't seem to make a difference, but thanks for the answer anyway.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users