StackOverflowException in Thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QnViYmE=?=

    #1

    StackOverflowException in Thread

    I've written code that uses a thread to read a 70K line CSV file one line at
    a time, however, after about 9 to 10 thousand lines into the file I get a
    StackOverflowEx ception while the thread tries to update a counter.

    I'm sure I'm doing this correctly but if anyone can tell me different I'd
    appreciate it.

    ssStrip is a StatusStrip on the main form.

    Private WithEvents m_tHistorical As BackgroundWorke r = New BackgroundWorke r()

    Private Sub cmdRun_Click(By Val sender As System.Object, ByVal e As
    System.EventArg s) Handles cmdRun.Click
    Me.ssStrip.Item s(1).Visible = True
    Me.ssStrip.Item s(2).Visible = True
    m_tHistorical.R unWorkerAsync(N ew
    StreamReader("S SG_Historical-29-03-2007.csv", False))
    End Sub

    Private Sub m_tHistorical_D oWork(ByVal sender As Object, ByVal e As
    System.Componen tModel.DoWorkEv entArgs) Handles m_tHistorical.D oWork
    e.Result = ReadHistoricalC SV(CType(sender , BackgroundWorke r), e)
    End Sub

    Private Function ReadHistoricalC SV(ByVal W As BackgroundWorke r, ByVal e
    As DoWorkEventArgs ) As Boolean
    Dim S As StreamReader = CType(e.Argumen t(), StreamReader), Result As
    Boolean = False
    Static Rows As Integer = 0
    If (W.Cancellation Pending()) Then
    e.Cancel = True
    Else
    Result = S.EndOfStream()
    If (Result) Then
    S.Close()
    Else
    S.ReadLine()
    Rows += 1
    UpdateHCSVRows( Rows) ' <-- This is the line with the
    StackOverFlow
    Result = ReadHistoricalC SV(W, e)
    End If
    End If
    Return Result
    End Function

    Private Sub UpdateHCSVRows( ByVal R As Integer)
    If (Me.ssStrip.Inv okeRequired()) Then
    Me.Invoke(New CSVHRowsCallbac k(AddressOf UpdateHCSVRows) , New
    Object() {R})
    Else
    Me.ssStrip.Item s(1).Text = FormatNumber(R, 0,
    TriState.UseDef ault, TriState.UseDef ault, TriState.True)
    End If
    End Sub

    --
    Regards,

    Bubba

  • Chris Dunaway

    #2
    Re: StackOverflowEx ception in Thread

    On Mar 30, 8:22 am, Bubba <B...@discussio ns.microsoft.co mwrote:
    I've written code that uses a thread to read a 70K line CSV file one line at
    a time, however, after about 9 to 10 thousand lines into the file I get a
    StackOverflowEx ception while the thread tries to update a counter.
    >
    <snip>
    Private Sub UpdateHCSVRows( ByVal R As Integer)
    If (Me.ssStrip.Inv okeRequired()) Then
    Me.Invoke(New CSVHRowsCallbac k(AddressOf UpdateHCSVRows) , New
    Object() {R})
    Else
    Me.ssStrip.Item s(1).Text = FormatNumber(R, 0,
    TriState.UseDef ault, TriState.UseDef ault, TriState.True)
    End If
    End Sub
    >
    Your Sub above is calling itself recursively! That's why you are
    getting the StackOverflowEx ception. Try moving the InvokeRequired
    check *outside* the Sub.

    Chris

    Comment

    • =?Utf-8?B?QnViYmE=?=

      #3
      Re: StackOverflowEx ception in Thread

      "Chris Dunaway" wrote:
      On Mar 30, 8:22 am, Bubba <B...@discussio ns.microsoft.co mwrote:
      I've written code that uses a thread to read a 70K line CSV file one line at
      a time, however, after about 9 to 10 thousand lines into the file I get a
      StackOverflowEx ception while the thread tries to update a counter.
      >
      <snip>
      >
      <snip/>
      Your Sub above is calling itself recursively! That's why you are
      getting the StackOverflowEx ception. Try moving the InvokeRequired
      check *outside* the Sub.
      >
      Not to sure what you mean here as this is required when updating the UI from
      a different thread.

      What I did try was to comment out all of the lines apart from
      Me.ssStrip.Item s... one and it ran updating the row count until it got to the
      10K mark and again failed with the same error.

      --
      Regards,

      Bubba


      Comment

      • Smokey Grindle

        #4
        Re: StackOverflowEx ception in Thread

        Me.Invoke(New CSVHRowsCallbac k(AddressOf UpdateHCSVRows) , New
        Object() {R})

        your code there is callign itself, which puts a new stack frame onto the
        stack, eventually it will overflow the stack because you got it stuck in a
        never ending loop


        "Bubba" <Bubba@discussi ons.microsoft.c omwrote in message
        news:FAC07EBC-765D-4901-BB25-021B28BC6716@mi crosoft.com...
        "Chris Dunaway" wrote:
        >
        >On Mar 30, 8:22 am, Bubba <B...@discussio ns.microsoft.co mwrote:
        I've written code that uses a thread to read a 70K line CSV file one
        line at
        a time, however, after about 9 to 10 thousand lines into the file I get
        a
        StackOverflowEx ception while the thread tries to update a counter.
        >
        >>
        ><snip>
        >>
        <snip/>
        >
        >Your Sub above is calling itself recursively! That's why you are
        >getting the StackOverflowEx ception. Try moving the InvokeRequired
        >check *outside* the Sub.
        >>
        >
        Not to sure what you mean here as this is required when updating the UI
        from
        a different thread.
        >
        What I did try was to comment out all of the lines apart from
        Me.ssStrip.Item s... one and it ran updating the row count until it got to
        the
        10K mark and again failed with the same error.
        >
        --
        Regards,
        >
        Bubba
        >
        >

        Comment

        • =?Utf-8?B?QnViYmE=?=

          #5
          Re: StackOverflowEx ception in Thread

          "Smokey Grindle" wrote:
          Me.Invoke(New CSVHRowsCallbac k(AddressOf UpdateHCSVRows) , New
          Object() {R})
          >
          your code there is callign itself, which puts a new stack frame onto the
          stack, eventually it will overflow the stack because you got it stuck in a
          never ending loop
          Fair enough I can see this and understand, but I was only following what I
          saw on the MSDN. Does anyone know how I should re-write this to fix it?


          --
          Regards,

          Bubba

          Comment

          • =?Utf-8?B?QnViYmE=?=

            #6
            Re: StackOverflowEx ception in Thread

            "Bubba" wrote:
            "Smokey Grindle" wrote:
            >
            Me.Invoke(New CSVHRowsCallbac k(AddressOf UpdateHCSVRows) , New
            Object() {R})

            your code there is callign itself, which puts a new stack frame onto the
            stack, eventually it will overflow the stack because you got it stuck in a
            never ending loop
            >
            Fair enough I can see this and understand, but I was only following what I
            saw on the MSDN. Does anyone know how I should re-write this to fix it?
            >
            Ok with that said I've changed the code to the following:

            Private Function ReadHistoricalC SV(ByVal W As BackgroundWorke r, ByVal e
            As DoWorkEventArgs ) As Boolean
            Dim S As StreamReader = CType(e.Argumen t(), StreamReader), Result As
            Boolean = False
            Static Rows As Integer = 0
            If (W.Cancellation Pending()) Then
            e.Cancel = True
            Else
            Result = S.EndOfStream()
            If (Result) Then
            S.Close()
            Else
            S.ReadLine()
            Rows += 1
            Me.ssStrip.Item s(1).Text = FormatNumber(Ro ws, 0,
            TriState.UseDef ault, TriState.UseDef ault, TriState.True)
            Result = ReadHistoricalC SV(W, e)
            End If
            End If
            Return Result
            End Function

            And I still get an error, again this procedure is calling itself, as is
            shown in the MSDN so I'm assuming that this is the correct way, as it is the
            only way I can see that allows you to cancel the thread's execution.

            I'm at a loss with this one but I really need to get this resolved as I have
            another 2 files just as large that need proccessing. I'm using threads so
            that the processing of all three files can be done simultaneously to speed up
            the process and make use of the quad processor based machine it runs on.

            Can anyone help me out here?
            --
            Regards,

            Bubba

            Comment

            • Stephany Young

              #7
              Re: StackOverflowEx ception in Thread

              Well, the point is that you're still calling 'yourself' recursively thus
              causing the stack to blow out.

              You need to work on the 'keep it simple' principle and not turn a simple
              task into a complicated one.

              Try:

              Private Sub ReadHistoricalC SV(ByVal W As BackgroundWorke r, ByVal e As
              DoWorkEventArgs )

              Dim Rows As Integer = 0

              Dim S As StreamReader = CType(e.Argumen t, StreamReader)

              While Not S.EndOfStream AndAlso Not W.CancellationP ending
              S.ReadLine()
              Rows += 1
              Me.ssStrip.Item s(1).Text = FormatNumber(Ro ws, 0, TriState.UseDef ault,
              TriState.UseDef ault, TriState.True)
              Loop

              If W.CancellationP ending Then
              e.Cancel
              Else
              S.Close()
              End If

              End Sub

              Note that the method it is now a Sub rather than a Function because it does
              not need a return value.

              The method loops until EndOfStream is true on the StreamReader object or
              CancellationPen ding is set on the BackgroundWorke r object, whichever occurs
              first.


              "Bubba" <Bubba@discussi ons.microsoft.c omwrote in message
              news:BC0B8A50-3111-4475-957D-B1E1C6B711CD@mi crosoft.com...
              "Bubba" wrote:
              >
              >"Smokey Grindle" wrote:
              >>
              Me.Invoke(New CSVHRowsCallbac k(AddressOf UpdateHCSVRows) , New
              Object() {R})
              >
              your code there is callign itself, which puts a new stack frame onto
              the
              stack, eventually it will overflow the stack because you got it stuck
              in a
              never ending loop
              >>
              >Fair enough I can see this and understand, but I was only following what
              >I
              >saw on the MSDN. Does anyone know how I should re-write this to fix it?
              >>
              >
              Ok with that said I've changed the code to the following:
              >
              Private Function ReadHistoricalC SV(ByVal W As BackgroundWorke r, ByVal e
              As DoWorkEventArgs ) As Boolean
              Dim S As StreamReader = CType(e.Argumen t(), StreamReader), Result
              As
              Boolean = False
              Static Rows As Integer = 0
              If (W.Cancellation Pending()) Then
              e.Cancel = True
              Else
              Result = S.EndOfStream()
              If (Result) Then
              S.Close()
              Else
              S.ReadLine()
              Rows += 1
              Me.ssStrip.Item s(1).Text = FormatNumber(Ro ws, 0,
              TriState.UseDef ault, TriState.UseDef ault, TriState.True)
              Result = ReadHistoricalC SV(W, e)
              End If
              End If
              Return Result
              End Function
              >
              And I still get an error, again this procedure is calling itself, as is
              shown in the MSDN so I'm assuming that this is the correct way, as it is
              the
              only way I can see that allows you to cancel the thread's execution.
              >
              I'm at a loss with this one but I really need to get this resolved as I
              have
              another 2 files just as large that need proccessing. I'm using threads so
              that the processing of all three files can be done simultaneously to speed
              up
              the process and make use of the quad processor based machine it runs on.
              >
              Can anyone help me out here?
              --
              Regards,
              >
              Bubba
              >

              Comment

              • =?Utf-8?B?QnViYmE=?=

                #8
                Re: StackOverflowEx ception in Thread

                "Stephany Young" wrote:
                Well, the point is that you're still calling 'yourself' recursively thus
                causing the stack to blow out.
                >
                You need to work on the 'keep it simple' principle and not turn a simple
                task into a complicated one.
                >
                Try:
                >
                Private Sub ReadHistoricalC SV(ByVal W As BackgroundWorke r, ByVal e As
                DoWorkEventArgs )
                >
                Dim Rows As Integer = 0
                >
                Dim S As StreamReader = CType(e.Argumen t, StreamReader)
                >
                While Not S.EndOfStream AndAlso Not W.CancellationP ending
                S.ReadLine()
                Rows += 1
                Me.ssStrip.Item s(1).Text = FormatNumber(Ro ws, 0, TriState.UseDef ault,
                TriState.UseDef ault, TriState.True)
                Loop
                >
                If W.CancellationP ending Then
                e.Cancel
                Else
                S.Close()
                End If
                >
                End Sub
                >
                Note that the method it is now a Sub rather than a Function because it does
                not need a return value.
                >
                The method loops until EndOfStream is true on the StreamReader object or
                CancellationPen ding is set on the BackgroundWorke r object, whichever occurs
                first.
                Thanks Stephany,

                I see the logic of this approach and with a bit of tweaking it works,
                however I'm not to sure as to why the MSDN examples the use of re-entrant
                threads. I guess there must be a reason for it, but one that escapes me for
                now.

                Comment

                • Michel Posseth  [MCP]

                  #9
                  Re: StackOverflowEx ception in Thread


                  When i write multithreaded proggy`s i use seperate methods for UI updating
                  now in these methods you can use invokerequired without anny problems
                  of reentering your worker method


                  regards

                  Michel [MCP]






                  "Bubba" <Bubba@discussi ons.microsoft.c omschreef in bericht
                  news:D7AF4CFD-985E-4CBE-873A-29B13F0C8698@mi crosoft.com...
                  "Stephany Young" wrote:
                  >
                  >Well, the point is that you're still calling 'yourself' recursively thus
                  >causing the stack to blow out.
                  >>
                  >You need to work on the 'keep it simple' principle and not turn a simple
                  >task into a complicated one.
                  >>
                  >Try:
                  >>
                  > Private Sub ReadHistoricalC SV(ByVal W As BackgroundWorke r, ByVal e As
                  >DoWorkEventArg s)
                  >>
                  > Dim Rows As Integer = 0
                  >>
                  > Dim S As StreamReader = CType(e.Argumen t, StreamReader)
                  >>
                  > While Not S.EndOfStream AndAlso Not W.CancellationP ending
                  > S.ReadLine()
                  > Rows += 1
                  > Me.ssStrip.Item s(1).Text = FormatNumber(Ro ws, 0,
                  >TriState.UseDe fault,
                  >TriState.UseDe fault, TriState.True)
                  > Loop
                  >>
                  > If W.CancellationP ending Then
                  > e.Cancel
                  > Else
                  > S.Close()
                  > End If
                  >>
                  > End Sub
                  >>
                  >Note that the method it is now a Sub rather than a Function because it
                  >does
                  >not need a return value.
                  >>
                  >The method loops until EndOfStream is true on the StreamReader object or
                  >CancellationPe nding is set on the BackgroundWorke r object, whichever
                  >occurs
                  >first.
                  >
                  Thanks Stephany,
                  >
                  I see the logic of this approach and with a bit of tweaking it works,
                  however I'm not to sure as to why the MSDN examples the use of re-entrant
                  threads. I guess there must be a reason for it, but one that escapes me
                  for
                  now.
                  >

                  Comment

                  Working...