Multithreading question...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Just Browsing

    #1

    Multithreading question...

    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...reme mber 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


  • Wiktor Zychla

    #2
    Re: Multithreading question...

    > I've been playing around with C# and was trying to put together a small[color=blue]
    > application that uses multiple threads as a learning exercise. For a[/color]
    simple[color=blue]
    > (but fun <g>) sample project, I thought I would do a simple game like[/color]
    pong.[color=blue]
    > Unfortunately it isn't working. My approach was to create a thread for[/color]
    each[color=blue]
    > 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[/color]
    Text[color=blue]
    > property of the form but not it's client area. I guess my main question[/color]
    is[color=blue]
    > what should be the proper approach? Is having multiple theads writting to
    > the same form a bad idea (very possible...reme mber I'm new to C#)? If it[/color]
    is[color=blue]
    > an ok idea, how should each thread gain access to the form's[/color]
    graphics/client[color=blue]
    > area?
    >[/color]

    read this carefully (all 3 parts)
    http://msdn.microsoft.com/library/de...us/dnforms/htm
    l/winforms0611200 2.asp


    Comment

    • Jon Skeet

      #3
      Re: Multithreading question...

      Just Browsing <Just_Browsing@ somewhere.com> wrote:[color=blue]
      > 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...reme mber I'm new to C#)? If it is
      > an ok idea, how should each thread gain access to the form's graphics/client
      > area?[/color]

      The *only* thread which should be changing the GUI is the GUI event
      thread for the form. Use Control.Invoke and
      Control.BeginIn voke/Control.EndInvo ke to invoke a delegate in the right
      thread.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      Working...