Share via


System.Threading.Tasks.Task.ThrowIfExceptional

Question

Friday, September 21, 2018 9:08 AM

Hi, I have this error message:

Error Source: mscorlib

 Error Site: Void ThrowIfExceptional(Boolean)
 Error Message: Si sono verificati uno o più errori.

Stack: 
   in System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   in System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   in System.Threading.Tasks.Task.Wait()
   in System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
   in System.Threading.Tasks.Parallel.For(Int32 fromInclusive, Int32 toExclusive, Action`2 body)

Here my code where I have used Parallel.For:

Parallel.For(1, NumMaxTentSimu, Sub(i, loopState)
                                                Try
                                                    
                                                  RicalcolaDettagli()

                                                    If Act1 > ValMaxPiuAlto Then
                                                        ValMaxPiuAlto = Act1
                                                        'TextBoxValMaxPiuAlto.Text = ValMaxPiuAlto.ToString("#,##")
                                                    End If
                                                    If Act1 > Act Then
                                                        Act = Act1

                                                       
                                                        If ChkBoxSimulazione.Checked = True Then
                                                            NumConcNextWin = 0
                                                            TrovatoNumConcNextWin = False
                                                            For x = ConcDaAnalizzare - 1 To ConcDaAnalizzare + ConcSimulazione - 1
                                                                If Dettagli(x).Pnt = False And TrovatoNumConcNextWin = False Then
                                                                    NumConcNextWin += 1
                                                                Else
                                                                    TrovatoNumConcNextWin = True
                                                                    Exit For
                                                                End If
                                                            Next x
                                                        End If

                                                        'ScriviArraySuValMaxResultsHeaders
                                                        sb.Append("INSERT INTO ValMaxResultsHeaders (TipoRicerca, ProgrSimu, Progr, ValMax, Tentativi, NumConcNextWin, TimeFromLast, TimeFromStart, NomeDatabase) VALUES ")
                                                        sb.Append("('")
                                                        sb.Append(TipoRicerca)
                                                        sb.Append("', " + ProgrSimu.ToString)
                                                        sb.Append(", " + Progr.ToString)
                                                        sb.Append(", " + Act1.ToString)
                                                        sb.Append(", " + cTentativi.ToString)
                                                        sb.Append(", " + NumConcNextWin.ToString)
                                                        Duration = Now - OraI
                                                        sb.Append(", #" + Duration.Hours.ToString("00") + ":" + Duration.Minutes.ToString("00") + ":" + Duration.Seconds.ToString("00") + "#")
                                                        Duration = Now - OraStart
                                                        sb.Append(", #" + Duration.Hours.ToString("00") + ":" + Duration.Minutes.ToString("00") + ":" + Duration.Seconds.ToString("00") + "#")
                                                        sb.Append(", '" + cn.DataSource.ToString + "'")
                                                        sb.Append(")")

                                                        'cn.Open()
                                                        oleCommand = New OleDbCommand(sb.ToString, cn)
                                                        oleCommand.ExecuteNonQuery()
                                                        sb.Clear()
                                                        'cn.Close()

                                                        'ScriviArraySuValMaxResultsHeaders
                                                        ' - Cancello i dati del precedente ValMax dalla tabella
                                                        If ChkBoxSimulazione.Checked = False And ChkBoxScriviRisultati.Checked = True And (Act1 >= CInt(TextBoxValMaxStart.Text) And Act1 <= CInt(TextBoxValMaxStop.Text)) Then
                                                            strSql = "DELETE FROM ValMaxResultsDetails"
                                                            oleCommand = New OleDbCommand(strSql, cn)
                                                            oleCommand.ExecuteNonQuery()
                                                            ' - Ricopio l'array Dettagli in tabella
                                                            For n = 0 To Dettagli.GetUpperBound(0) - 1
                                                                sb.Clear()
                                                                sb.Append("INSERT INTO ValMaxResultsDetails (ValMax, DataEstrazione, Estratto, E1, E2, E3, E4, E5, Ext1, Ext2, Pnt, Prg) VALUES ")
                                                                sb.Append("(")
                                                                sb.Append(Act1.ToString)
                                                                sb.Append(", #" + Dettagli(n).DataEstrazione.ToString("MM/dd/yyyy") + "#")
                                                                sb.Append(", " + Dettagli(n).Estratto.ToString)
                                                                sb.Append(", " + Dettagli(n).E1.ToString)
                                                                sb.Append(", " + Dettagli(n).E2.ToString)
                                                                sb.Append(", " + Dettagli(n).E3.ToString)
                                                                sb.Append(", " + Dettagli(n).E4.ToString)
                                                                sb.Append(", " + Dettagli(n).E5.ToString)
                                                                sb.Append(", " + Dettagli(n).Ext1.ToString)
                                                                sb.Append(", " + Dettagli(n).Ext2.ToString)
                                                                sb.Append(", " + Dettagli(n).Pnt.ToString)
                                                                sb.Append(", " + Dettagli(n).Prg.ToString)
                                                                sb.Append(")")

                                                                'cn.Open()
                                                                oleCommand = New OleDbCommand(sb.ToString, cn)
                                                                oleCommand.ExecuteNonQuery()
                                                                sb.Clear()
                                                                'cn.Close()
                                                            Next n
                                                            AggiornaDgvRsultati()
                                                            AggiornaDgvNumeriDaGiocare()
                                                        End If

                                                        If ChkBoxScriviRisultati.Checked = False Then AggiornaDgvRsultati()

                                                        TextBoxTentativiFatti.Text = Cont.ToString("#,##")
                                                        cTentativi = 0
                                                        Progr += 1
                                                        OraI = Now

                                                        'System.Threading.Thread.Sleep(PauseTime)
                                                        System.Windows.Forms.Application.DoEvents()

                                                    End If

                                                    Cont += 1
                                                    cTentativi += 1
                                                    cTentMin += 1
                                                    If DateDiff(DateInterval.Second, OraMin, Now) >= 60 Then
                                                        TextBoxTentativiMinuto.Text = cTentMin.ToString("#,##")
                                                        cTentMin = 1
                                                        OraMin = Now
                                                        Duration = Now - OraStart
                                                        TextBoxTempoTrascorso.Text = Duration.Days.ToString() + "." + Duration.Hours.ToString("00") + ":" + Duration.Minutes.ToString("00") + ":" + Duration.Seconds.ToString("00")
                                                        Duration = Now - OraStart2
                                                        TextBoxMediaTentSec.Text = Int(Cont / DateDiff(DateInterval.Second, OraStart2, Now)).ToString("#,##")
                                                        TextBoxMediaTentMin.Text = Int((Cont / DateDiff(DateInterval.Second, OraStart2, Now)) * 60).ToString("#,##")
                                                        TextBoxMediaTentOra.Text = Int((Cont / DateDiff(DateInterval.Second, OraStart2, Now)) * 3600).ToString("#,##")
                                                        'System.Threading.Thread.Sleep(PauseTime)
                                                        System.Windows.Forms.Application.DoEvents()
                                                        Me.Refresh()
                                                    End If

                                                    If ChkBoxSimulazione.Checked = False And Act1 >= CInt(TextBoxValMaxStart.Text) And Act1 <= CInt(TextBoxValMaxStop.Text) Then
                                                        CtrlBoxStop.Checked = True
                                                        loopState.[Stop]()
                                                        Exit Sub
                                                    ElseIf ChkBoxSimulazione.Checked = False And Act1 > CInt(TextBoxValMaxStop.Text) Then
                                                        Act = CInt(TextBoxValMaxStart.Text) - 1
                                                    ElseIf ChkBoxSimulazione.Checked = True And Act1 >= CInt(TextBoxValMaxStop.Text) Then
                                                        CtrlBoxStop.Checked = True
                                                        loopState.[Stop]()
                                                        Exit Sub
                                                    End If

                                                    If Cont Mod CInt(TextBoxIntervalloRefresh.Text) = 0 Then
                                                        If CtrlBoxStop.Checked = True Then
                                                            loopState.[Stop]()
                                                            Exit Sub
                                                        End If
                                                        TextBoxTentativiFatti.Text = Cont.ToString("#,##")
                                                        Duration = Now - OraStart
                                                        TextBoxTempoTrascorso.Text = Duration.Days.ToString() + "." + Duration.Hours.ToString("00") + ":" + Duration.Minutes.ToString("00") + ":" + Duration.Seconds.ToString("00")
                                                        Duration = Now - OraStart2
                                                        TextBoxMediaTentSec.Text = Int(Cont / DateDiff(DateInterval.Second, OraStart2, Now)).ToString("#,##")
                                                        TextBoxMediaTentMin.Text = Int((Cont / DateDiff(DateInterval.Second, OraStart2, Now)) * 60).ToString("#,##")
                                                        TextBoxMediaTentOra.Text = Int((Cont / DateDiff(DateInterval.Second, OraStart2, Now)) * 3600).ToString("#,##")
                                                        'System.Threading.Thread.Sleep(PauseTime)
                                                        System.Windows.Forms.Application.DoEvents()
                                                        Me.Refresh()
                                                    End If

                                                Catch ex As Exception
                                                    LogError(ex)
                                                Finally
                                                    ReDim Dettagli(0)
                                                    Me.Cursor = Cursors.Default
                                                End Try

                                            End Sub)

Where is the error?

All replies (4)

Friday, September 21, 2018 1:59 PM

This is the English forums. Please post your question in English or use the forums appropriate for your locale.

Also, this is the C# forum. Your code is VB so please post VB questions over in that forum. I'll move your question.

Michael Taylor http://www.michaeltaylorp3.net


Thursday, September 27, 2018 8:59 AM

Hi Michael, now I have post my question in English, I think this is the apprpriate forum: Visual Studio Languages  ,  .NET Framework  >  Visual Basic

Thanks

Marco


Thursday, September 27, 2018 12:48 PM

You'll have to debug the code.  It appears to have problems or you would not have put a call to DoEvents inside of a parallel for.  Most likely you have some fundamental design flaw.  But this is too much code in one big chunk, and we have no idea what some of those other method calls do.

Take the code out of the parallel loop and just run it sequentially.  That way you can get the debugger to break on the actual line that is causing the error.  Once you have the error resolved, put the code back into the parallel loop.

Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


Friday, September 28, 2018 6:07 PM

Ok thanks , I will try!