Suspend/resume Thread problems

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

    Suspend/resume Thread problems

    Hi all,

    I have a loop in the thread checking for a particular service status,
    whenever the status changes to "stopped" a RaiseEvent is generated by
    thread and another function runs. At the same time the thread is
    suspended. When I try to resume it from the another function I have
    some problems.

    Thank you.
    Juky

    Here the code:

    Public Event StatusEventRais e(ByVal EventGenerated As Integer)

    Dim StatusService As Threading.Threa d
    Private svc As ServiceControll er
    .......

    Public Sub New()
    .....
    AddHandler StatusEventRais e, AddressOf StatusEventRais eHandler
    StatusService = New Threading.Threa d(AddressOf LoopCheck)
    StatusService.S tart()
    ....
    end Sub

    Private Sub LoopCheck()
    While True
    svc.Refresh()
    Select Case svc.Status
    Case ServiceControll erStatus.StopPe nding
    RaiseEvent StatusEventRais e
    ServiceControll erStatus.StopPe nding)
    StatusService.S uspend()
    Case ServiceControll erStatus.Stoppe d
    RaiseEvent
    StatusEventRais e(ServiceContro llerStatus.Stop ped)
    Me.StatusServic e.Suspend()
    End Select
    End While
    End Sub


    Private Sub StatusEventRais eHandler(ByVal EventGenerated As Integer)
    Static nr As Integer = 0

    Select Case EventGenerated
    Case ServiceControll erStatus.Stoppe d
    svc.Start()
    StatusService.R esume()
    Case ServiceControll erStatus.StopPe nding
    While svc.Status <> ServiceControll erStatus.Stoppe d
    svc.Refresh()
    End While
    svc.Start()
    while svc.Status <> ServiceControll erStatus.Runnin g
    svc.Refresh()
    End While
    StatusService.R esume()
    End Select
    End Sub

  • Nick

    #2
    Re: Suspend/resume Thread problems

    Sorry, I haven't really looked through your code but, I would check that
    each class that you are using within your functions is thread safe.
    For instance MSDN says that only static/shared instances your
    ServiceControll er class are guaranteed to be threadsafe.

    "juky" <juklingos@yaho o.com> wrote in message
    news:1108115225 .575130.294990@ o13g2000cwo.goo glegroups.com.. .[color=blue]
    > Hi all,
    >
    > I have a loop in the thread checking for a particular service status,
    > whenever the status changes to "stopped" a RaiseEvent is generated by
    > thread and another function runs. At the same time the thread is
    > suspended. When I try to resume it from the another function I have
    > some problems.
    >
    > Thank you.
    > Juky
    >
    > Here the code:
    >
    > Public Event StatusEventRais e(ByVal EventGenerated As Integer)
    >
    > Dim StatusService As Threading.Threa d
    > Private svc As ServiceControll er
    > ......
    >
    > Public Sub New()
    > ....
    > AddHandler StatusEventRais e, AddressOf StatusEventRais eHandler
    > StatusService = New Threading.Threa d(AddressOf LoopCheck)
    > StatusService.S tart()
    > ...
    > end Sub
    >
    > Private Sub LoopCheck()
    > While True
    > svc.Refresh()
    > Select Case svc.Status
    > Case ServiceControll erStatus.StopPe nding
    > RaiseEvent StatusEventRais e
    > ServiceControll erStatus.StopPe nding)
    > StatusService.S uspend()
    > Case ServiceControll erStatus.Stoppe d
    > RaiseEvent
    > StatusEventRais e(ServiceContro llerStatus.Stop ped)
    > Me.StatusServic e.Suspend()
    > End Select
    > End While
    > End Sub
    >
    >
    > Private Sub StatusEventRais eHandler(ByVal EventGenerated As Integer)
    > Static nr As Integer = 0
    >
    > Select Case EventGenerated
    > Case ServiceControll erStatus.Stoppe d
    > svc.Start()
    > StatusService.R esume()
    > Case ServiceControll erStatus.StopPe nding
    > While svc.Status <> ServiceControll erStatus.Stoppe d
    > svc.Refresh()
    > End While
    > svc.Start()
    > while svc.Status <> ServiceControll erStatus.Runnin g
    > svc.Refresh()
    > End While
    > StatusService.R esume()
    > End Select
    > End Sub
    >[/color]


    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: Suspend/resume Thread problems

      Juky,
      In addition to the other comments.
      [color=blue]
      > When I try to resume it from the another function I have
      > some problems.[/color]
      What specifically is the problem? Remember if you suspend a thread, another
      thread needs to resume that thread. From your code you are suspending the
      thread on the same thread that is handling the event, then on the exact same
      thread you attempt to resume yourself. You put yourself to sleep, someone
      else will need to wake you!

      Also I would suggest you investigate ServiceControll er.WaitForStatu s. Using
      WaitForStatus may actually be what you want rather then Suspend & Resume!
      [color=blue]
      > Dim StatusService As Threading.Threa d
      > Private svc As ServiceControll er[/color]
      This seems like an Odd Ball solution. You have a Long verbose name for the
      Thread, yet a short obscure name for the ServiceControll er. In 6 months when
      you visit this code, I'm sure it will be far easier to understand if you
      used the same naming convention for both! Especially since "StatusServ ice"
      sounds like the name for a ServiceControll er or ServiceBase object, not a
      Thread object!


      Something like:
      [color=blue]
      > Private Sub LoopCheck()
      > While True[/color]

      ' TODO: Decide if this first wait really needed?
      svc.WaitForStat us(ServiceContr ollerStatus.Sto pPending)

      ' TODO: Decide if this first RaiseEvent really needed?[color=blue]
      > RaiseEvent StatusEventRais e
      > ServiceControll erStatus.StopPe nding)[/color]

      svc.WaitForStat us(ServiceContr ollerStatus.Sto pped)[color=blue]
      > RaiseEvent
      > StatusEventRais e(ServiceContro llerStatus.Stop ped)[/color]
      [color=blue]
      > End While
      > End Sub[/color]

      [color=blue]
      > Private Sub StatusEventRais eHandler(ByVal EventGenerated As Integer)
      > Static nr As Integer = 0
      >
      > Select Case EventGenerated
      > Case ServiceControll erStatus.Stoppe d
      > svc.Start()
      > Case ServiceControll erStatus.StopPe nding[/color]
      svc.WaitForStat us(ServiceContr ollerStatus.Sto pped)[color=blue]
      > svc.Start()[/color]
      svc.WaitForStat us(ServiceContr ollerStatus.Run ning)[color=blue]
      > End Select
      > End Sub[/color]

      The "problem" with WaitForStatus is that it can only wait for a single
      Status, so your LoopCheck, as you wrote it, does not make as much sense, I
      would consider using the WaitForStatus overload that expects a TimeSpan &
      have it time out, however this causes an exception... Having LoopCheck check
      for a single status may make more sense...

      Hope this helps
      Jay


      "juky" <juklingos@yaho o.com> wrote in message
      news:1108115225 .575130.294990@ o13g2000cwo.goo glegroups.com.. .[color=blue]
      > Hi all,
      >
      > I have a loop in the thread checking for a particular service status,
      > whenever the status changes to "stopped" a RaiseEvent is generated by
      > thread and another function runs. At the same time the thread is
      > suspended. When I try to resume it from the another function I have
      > some problems.
      >
      > Thank you.
      > Juky
      >
      > Here the code:
      >
      > Public Event StatusEventRais e(ByVal EventGenerated As Integer)
      >
      > Dim StatusService As Threading.Threa d
      > Private svc As ServiceControll er
      > ......
      >
      > Public Sub New()
      > ....
      > AddHandler StatusEventRais e, AddressOf StatusEventRais eHandler
      > StatusService = New Threading.Threa d(AddressOf LoopCheck)
      > StatusService.S tart()
      > ...
      > end Sub
      >
      > Private Sub LoopCheck()
      > While True
      > svc.Refresh()
      > Select Case svc.Status
      > Case ServiceControll erStatus.StopPe nding
      > RaiseEvent StatusEventRais e
      > ServiceControll erStatus.StopPe nding)
      > StatusService.S uspend()
      > Case ServiceControll erStatus.Stoppe d
      > RaiseEvent
      > StatusEventRais e(ServiceContro llerStatus.Stop ped)
      > Me.StatusServic e.Suspend()
      > End Select
      > End While
      > End Sub
      >
      >
      > Private Sub StatusEventRais eHandler(ByVal EventGenerated As Integer)
      > Static nr As Integer = 0
      >
      > Select Case EventGenerated
      > Case ServiceControll erStatus.Stoppe d
      > svc.Start()
      > StatusService.R esume()
      > Case ServiceControll erStatus.StopPe nding
      > While svc.Status <> ServiceControll erStatus.Stoppe d
      > svc.Refresh()
      > End While
      > svc.Start()
      > while svc.Status <> ServiceControll erStatus.Runnin g
      > svc.Refresh()
      > End While
      > StatusService.R esume()
      > End Select
      > End Sub
      >[/color]


      Comment

      Working...