Multithreading question...

  • Thread starter Thread starter Just Browsing
  • Start date Start date
J

Just Browsing

Hi,

I've been playing around with C# and was trying to put together a small
application that uses multiple threads as a learning exercise. For a simple
(but fun <g>) sample project, I thought I would do a simple game like pong.
Unfortunately it isn't working. My approach was to create a thread for each
object (ball, paddle etc.) and have each thread draw it's "object" back to
the main form. I was able to get each thread to be able to update the Text
property of the form but not it's client area. I guess my main question is
what should be the proper approach? Is having multiple theads writting to
the same form a bad idea (very possible...remember I'm new to C#)? If it is
an ok idea, how should each thread gain access to the form's graphics/client
area?

Any help or pointers to samples or reading would be appreciated.

Thanks

Eric
 
I've been playing around with C# and was trying to put together a small
application that uses multiple threads as a learning exercise. For a simple
(but fun <g>) sample project, I thought I would do a simple game like pong.
Unfortunately it isn't working. My approach was to create a thread for each
object (ball, paddle etc.) and have each thread draw it's "object" back to
the main form. I was able to get each thread to be able to update the Text
property of the form but not it's client area. I guess my main question is
what should be the proper approach? Is having multiple theads writting to
the same form a bad idea (very possible...remember I'm new to C#)? If it is
an ok idea, how should each thread gain access to the form's graphics/client
area?

read this carefully (all 3 parts)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/htm
l/winforms06112002.asp
 
Just Browsing said:
I've been playing around with C# and was trying to put together a small
application that uses multiple threads as a learning exercise. For a simple
(but fun <g>) sample project, I thought I would do a simple game like pong.
Unfortunately it isn't working. My approach was to create a thread for each
object (ball, paddle etc.) and have each thread draw it's "object" back to
the main form. I was able to get each thread to be able to update the Text
property of the form but not it's client area. I guess my main question is
what should be the proper approach? Is having multiple theads writting to
the same form a bad idea (very possible...remember I'm new to C#)? If it is
an ok idea, how should each thread gain access to the form's graphics/client
area?

The *only* thread which should be changing the GUI is the GUI event
thread for the form. Use Control.Invoke and
Control.BeginInvoke/Control.EndInvoke to invoke a delegate in the right
thread.
 
Back
Top