Why Does This Fail ( Threading )

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • One Handed Man \( OHM - Terry Burns \)

    #16
    Re: Why Does This Fail ( Threading )

    Hi Larry,

    I have implemented your solution in my code and it works. However, the way I
    had to do this was to pass a reference of the form to the Class Invader and
    Gun, this way I could do the Invoke of the method on the UI Thread in the
    form.

    Is this the way you would have done it ?

    Thanks





    --
    OHM ( Terry Burns ) * Use the following to email me *

    Dim ch() As Char = "ufssz/cvsotAhsfbuTpmv ujpotXjui/OFU".ToCharArra y()
    For i As Int32 = 0 To ch.Length - 1
    ch(i) = Convert.ToChar( Convert.ToInt16 (ch(i)) - 1)
    Next
    Process.Start(" mailto:" & New String(ch))
    --


    "Larry Serflaten" <serflaten@usin ternet.com> wrote in message
    news:eNTqUWBuEH A.3788@TK2MSFTN GP09.phx.gbl...[color=blue]
    >
    > "Larry Serflaten" <serflaten@usin ternet.com> wrote
    >[color=green]
    >> Private Sub Form1_Closing(. ..)
    >> e.Cancel = Done
    >> Done = True
    >> Application.DoE vents()
    >> Th1.Abort()
    >> Th2.Abort()
    >> End Sub[/color]
    >
    >
    > Should have been:
    >
    > e.Cancel = Not Done
    >
    > It was meant to cause the first hit on the close button to stop the
    > threads (they take a while to close down) and the second hit to
    > close the form....
    >
    > (Rather than add another button to stop the threads...)
    >
    > LFS[/color]


    Comment

    • Larry Serflaten

      #17
      Re: Why Does This Fail ( Threading )


      "One Handed Man ( OHM - Terry Burns )" <news.microsoft .com> wrote[color=blue]
      > I have implemented your solution in my code and it works. However, the way I
      > had to do this was to pass a reference of the form to the Class Invader and
      > Gun, this way I could do the Invoke of the method on the UI Thread in the
      > form.
      >
      > Is this the way you would have done it ?[/color]


      You are using a design I would have avoided. If you are trying to duplicate
      'Space Invaders' then you'd have rows of aliens that march side to side as
      they advance toward the player.

      Because the whole row advances, and moves as a unit, I would have tried
      to make a class that handles the whole row. In addition to fewer objects,
      I'd be copying an image of the whole row over at a time, and not each
      individual invader.

      But, with that said, it may be this is more of an exercise in using threads,
      which is OK, if that is what you are really after....

      At first glance, it seems you should be using a collection (of some sort) to
      house all the invaders that are currently being used. I would think the call
      from the invaders should be made to their container, to interact with the
      outside world. That would mean the invaders call on a method of their
      collection object, who forwards their call to the form. That forwarding
      routine should add a check to be sure the form reference is still valid
      before forwarding the call.

      While that may be a more refined approach, it may just be more practical
      to make the form available globally (project wide) and let the invaders
      call it direct.

      Because that second method would be easy to code and understand, that
      is probably what I would do. A few choice (global) references is sometimes
      necessary to simplify a large part of the program. This certainly seems to
      be one of those times....

      While some may discourage using global variables, and avoid them at all
      costs, I am not that strict. It pays off, on occasion, to make a few objects
      globally available. To those who think otherwise, I would simply ask them
      how they would call DoEvents, if the Application object wasn't available,
      or other such common tasks that are included in (what appears to be) globally
      available objects....

      Do as you see fit. I favor a simple design when it will work well, so in this
      case, a global variable seems appropreate for the job....

      LFS

      Comment

      • One Handed Man \( OHM - Terry Burns \)

        #18
        Re: Why Does This Fail ( Threading )

        Thanks very much for your help here, it has assisted me greatly.

        I am not really trying to build a proper space invaders as you correctly
        suggest, just trying to get a handle on threading issues which is something
        I have not had to do 'much' of in the past other than one or two cursory
        routines which would have tripped the UI.

        I think I picked an awkward example unbeknown to me, however, it has served
        to illustrate several points to me as far as design consideration is
        concerned.

        Thanks Again . . .


        Best Regards - OHM
        --
        OHM ( Terry Burns ) * Use the following to email me *

        Dim ch() As Char = "ufssz/cvsotAhsfbuTpmv ujpotXjui/OFU".ToCharArra y()
        For i As Int32 = 0 To ch.Length - 1
        ch(i) = Convert.ToChar( Convert.ToInt16 (ch(i)) - 1)
        Next
        Process.Start(" mailto:" & New String(ch))
        --


        "Larry Serflaten" <serflaten@usin ternet.com> wrote in message
        news:ehR2tqPuEH A.2804@TK2MSFTN GP14.phx.gbl...[color=blue]
        >
        > "One Handed Man ( OHM - Terry Burns )" <news.microsoft .com> wrote[color=green]
        >> I have implemented your solution in my code and it works. However, the
        >> way I
        >> had to do this was to pass a reference of the form to the Class Invader
        >> and
        >> Gun, this way I could do the Invoke of the method on the UI Thread in the
        >> form.
        >>
        >> Is this the way you would have done it ?[/color]
        >
        >
        > You are using a design I would have avoided. If you are trying to
        > duplicate
        > 'Space Invaders' then you'd have rows of aliens that march side to side as
        > they advance toward the player.
        >
        > Because the whole row advances, and moves as a unit, I would have tried
        > to make a class that handles the whole row. In addition to fewer objects,
        > I'd be copying an image of the whole row over at a time, and not each
        > individual invader.
        >
        > But, with that said, it may be this is more of an exercise in using
        > threads,
        > which is OK, if that is what you are really after....
        >
        > At first glance, it seems you should be using a collection (of some sort)
        > to
        > house all the invaders that are currently being used. I would think the
        > call
        > from the invaders should be made to their container, to interact with the
        > outside world. That would mean the invaders call on a method of their
        > collection object, who forwards their call to the form. That forwarding
        > routine should add a check to be sure the form reference is still valid
        > before forwarding the call.
        >
        > While that may be a more refined approach, it may just be more practical
        > to make the form available globally (project wide) and let the invaders
        > call it direct.
        >
        > Because that second method would be easy to code and understand, that
        > is probably what I would do. A few choice (global) references is
        > sometimes
        > necessary to simplify a large part of the program. This certainly seems
        > to
        > be one of those times....
        >
        > While some may discourage using global variables, and avoid them at all
        > costs, I am not that strict. It pays off, on occasion, to make a few
        > objects
        > globally available. To those who think otherwise, I would simply ask them
        > how they would call DoEvents, if the Application object wasn't available,
        > or other such common tasks that are included in (what appears to be)
        > globally
        > available objects....
        >
        > Do as you see fit. I favor a simple design when it will work well, so in
        > this
        > case, a global variable seems appropreate for the job....
        >
        > LFS[/color]


        Comment

        Working...