Solved

VB.Net 2003 versus VB.Net 2005 Express

Posted on 2006-07-09
10
399 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now