• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 430
  • Last Modified:

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.
0
BigBerner
Asked:
BigBerner
  • 5
  • 5
1 Solution
 
AlexFMCommented:
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.
0
 
BigBernerAuthor Commented:
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?
0
 
AlexFMCommented:
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BigBernerAuthor Commented:
It came back true.
0
 
AlexFMCommented:
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
0
 
BigBernerAuthor Commented:
OK I have done all of that and it all compiles without any errors, however it still does not update the textbox.
0
 
AlexFMCommented:
In think you need to change
Me.Invoke(New DelegateSetString(AddressOf Me.SetString), New Object() {TopicValue})
with
Me.Invoke(New DelegateSetString(AddressOf Me.SetString), New Object() {TopicValue.ToString()})

Debug this ans check what is string value passed to SetString function.
0
 
BigBernerAuthor Commented:
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.
0
 
AlexFMCommented:
.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.
0
 
BigBernerAuthor Commented:
Thanks for your help.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now