ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  StackOverflowException
Posted by Salam on Jun-15-2015 11:35
I am writing a program to draw a finance chart for real-time data received from data feed
API. I'm using the sample code as reference. While I run the program it throws following
error.

'System.StackOverflowException' An unhandled exception occurred in netchartdir_cp.dll.

Till now I am using evaluation period unregistered copy. and developing the program in
VB.net 2010. I don't understand the reason for this Exception, any help is greatly
appreciated.

  Re: StackOverflowException
Posted by Peter Kwan on Jun-15-2015 15:19
Hi Salam,

This exception usually occur if your code has an infinite calling loop. You can look at the
stack trace to see how this exception occur.

As an example, I have seen code that does something like the followings:

(a) There is a date picker or scrollbar or some control to control the chart, such as to
control the display date range. The chart will update if the control changes (such as the
scrollbar moves or the date picker changes).

(b) When new data come in, the chart is updated. The controls (such as date picker) is
also update to reflect the data change (such as to extend the scrollbar because there is
more data to scroll). When the control is updated, the chart is updated again (due to (a)
above). When the chart is updated, the control is updated. This leads to an infinite calling
loop.

The stack trace will tell you how the calling loop occurs. You can then modify your code to
avoid the loop.

If the above still does not solve the problem, would you mind to inform me of your stack
trace when the error occur?

Regards
Peter Kwan

  Re: StackOverflowException
Posted by Salam on Jun-15-2015 17:15
Hi Peter,

Thank you for your reply.

Yes I am using some control to update the data in chart. I'm trying to fix it. And I will
update my status again.

Regards,
Salam

  Re: StackOverflowException
Posted by Salam on Jun-22-2015 12:38
Attachments:
Hi Peter,

Following your tips I tried to separate two control s using timer also used in example code.
but yet I am receiving run-time error in creating chart element in
"Dim c As XYChart = New XYChart(600, 270, &HF4F4F4, &H0, 1)".

Here I attached my code.

Just for reference, AxQISAPIX1 is OCX control to get data feed in realtime.

Thanks in advance.

Regards,
Salam
Form1.vb
Imports ChartDirector
Imports QISAPIXLib

