Threading logical problem??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James A Taber

    Threading logical problem??

    What I want to implement is to restart certain thread every 24 hours...

    sample code:

    Dim T() As Thread
    Dim L() As Lib1.Class1
    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click

    Dim i As Integer = 0
    For i = 0 To 2

    ReDim Preserve T(i)
    ReDim Preserve L(i)

    L(i) = New Lib1.Class1
    L(i).m1 = i

    T(i) = New Thread(AddressO f L(i).StartTimer ) '<--- Class1
    contains a timer to do the required task.
    T(i).Start()

    Trace.WriteLine (T(i).ThreadSta te.ToString) '<---- Unstarted ??
    Next

    End Sub







  • Armin Zingler

    #2
    Re: Threading logical problem??

    "James A Taber" <icecool_6@hotm ail.com> schrieb[color=blue]
    > What I want to implement is to restart certain thread every 24
    > hours...
    >
    > sample code:
    >
    > [...]
    > Trace.WriteLine (T(i).ThreadSta te.ToString) '<----
    > Unstarted ??[/color]

    As long as *this* thread runs, the other one might still be unstarted, but
    the OS will start it when it is it's turn.
    [color=blue]
    > Next
    >
    > End Sub[/color]

    Maybe I did not understand the problem, yet. Isn't starting a
    System.Timers.T imer sufficient?


    --
    Armin




    Comment

    • James A Taber

      #3
      Re: Threading logical problem??

      Hi Armin,

      Thanks for you reply,

      The timer works fine... what I am experiencing is that somtimes some of the
      thread seams to stop working ... thats why I want to restart all the threads
      in the application after a certain time....

      -James A Taber




      "Armin Zingler" <az.nospam@free net.de> wrote in message
      news:%233ycEFd2 DHA.1764@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > "James A Taber" <icecool_6@hotm ail.com> schrieb[color=green]
      > > What I want to implement is to restart certain thread every 24
      > > hours...
      > >
      > > sample code:
      > >
      > > [...]
      > > Trace.WriteLine (T(i).ThreadSta te.ToString) '<----
      > > Unstarted ??[/color]
      >
      > As long as *this* thread runs, the other one might still be unstarted, but
      > the OS will start it when it is it's turn.
      >[color=green]
      > > Next
      > >
      > > End Sub[/color]
      >
      > Maybe I did not understand the problem, yet. Isn't starting a
      > System.Timers.T imer sufficient?
      >
      >
      > --
      > Armin
      >
      > http://www.plig.net/nnq/nquote.html
      > http://www.netmeister.org/news/learn2quote.html
      >[/color]


      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: Threading logical problem??

        James,
        In addition to Armin's comments, have you tried using Thread.Sleep(0) to
        give the second thread a chance to start?

        Something like:[color=blue]
        > T(i) = New Thread(AddressO f L(i).StartTimer ) '<--- Class1
        > contains a timer to do the required task.
        > T(i).Start()[/color]

        Thread.Sleep(0)
        [color=blue]
        >
        > Trace.WriteLine (T(i).ThreadSta te.ToString) '<---- Unstarted ??[/color]

        Its not that your second thread did not start as much as your first thread
        is not sharing the CPU. ;-)

        Also in addition to restarting threads, you may want to consider restarting
        AppDomains also.

        Hope this helps
        Jay

        "James A Taber" <icecool_6@hotm ail.com> wrote in message
        news:e33uS6c2DH A.2184@TK2MSFTN GP12.phx.gbl...[color=blue]
        > What I want to implement is to restart certain thread every 24 hours...
        >
        > sample code:
        >
        > Dim T() As Thread
        > Dim L() As Lib1.Class1
        > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
        > System.EventArg s) Handles Button1.Click
        >
        > Dim i As Integer = 0
        > For i = 0 To 2
        >
        > ReDim Preserve T(i)
        > ReDim Preserve L(i)
        >
        > L(i) = New Lib1.Class1
        > L(i).m1 = i
        >
        > T(i) = New Thread(AddressO f L(i).StartTimer ) '<--- Class1
        > contains a timer to do the required task.
        > T(i).Start()
        >
        > Trace.WriteLine (T(i).ThreadSta te.ToString) '<---- Unstarted ??
        > Next
        >
        > End Sub
        >
        >
        >
        >
        >
        >
        >[/color]


        Comment

        • James A Taber

          #5
          Re: Threading logical problem??

          Hi Jay,

          I have tried use Sleep(3000) but it didnt make any difference ... is it
          possible that the timer in Class1 one is running in its own thread?

          hm... restart Appdomain ? how can I do that ?

          is this true --> Restart Appdomain = Initialize Application

          James A Taber


          "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
          news:esuk3cd2DH A.2556@TK2MSFTN GP10.phx.gbl...[color=blue]
          > James,
          > In addition to Armin's comments, have you tried using Thread.Sleep(0) to
          > give the second thread a chance to start?
          >
          > Something like:[color=green]
          > > T(i) = New Thread(AddressO f L(i).StartTimer ) '<--- Class1
          > > contains a timer to do the required task.
          > > T(i).Start()[/color]
          >
          > Thread.Sleep(0)
          >[color=green]
          > >
          > > Trace.WriteLine (T(i).ThreadSta te.ToString) '<---- Unstarted[/color][/color]
          ??[color=blue]
          >
          > Its not that your second thread did not start as much as your first thread
          > is not sharing the CPU. ;-)
          >
          > Also in addition to restarting threads, you may want to consider[/color]
          restarting[color=blue]
          > AppDomains also.
          >
          > Hope this helps
          > Jay
          >
          > "James A Taber" <icecool_6@hotm ail.com> wrote in message
          > news:e33uS6c2DH A.2184@TK2MSFTN GP12.phx.gbl...[color=green]
          > > What I want to implement is to restart certain thread every 24 hours...
          > >
          > > sample code:
          > >
          > > Dim T() As Thread
          > > Dim L() As Lib1.Class1
          > > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
          > > System.EventArg s) Handles Button1.Click
          > >
          > > Dim i As Integer = 0
          > > For i = 0 To 2
          > >
          > > ReDim Preserve T(i)
          > > ReDim Preserve L(i)
          > >
          > > L(i) = New Lib1.Class1
          > > L(i).m1 = i
          > >
          > > T(i) = New Thread(AddressO f L(i).StartTimer ) '<--- Class1
          > > contains a timer to do the required task.
          > > T(i).Start()
          > >
          > > Trace.WriteLine (T(i).ThreadSta te.ToString) '<---- Unstarted[/color][/color]
          ??[color=blue][color=green]
          > > Next
          > >
          > > End Sub
          > >
          > >
          > >
          > >
          > >
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: Threading logical problem??

            James,[color=blue]
            > is it
            > possible that the timer in Class1 one is running in its own thread?[/color]
            Hard to say as you don't say what type of timer is in Class1. Remember there
            are 3 distinct Timer classes in .NET.

            Hope this helps
            Jay

            "James A Taber" <icecool_6@hotm ail.com> wrote in message
            news:us6CY2d2DH A.4032@tk2msftn gp13.phx.gbl...[color=blue]
            > Hi Jay,
            >
            > I have tried use Sleep(3000) but it didnt make any difference ... is it
            > possible that the timer in Class1 one is running in its own thread?
            >
            > hm... restart Appdomain ? how can I do that ?
            >
            > is this true --> Restart Appdomain = Initialize Application
            >
            > James A Taber
            >
            >
            > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
            > news:esuk3cd2DH A.2556@TK2MSFTN GP10.phx.gbl...[color=green]
            > > James,
            > > In addition to Armin's comments, have you tried using Thread.Sleep(0) to
            > > give the second thread a chance to start?
            > >
            > > Something like:[color=darkred]
            > > > T(i) = New Thread(AddressO f L(i).StartTimer ) '<--- Class1
            > > > contains a timer to do the required task.
            > > > T(i).Start()[/color]
            > >
            > > Thread.Sleep(0)
            > >[color=darkred]
            > > >
            > > > Trace.WriteLine (T(i).ThreadSta te.ToString) '<----[/color][/color][/color]
            Unstarted[color=blue]
            > ??[color=green]
            > >
            > > Its not that your second thread did not start as much as your first[/color][/color]
            thread[color=blue][color=green]
            > > is not sharing the CPU. ;-)
            > >
            > > Also in addition to restarting threads, you may want to consider[/color]
            > restarting[color=green]
            > > AppDomains also.
            > >
            > > Hope this helps
            > > Jay
            > >
            > > "James A Taber" <icecool_6@hotm ail.com> wrote in message
            > > news:e33uS6c2DH A.2184@TK2MSFTN GP12.phx.gbl...[color=darkred]
            > > > What I want to implement is to restart certain thread every 24[/color][/color][/color]
            hours...[color=blue][color=green][color=darkred]
            > > >
            > > > sample code:
            > > >
            > > > Dim T() As Thread
            > > > Dim L() As Lib1.Class1
            > > > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e[/color][/color][/color]
            As[color=blue][color=green][color=darkred]
            > > > System.EventArg s) Handles Button1.Click
            > > >
            > > > Dim i As Integer = 0
            > > > For i = 0 To 2
            > > >
            > > > ReDim Preserve T(i)
            > > > ReDim Preserve L(i)
            > > >
            > > > L(i) = New Lib1.Class1
            > > > L(i).m1 = i
            > > >
            > > > T(i) = New Thread(AddressO f L(i).StartTimer ) '<--- Class1
            > > > contains a timer to do the required task.
            > > > T(i).Start()
            > > >
            > > > Trace.WriteLine (T(i).ThreadSta te.ToString) '<----[/color][/color][/color]
            Unstarted[color=blue]
            > ??[color=green][color=darkred]
            > > > Next
            > > >
            > > > End Sub
            > > >
            > > >
            > > >
            > > >
            > > >
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: Threading logical problem??

              James,
              Yes restarting an AppDomain is effectively the same as re-initializing your
              app, however you can control what parts of your app are running in which
              AppDomain.

              I don't have a clean example, to "restart" an AppDomain, you need to create
              a new AppDomain and start execution there, letting the current one finish. I
              would start by looking at the System.AppDomai n class and its Load & Unload
              methods...

              Hope this helps
              Jay

              "James A Taber" <icecool_6@hotm ail.com> wrote in message
              news:us6CY2d2DH A.4032@tk2msftn gp13.phx.gbl...[color=blue]
              > Hi Jay,
              >
              > I have tried use Sleep(3000) but it didnt make any difference ... is it
              > possible that the timer in Class1 one is running in its own thread?
              >
              > hm... restart Appdomain ? how can I do that ?
              >
              > is this true --> Restart Appdomain = Initialize Application
              >
              > James A Taber
              >
              >
              > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
              > news:esuk3cd2DH A.2556@TK2MSFTN GP10.phx.gbl...[color=green]
              > > James,
              > > In addition to Armin's comments, have you tried using Thread.Sleep(0) to
              > > give the second thread a chance to start?
              > >
              > > Something like:[color=darkred]
              > > > T(i) = New Thread(AddressO f L(i).StartTimer ) '<--- Class1
              > > > contains a timer to do the required task.
              > > > T(i).Start()[/color]
              > >
              > > Thread.Sleep(0)
              > >[color=darkred]
              > > >
              > > > Trace.WriteLine (T(i).ThreadSta te.ToString) '<----[/color][/color][/color]
              Unstarted[color=blue]
              > ??[color=green]
              > >
              > > Its not that your second thread did not start as much as your first[/color][/color]
              thread[color=blue][color=green]
              > > is not sharing the CPU. ;-)
              > >
              > > Also in addition to restarting threads, you may want to consider[/color]
              > restarting[color=green]
              > > AppDomains also.
              > >
              > > Hope this helps
              > > Jay
              > >
              > > "James A Taber" <icecool_6@hotm ail.com> wrote in message
              > > news:e33uS6c2DH A.2184@TK2MSFTN GP12.phx.gbl...[color=darkred]
              > > > What I want to implement is to restart certain thread every 24[/color][/color][/color]
              hours...[color=blue][color=green][color=darkred]
              > > >
              > > > sample code:
              > > >
              > > > Dim T() As Thread
              > > > Dim L() As Lib1.Class1
              > > > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e[/color][/color][/color]
              As[color=blue][color=green][color=darkred]
              > > > System.EventArg s) Handles Button1.Click
              > > >
              > > > Dim i As Integer = 0
              > > > For i = 0 To 2
              > > >
              > > > ReDim Preserve T(i)
              > > > ReDim Preserve L(i)
              > > >
              > > > L(i) = New Lib1.Class1
              > > > L(i).m1 = i
              > > >
              > > > T(i) = New Thread(AddressO f L(i).StartTimer ) '<--- Class1
              > > > contains a timer to do the required task.
              > > > T(i).Start()
              > > >
              > > > Trace.WriteLine (T(i).ThreadSta te.ToString) '<----[/color][/color][/color]
              Unstarted[color=blue]
              > ??[color=green][color=darkred]
              > > > Next
              > > >
              > > > End Sub
              > > >
              > > >
              > > >
              > > >
              > > >
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • James A Taber

                #8
                Re: Threading logical problem??

                Thanks for helping me out Jay...

                This is the Timer iam using

                System.Timers.T imer

                I will look more into the Appdomain "feature" later tonigh...

                Thanks again
                -James A Taber


                "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                news:uNGEvUe2DH A.2528@TK2MSFTN GP10.phx.gbl...[color=blue]
                > James,[color=green]
                > > is it
                > > possible that the timer in Class1 one is running in its own thread?[/color]
                > Hard to say as you don't say what type of timer is in Class1. Remember[/color]
                there[color=blue]
                > are 3 distinct Timer classes in .NET.
                >
                > Hope this helps
                > Jay
                >
                > "James A Taber" <icecool_6@hotm ail.com> wrote in message
                > news:us6CY2d2DH A.4032@tk2msftn gp13.phx.gbl...[color=green]
                > > Hi Jay,
                > >
                > > I have tried use Sleep(3000) but it didnt make any difference ... is[/color][/color]
                it[color=blue][color=green]
                > > possible that the timer in Class1 one is running in its own thread?
                > >
                > > hm... restart Appdomain ? how can I do that ?
                > >
                > > is this true --> Restart Appdomain = Initialize Application
                > >
                > > James A Taber
                > >
                > >
                > > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in[/color][/color]
                message[color=blue][color=green]
                > > news:esuk3cd2DH A.2556@TK2MSFTN GP10.phx.gbl...[color=darkred]
                > > > James,
                > > > In addition to Armin's comments, have you tried using Thread.Sleep(0)[/color][/color][/color]
                to[color=blue][color=green][color=darkred]
                > > > give the second thread a chance to start?
                > > >
                > > > Something like:
                > > > > T(i) = New Thread(AddressO f L(i).StartTimer ) '<---[/color][/color][/color]
                Class1[color=blue][color=green][color=darkred]
                > > > > contains a timer to do the required task.
                > > > > T(i).Start()
                > > >
                > > > Thread.Sleep(0)
                > > >
                > > > >
                > > > > Trace.WriteLine (T(i).ThreadSta te.ToString) '<----[/color][/color]
                > Unstarted[color=green]
                > > ??[color=darkred]
                > > >
                > > > Its not that your second thread did not start as much as your first[/color][/color]
                > thread[color=green][color=darkred]
                > > > is not sharing the CPU. ;-)
                > > >
                > > > Also in addition to restarting threads, you may want to consider[/color]
                > > restarting[color=darkred]
                > > > AppDomains also.
                > > >
                > > > Hope this helps
                > > > Jay
                > > >
                > > > "James A Taber" <icecool_6@hotm ail.com> wrote in message
                > > > news:e33uS6c2DH A.2184@TK2MSFTN GP12.phx.gbl...
                > > > > What I want to implement is to restart certain thread every 24[/color][/color]
                > hours...[color=green][color=darkred]
                > > > >
                > > > > sample code:
                > > > >
                > > > > Dim T() As Thread
                > > > > Dim L() As Lib1.Class1
                > > > > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e[/color][/color]
                > As[color=green][color=darkred]
                > > > > System.EventArg s) Handles Button1.Click
                > > > >
                > > > > Dim i As Integer = 0
                > > > > For i = 0 To 2
                > > > >
                > > > > ReDim Preserve T(i)
                > > > > ReDim Preserve L(i)
                > > > >
                > > > > L(i) = New Lib1.Class1
                > > > > L(i).m1 = i
                > > > >
                > > > > T(i) = New Thread(AddressO f L(i).StartTimer ) '<---[/color][/color][/color]
                Class1[color=blue][color=green][color=darkred]
                > > > > contains a timer to do the required task.
                > > > > T(i).Start()
                > > > >
                > > > > Trace.WriteLine (T(i).ThreadSta te.ToString) '<----[/color][/color]
                > Unstarted[color=green]
                > > ??[color=darkred]
                > > > > Next
                > > > >
                > > > > End Sub
                > > > >
                > > > >
                > > > >
                > > > >
                > > > >
                > > > >
                > > > >
                > > >
                > > >[/color]
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • Jay B. Harlow [MVP - Outlook]

                  #9
                  Re: Threading logical problem??

                  James,
                  System.Timers.T imer will raise its event in a new thread (via the
                  ThreadPool), there is no need to create a thread to explicitly handle it.

                  For details on the Timers.Timer and the other 2 timers in .NET check out
                  this recent article:

                  Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


                  Hope this helps
                  Jay

                  "James A Taber" <icecool_6@hotm ail.com> wrote in message
                  news:eJR7C2e2DH A.1272@TK2MSFTN GP12.phx.gbl...[color=blue]
                  > Thanks for helping me out Jay...
                  >
                  > This is the Timer iam using
                  >
                  > System.Timers.T imer
                  >
                  > I will look more into the Appdomain "feature" later tonigh...
                  >
                  > Thanks again
                  > -James A Taber
                  >
                  >
                  > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                  > news:uNGEvUe2DH A.2528@TK2MSFTN GP10.phx.gbl...[color=green]
                  > > James,[color=darkred]
                  > > > is it
                  > > > possible that the timer in Class1 one is running in its own thread?[/color]
                  > > Hard to say as you don't say what type of timer is in Class1. Remember[/color]
                  > there[color=green]
                  > > are 3 distinct Timer classes in .NET.
                  > >
                  > > Hope this helps
                  > > Jay
                  > >
                  > > "James A Taber" <icecool_6@hotm ail.com> wrote in message
                  > > news:us6CY2d2DH A.4032@tk2msftn gp13.phx.gbl...[color=darkred]
                  > > > Hi Jay,
                  > > >
                  > > > I have tried use Sleep(3000) but it didnt make any difference ... is[/color][/color]
                  > it[color=green][color=darkred]
                  > > > possible that the timer in Class1 one is running in its own thread?
                  > > >
                  > > > hm... restart Appdomain ? how can I do that ?
                  > > >
                  > > > is this true --> Restart Appdomain = Initialize Application
                  > > >
                  > > > James A Taber
                  > > >
                  > > >
                  > > > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in[/color][/color]
                  > message[color=green][color=darkred]
                  > > > news:esuk3cd2DH A.2556@TK2MSFTN GP10.phx.gbl...
                  > > > > James,
                  > > > > In addition to Armin's comments, have you tried using[/color][/color][/color]
                  Thread.Sleep(0)[color=blue]
                  > to[color=green][color=darkred]
                  > > > > give the second thread a chance to start?
                  > > > >
                  > > > > Something like:
                  > > > > > T(i) = New Thread(AddressO f L(i).StartTimer ) '<---[/color][/color]
                  > Class1[color=green][color=darkred]
                  > > > > > contains a timer to do the required task.
                  > > > > > T(i).Start()
                  > > > >
                  > > > > Thread.Sleep(0)
                  > > > >
                  > > > > >
                  > > > > > Trace.WriteLine (T(i).ThreadSta te.ToString) '<----[/color]
                  > > Unstarted[color=darkred]
                  > > > ??
                  > > > >
                  > > > > Its not that your second thread did not start as much as your first[/color]
                  > > thread[color=darkred]
                  > > > > is not sharing the CPU. ;-)
                  > > > >
                  > > > > Also in addition to restarting threads, you may want to consider
                  > > > restarting
                  > > > > AppDomains also.
                  > > > >
                  > > > > Hope this helps
                  > > > > Jay
                  > > > >
                  > > > > "James A Taber" <icecool_6@hotm ail.com> wrote in message
                  > > > > news:e33uS6c2DH A.2184@TK2MSFTN GP12.phx.gbl...
                  > > > > > What I want to implement is to restart certain thread every 24[/color]
                  > > hours...[color=darkred]
                  > > > > >
                  > > > > > sample code:
                  > > > > >
                  > > > > > Dim T() As Thread
                  > > > > > Dim L() As Lib1.Class1
                  > > > > > Private Sub Button1_Click(B yVal sender As System.Object, ByVal[/color][/color][/color]
                  e[color=blue][color=green]
                  > > As[color=darkred]
                  > > > > > System.EventArg s) Handles Button1.Click
                  > > > > >
                  > > > > > Dim i As Integer = 0
                  > > > > > For i = 0 To 2
                  > > > > >
                  > > > > > ReDim Preserve T(i)
                  > > > > > ReDim Preserve L(i)
                  > > > > >
                  > > > > > L(i) = New Lib1.Class1
                  > > > > > L(i).m1 = i
                  > > > > >
                  > > > > > T(i) = New Thread(AddressO f L(i).StartTimer ) '<---[/color][/color]
                  > Class1[color=green][color=darkred]
                  > > > > > contains a timer to do the required task.
                  > > > > > T(i).Start()
                  > > > > >
                  > > > > > Trace.WriteLine (T(i).ThreadSta te.ToString) '<----[/color]
                  > > Unstarted[color=darkred]
                  > > > ??
                  > > > > > Next
                  > > > > >
                  > > > > > End Sub
                  > > > > >
                  > > > > >
                  > > > > >
                  > > > > >
                  > > > > >
                  > > > > >
                  > > > > >
                  > > > >
                  > > > >
                  > > >
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  Working...