Solved

VB.Net 2003 versus VB.Net 2005 Express

Posted on 2006-07-09
10
411 Views
Last Modified: 2010-04-23
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
Comment
Question by:BigBerner
  • 5
  • 5
10 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17070822
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
 

Author Comment

by:BigBerner
ID: 17070959
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 17070971
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
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.

 

Author Comment

by:BigBerner
ID: 17071035
It came back true.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17071097
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
 

Author Comment

by:BigBerner
ID: 17071145
OK I have done all of that and it all compiles without any errors, however it still does not update the textbox.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
ID: 17071182
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
 

Author Comment

by:BigBerner
ID: 17071214
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 17071306
.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
 

Author Comment

by:BigBerner
ID: 17071336
Thanks for your help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question