Link to home
Start Free TrialLog in
Avatar of BigBerner
BigBerner

asked on

VB.Net 2003 versus VB.Net 2005 Express

Hello experts,

I have some code that works fine in vb.net 2003 but does not work in vb.net 2005 express and was wondering why this is so and how to fix it.

Here is the code.
-----------------------------------------------------------------------------------------------
Imports System.Threading

Public Class frmRTDClient
    Inherits System.Windows.Forms.Form
    Implements EccoDotNet.IRTDUpdateEvent
    Private RTDServer As EccoDotNet.IRtdServer


 Private Sub frmRTDClient_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim EccoPricesType As Type = Type.GetTypeFromProgID("Ecco.Prices")
        RTDServer = Activator.CreateInstance(EccoPricesType)
        RTDServer.ServerStart(Me)

        Dim TopicID As Integer = 100
        Dim Topic As String = "BIDQTY"
        Dim Contract1 As String = "SFEP:SFE:XT:F:0906"
        Dim TopicParams(3) As Object
        TopicParams(0) = Topic
        TopicParams(1) = 0
        TopicParams(2) = "OUT"
        TopicParams(3) = Contract1
       
        Dim CheckStartingValue As Boolean
        Dim StartingValue As Object = RTDServer.ConnectData(TopicID, TopicParams, CheckStartingValue)

        If CheckStartingValue = True Then
            txtTopic.Text = StartingValue
        End If

    End Sub

    Public Sub UpdateNotify() Implements EccoDotNet.IRTDUpdateEvent.UpdateNotify

        Dim TopicCount As Integer
        Dim Values As System.Array = RTDServer.RefreshData(TopicCount)
        Dim i As Integer
        For i = 0 To TopicCount - 1
            Dim TopicID As Integer = Values.GetValue(0, i)
            Dim TopicValue As Object = Values.GetValue(1, i)
            Me.txtTopic.Text = TopicValue       ***This line is where it does not work in vb.net 2005 express***
        Next

    End Sub
   
End Class
--------------------------------------------------------------------------------------------------
This code is part of a live stock market data feed. When the form loads it gets the intitial value and outputs it to the textbox. This works fine in both versions of vb.net. All subsequent updates to the textbox are carried out by UpdateNotify, but this does not work in vb.net 2005 express. Any ideas?

Thanks



Thanks.
Avatar of AlexFM
AlexFM

If UpdateNotify is called in the context of worker thread, you cannot update Windows form controls directly. You need to use Invoke or BeginInvoke for this.
Avatar of BigBerner

ASKER

So why wouldn't I need to do this in both versions of vb.net??
Also I am completely hopeless with threading. How do you use Invoke or BeginInvoke?
To check whether this is threading issue add the following code to UpdateNotify function:

Dim b as Boolean
b = Me.InvokeRequired()

If b is true, you need Invoke, I will post you code. If false, this is something else.
It came back true.
Make the following changes:

Delegate Sub DelegateSetString(ByVal s As String)     ' add this line before class definition


    Public Sub UpdateNotify() Implements EccoDotNet.IRTDUpdateEvent.UpdateNotify

        Dim TopicCount As Integer
        Dim Values As System.Array = RTDServer.RefreshData(TopicCount)
        Dim i As Integer
        For i = 0 To TopicCount - 1
            Dim TopicID As Integer = Values.GetValue(0, i)
            Dim TopicValue As Object = Values.GetValue(1, i)

            ' Me.txtTopic.Text = TopicValue       ***This line is where it does not work in vb.net 2005 express***

            Me.Invoke(New DelegateSetString(AddressOf Me.SetString), New Object() {TopicValue})

        Next

    End Sub

    private sub SetString(ByVal s as string)
       Me.txtTopic.Text = s
    end sub
OK I have done all of that and it all compiles without any errors, however it still does not update the textbox.
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent, that works now.
Do you have any ideas on why I would not need to use delegates in vb.net 2003 but do need to use them in .net 05 express? I am doing exactly the same thing in both.
.NET allows to work with Windows controls only from the thread which created them. But .NET developers were not 100% correct in previous versions, and result of cross-thread calls were not consistent: sometimes it works, sometimes doesn't work, sometimes gives exception. Your old program was not correct, but it worked.
In 2005 version there is Control.CheckForIllegalCrossThreadCalls Property which can be set to true, in this case all illegal cross-thread calls give exception. For backward compatibility this is set to false by default. It is recommended to set it to true in every program, and fix bugs in legacy code, replacing direct calls with Invoke or BeginInvoke.
Thanks for your help.