Public Class Form1
    Private Const sampleSize As Integer = 240

    Public price As List(Of Double)

    Private Quotes2 As QISQuotes
    Private QuoteUpdate As QISQuote

    Private dataSeriesA(sampleSize - 1) As Double
    'Private dataSeriesB(sampleSize - 1) As Double
    'Private dataSeriesC(sampleSize - 1) As Double
    Private timeStamps(sampleSize - 1) As Date

    Private dataindex As Integer = 0

    Private lastQAP As Double = 0.0

    ' The index of the array position to which new data values are added.
    Private currentIndex As Integer = 0
    Private nextDataTime As DateTime = New DateTime((Now.Ticks \\ 10000000) * 10000000)


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label1.Text = UBound(timeStamps).ToString

        Dim ret As Integer
        ret = AxQISAPIX1.OpenSession()
        If ret <= 0 Then
            Dim Error1 As Long
            Dim Error2 As Long
            Error1 = AxQISAPIX1.ProxyErrorInfo
            Error2 = AxQISAPIX1.ECErrorInfo
            MsgBox("OpenSessionに失敗しました。" & ret & " " & Error1 & " " & Error2)
            End
        End If

        'Dim i As Integer
        'For i = 0 To UBound(timeStamps)
        '    timeStamps(i) = DateTime.MinValue
        'Next

        Me.SetQuote()

    End Sub

    Private Sub drawChart(ByVal viewer As WinChartViewer)

        ' Create an XYChart object 600 x 270 pixels in size, with light grey (f4f4f4) 
        ' background, black (000000) border, 1 pixel raised effect, and with a rounded frame.
        Try
            Dim c As XYChart = New XYChart(600, 270, &HF4F4F4, &H0, 1)
            c.setRoundedFrame(Chart.CColor(BackColor))

            ' Set the plotarea at (55, 55) and of size 520 x 185 pixels. Use white (ffffff) 
            ' background. Enable both horizontal and vertical grids by setting their colors to 
            ' grey (cccccc). Set clipping mode to clip the data lines to the plot area.
            c.setPlotArea(55, 55, 520, 185, &HFFFFFF, -1, -1, &HCCCCCC, &HCCCCCC)
            c.setClipping()

            ' Add a title to the chart using 15 pts Times New Roman Bold Italic font, with a light
            ' grey (dddddd) background, black (000000) border, and a glass like raised effect.
            c.addTitle("Stock Price", "Times New Roman Bold Italic", 15 _
                ).setBackground(&HDDDDDD, &H0, Chart.glassEffect())

            ' Set the reference font size of the legend box
            c.getLegend().setFontSize(8)

            ' Configure the y-axis with a 10pts Arial Bold axis title
            c.yAxis().setTitle("Price(t)", "Arial Bold", 10)

            c.xAxis().setTitle("time(s)", "Arial Bold", 10)

            ' Configure the x-axis to auto-scale with at least 75 pixels between major tick and 15 
            ' pixels between minor ticks. This shows more minor grid lines on the chart.
            c.xAxis().setTickDensity(75, 15)

            ' Set the axes width to 2 pixels
            c.xAxis().setWidth(2)
            c.yAxis().setWidth(2)

            ' Now we add the data to the chart
            Dim firstTime As DateTime = timeStamps(0)
            If firstTime <> DateTime.MinValue Then
                ' Set up the x-axis scale. In this demo, we set the x-axis to show the 240 samples,
                ' with 250ms per sample.
                c.xAxis().setDateScale(firstTime, firstTime.AddSeconds( _
                    250 * timeStamps.Length / 1000))

                ' Set the x-axis label format
                c.xAxis().setLabelFormat("{value|hh:nn:ss}")

                ' Create a line layer to plot the lines
                Dim layer As LineLayer = c.addLineLayer2()

                ' The x-coordinates are the timeStamps.
                layer.setXData(timeStamps)

                ' The 3 data series are used to draw 3 lines.
                layer.addDataSet(dataSeriesA, &HFF0000, "Alpha")
                'layer.addDataSet(dataSeriesB, &HCC00, "Beta")
                'layer.addDataSet(dataSeriesC, &HFF, "Gamma")
            End If

            ' Include track line with legend. If the mouse is on the plot area, show the track 
            ' line with legend at the mouse position; otherwise, show them for the latest data
            ' values (that is, at the rightmost position).
            'trackLineLegend(c, IIf(viewer.IsMouseOnPlotArea, viewer.PlotAreaMouseX, c.getPlotArea().getRightX()))

            ' Assign the chart to the WinChartViewer
            viewer.Chart = c
        Catch ex As Exception
            Debug.WriteLine(vbCrLf & "StackTrace ---" & vbCrLf & ex.StackTrace)
        End Try
        

    End Sub

    Private Sub SetQuote()

        Me.Quotes2 = AxQISAPIX1.QISQuotes    'QISQuoteを作成しQISQuotesに追加
        Me.QuoteUpdate = Quotes2.Add
        With QuoteUpdate                    'QuoteRequestをQuoteUpdateへ変更
            .QuoteNo = 1                    '銘柄数を指定
            .ElementNo = 5                  'Element数を指定
            '.QuoteCode(0) = "XJPY/4"       '銘柄コードを指定
            .QuoteCode(0) = "6501/T"        '銘柄コードを指定
            .ElementCode(0) = "NAME"        'Elementコードを指定
            .ElementCode(1) = "QAP"         'Elementコードを指定
            .ElementCode(2) = "TIME"        'Elementコードを指定
            .ElementCode(3) = "GAP1"        'Elementコードを指定
            .ElementCode(4) = "GBP1"        'Elementコードを指定
            .ElementType(0) = 0             'ElementTypeを指定
            .RequestMode = 1                '自動更新モードに指定	非自動更新モードから変更
            .Request()                      '問合せ実行
            'Debug.WriteLine(.Data(0, 0))   'データを表示(101 / Name)
            'Debug.WriteLine(.Data(0, 1))   'データを表示(101 / DPP)
        End With

        WinChartViewer1.Visible = True

        Try
            drawChart(Me.WinChartViewer1)
        Catch ex As Exception
            Debug.WriteLine(vbCrLf & "StackTrace ---" & vbCrLf & ex.StackTrace)
        End Try

    End Sub

    '
    ' Utility to shift a value into an array
    '
    Private Sub shiftData(ByVal data As Object, ByVal newValue As Object)

        Dim i As Integer
        For i = 1 To UBound(data)
            data(i - 1) = data(i)
        Next
        data(UBound(data)) = newValue

    End Sub

    Private Sub AxQISAPIX1_OnQuoteChange(ByVal sender As System.Object, ByVal e As AxQISAPIXLib._DQISAPIXEvents_OnQuoteChangeEvent) Handles AxQISAPIX1.OnQuoteChange

        Dim i As Integer
        'Dim j As Integer
        Dim QIndex As Integer
        Dim ENo As Integer
        Dim QAP As Double

        Dim p As Double
        Dim dataA As Double = 0.0

        With QuoteUpdate
            QIndex = QuoteUpdate.UpdateQuoteIndex()  '自動更新した銘柄のIndex
            ENo = QuoteUpdate.UpdateElemNo() '自動更新したエレメントの数

            'RichTextBox2.Text = ""
            'RichTextBox2.AppendText("Eno: " & ENo & Environment.NewLine)
            'RichTextBox2.AppendText(Environment.NewLine)

            Dim EIndex As Integer
            For i = 0 To ENo - 1    '自動更新したエレメントの数分ループ
                EIndex = QuoteUpdate.UpdateElemIndex(i)    '自動更新したエレメントのIndex
                'RichTextBox2.AppendText(QuoteUpdate.QuoteCode(QIndex) + " " + QuoteUpdate.ElementCode(EIndex) + " =" + QuoteUpdate.Data(QIndex, EIndex) + System.Environment.NewLine)
                QAP = Double.Parse(QuoteUpdate.Data(0, 1))
                'RichTextBox2.AppendText(QuoteUpdate.Data(0, 1) & Environment.NewLine)

                If QAP <> Me.lastQAP Then
                    lastQAP = QAP
                    p = nextDataTime.Ticks / 10000000.0 * 4
                    dataA = QAP
                End If
                ' After obtaining the new values, we need to update the data arrays.
                If currentIndex < timeStamps.Length Then

                    ' Store the new values in the current index position, and increment the index.
                    dataSeriesA(currentIndex) = dataA
                    timeStamps(currentIndex) = nextDataTime
                    currentIndex += 1
                Else
                    ' The data arrays are full. Shift the arrays and store the values at the end.

                    shiftData(dataSeriesA, dataA)
                    shiftData(timeStamps, nextDataTime)
                End If

                ' Update nextDataTime. This is needed by our data generator. In real applications,
                ' you may not need this variable or the associated do/while loop.
                nextDataTime = nextDataTime.AddMilliseconds(250)
            Next

        End With


    End Sub

    Private Sub Timer_chartupdate_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer_chartupdate.Tick
        Label1.Text = Me.currentIndex.ToString()
        WinChartViewer1.updateViewPort(True, False)
    End Sub
End Class

  Re: StackOverflowException
Posted by Peter Kwan on Jun-22-2015 18:57
Hi Salam,

Is it possible to inform me what is the exact error message and the stack trace? If you
cannot post them in the forum, you can email me at pkwan@advsofteng.net.

Reagrds
Peter Kwan