Invoke function RRS feed

  • Question

  • I can create awindow app the will show gif images in loading for that i use invoke function how to use it i dont know
    Friday, December 15, 2017 10:43 AM

All replies

  • As many you make the mistake that the name of a method is something from .Net or VB.

    It is not, it is just a name made by a programmer, in the same way as you do. 

    This is the meaning on dictionary com.

    If you see it related to a special namespace or class, then tell where you use it. 


    Friday, December 15, 2017 11:56 AM
  • Hello,

    The following code sample shows how to do this. Here are parts of the code that are important to understand.

    The following is reading data from a database, on each read operation I update controls on a splash screen using Invoke.

    The following is only valid beginning with VS2015

    Imports System.Data.OleDb
    ''' <summary>
    ''' Responsible for obtain data from a MS-Access database table,
    ''' create a customer object, push it back to the caller using
    ''' an Iterator routine that pushes data back using Yield statement.
    ''' In the above process we are popping information to a Splash screen
    ''' via a delegate. We could do this in the caller but I see no reason
    ''' why for this simple demonstration.
    ''' </summary>
    ''' <remarks>
    ''' Since there are not many records Threading.Thread.Sleep(25) is used
    ''' to slow things down to see the results in the splash screen.
    ''' The last sleep is there for demo purposes only.
    ''' None of the sleeps would be in production.
    ''' </remarks>
    Public Class DataAccess
        Private Builder As New OleDbConnectionStringBuilder With
                .Provider = "Microsoft.ACE.OLEDB.12.0",
                .DataSource = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database1.accdb")
        Private ConnectionString As String = ""
        Private Delay As Integer = 0
        Public Sub New(ByVal Delay As Integer)
            CurrentIndex = 1
            ConnectionString = Builder.ConnectionString
            Me.Delay = Delay
        End Sub
        Private mRecordCount As Integer
        Public ReadOnly Property RecordCount As Integer
                Return mRecordCount
            End Get
        End Property
        ''' <summary>
        ''' Get total records to be used with ProgressBar on SplashScreen
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub GetRecordCount()
            Using cn As New OleDbConnection With {.ConnectionString = ConnectionString}
                Using cmd As New OleDbCommand With {.Connection = cn, .CommandText = "SELECT Count(Identifier) FROM Customer"}
                    mRecordCount = CInt(cmd.ExecuteScalar)
                End Using
            End Using
        End Sub
        Public Property CurrentIndex As Integer
        ''' <summary>
        ''' used to return customer data
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Iterator Function Retrieve() As IEnumerable(Of Customer)
            CurrentIndex = 0
            Dim CustomerIdentifierPosition As Integer = 0
            Dim CustomerNamePosition As Integer = 1
            Dim ContactNamePosition As Integer = 2
            Using cn As New OleDbConnection With {.ConnectionString = ConnectionString}
                Using cmd As New OleDbCommand With
                        .Connection = cn,
                        .CommandText = "SELECT Identifier, CompanyName, ContactName FROM Customer;"
                    Dim Reader As OleDbDataReader = cmd.ExecuteReader
                    If Reader.HasRows Then
                        Dim CompanyName As String = ""
                        While Reader.Read
                            CompanyName = Reader.GetFieldValue(Of String)(CustomerNamePosition)
                            Yield New Customer With
                                      .Identifier = Reader.GetFieldValue(Of Integer)(CustomerIdentifierPosition),
                                      .Company = CompanyName,
                                      .ContactName = Reader.GetFieldValue(Of String)(ContactNamePosition)
                            Dim PercentDone As Integer = CInt((CurrentIndex / Me.RecordCount) * 100)
                            CurrentIndex += 1
                        End While
                    End If
                    ' Current index at this point shows 75 thus the last call to ShowRecord
                    ' will not push the progressbar to 100 percent so the next line does using
                    ' 99 as the ShowRecord code adds one to the integer passed in.
                    ' Place here else the splashscreen disappears before we can see that
                    ' the progressbar is at 100 percent.
                End Using
            End Using
        End Function
    End Class

    Here is the splash screen code

    Public Class SplashScreen1
        Public Delegate Sub UpdateRecordDelegate(ByVal value As Integer)
        Public Sub ShowRecord(ByVal sender As Integer)
            If Me.InvokeRequired Then
                Me.Invoke(New UpdateRecordDelegate(AddressOf ShowRecord), sender)
                Me.Label1.Text = sender.ToString & " percent complete"
                Me.ProgressBar1.Value = sender + 1
            End If
        End Sub
        Private Const CP_NOCLOSE_BUTTON As Integer = &H200
        Protected Overrides ReadOnly Property CreateParams() As CreateParams
                Dim myCp As CreateParams = MyBase.CreateParams
                myCp.ClassStyle = myCp.ClassStyle Or CP_NOCLOSE_BUTTON
                Return myCp
            End Get
        End Property
    End Class
    Since this code sample is generic, I show a spinner gif along with a progress bar, only one is needed.

    Main form code

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim da As New DataAccess(3)
            For Each item In da.Retrieve
            ' optional
        End Sub
    End Class

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, December 15, 2017 11:58 AM
  • For the OP, Karen shows code, which can be used in asynchronous code. That is not something you must do as first although you sure should learn it. 

    The most simple method to show gif images in a window and invoke the image from file read method  

    MyPicturebox.image = images.fromfile("TheFullPathToTheGifImage")

    For this async make few sense because there is nothing which can interrupt this if it is processing.

    (Be aware that this method can give problems as Ray has shown yesterday) 


    Friday, December 15, 2017 1:11 PM