Results 1 to 2 of 2

Thread: Would like to make a module for common Multithreading functions

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Would like to make a module for common Multithreading functions

    I've got 4 projects all running very similar multi-threading HTTPListener stuff.

    I wanted to make these functions common - in some module - but I'm having a hard time getting the AddressOf the "callback" passed in.

    This was the code in one of the 4 projects

    Code:
        Private Sub StartRequests(ByVal prefixes() As String)
            If Not System.Net.HttpListener.IsSupported Then
                DisplayInfo( _
                    "Windows XP SP2, Server 2003, or higher is required to " & _
                    "use the HttpListener class.")
                Exit Sub
            End If
    
            ' URI prefixes are required,
            If prefixes Is Nothing OrElse prefixes.Length = 0 Then
                Throw New ArgumentException("prefixes")
            End If
    
            If m_listener Is Nothing Then m_listener = New HttpListener()
            For Each s As String In prefixes
                m_listener.Prefixes.Add(s)
            Next
            Try
                m_listener.Start()
                DisplayInfo("Listening...")
                m_listener.BeginGetContext(New AsyncCallback(AddressOf GetContextCallBack), Nothing)
                DisplayInfo("Waiting for request to be processed asyncronously.")
            Catch ex As HttpListenerException
                If ex.ErrorCode = 5 Then
                    DisplayInfo("No Entry: netsh http add urlacl url=" & prefixes(0) & " user=\Everyone")
                Else
                    DisplayInfo(ex.Message)
                End If
            End Try
        End Sub
    I was trying to put it into this module

    but this line is not working - and I do not know how to pass the AddressOf something into a module function...

    l_listener.BeginGetContext(New AsyncCallback(AddressOf GetContextCallBack), Nothing)

    Code:
    Module dcxCommon
    
        Private Sub DisplayInfo(uiListbox As System.Windows.Forms.ListBox, aString As String)
            uiListbox.Items.Add(aString)
        End Sub
    
        Public Sub StartRequests(ByVal prefixes() As String, l_listener As System.Net.HttpListener, uiListbox As System.Windows.Forms.ListBox)
            If Not System.Net.HttpListener.IsSupported Then
                DisplayInfo(uiListbox, _
                    "Windows XP SP2, Server 2003, or higher is required to " & _
                    "use the HttpListener class.")
                Exit Sub
            End If
    
            ' URI prefixes are required,
            If prefixes Is Nothing OrElse prefixes.Length = 0 Then
                Throw New ArgumentException("prefixes")
            End If
    
            If l_listener Is Nothing Then l_listener = New System.Net.HttpListener()
            For Each s As String In prefixes
                l_listener.Prefixes.Add(s)
            Next
            Try
                l_listener.Start()
                DisplayInfo(uiListbox, "Listening...")
                l_listener.BeginGetContext(New AsyncCallback(AddressOf GetContextCallBack), Nothing)
                DisplayInfo(uiListbox, "Waiting for request to be processed asyncronously.")
            Catch ex As System.Net.HttpListenerException
                If ex.ErrorCode = 5 Then
                    DisplayInfo(uiListbox, "No Entry: netsh http add urlacl url=" & prefixes(0) & " user=\Everyone")
                Else
                    DisplayInfo((uiListbox, ex.Message)
                End If
            End Try
        End Sub
    
    End Module

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Would like to make a module for common Multithreading functions

    Well - I at least got it to run without errors - made the COMMON SUB in the module look like this

    Code:
        Public Sub StartRequests(ByVal prefixes() As String _
                            , ByRef l_listener As System.Net.HttpListener _
                            , ByRef l_callback As AsyncCallback _
                            , ByRef uiListbox As System.Windows.Forms.ListBox)
            If Not System.Net.HttpListener.IsSupported Then
                DisplayInfo(uiListbox, _
                    "Windows XP SP2, Server 2003, or higher is required to " & _
                    "use the HttpListener class.")
                Exit Sub
            End If
    
            ' URI prefixes are required,
            If prefixes Is Nothing OrElse prefixes.Length = 0 Then
                Throw New ArgumentException("prefixes")
            End If
    
            If l_listener Is Nothing Then l_listener = New System.Net.HttpListener()
            For Each s As String In prefixes
                l_listener.Prefixes.Add(s)
            Next
            Try
                l_listener.Start()
                DisplayInfo(uiListbox, "Listening...")
                'l_listener.BeginGetContext(New AsyncCallback(AddressOf l_callback), Nothing)
                l_listener.BeginGetContext(l_callback, Nothing)
                DisplayInfo(uiListbox, "Waiting for request to be processed asyncronously.")
            Catch ex As System.Net.HttpListenerException
                If ex.ErrorCode = 5 Then
                    DisplayInfo(uiListbox, "No Entry: netsh http add urlacl url=" & prefixes(0) & " user=\Everyone")
                Else
                    DisplayInfo(uiListbox, ex.Message)
                End If
            End Try
        End Sub
    and it's called from the main project like this

    Code:
        Private Sub TSStart_Click(sender As System.Object, e As System.EventArgs) Handles TSStart.Click
            TSStart.Enabled = False
            ' Start CMD (run as administrator)
            ' cmd>netsh http add urlacl url=http://*:8080/dcxCurator user=\Everyone
            Dim prefixes() As String = {"http://*:8080/dcxCurator/"}
            dcxCommon.StartRequests(prefixes, m_listener, New AsyncCallback(AddressOf GetContextCallBack), CuratorList)
            'StartRequests(prefixes)
        End Sub
    But once in the GetContextCallBack in the "main project" the m_listener is nothing - I stepped through the common dcxCommon.StartRequests and it setup some listener - but not the module level on that got passed.

    Oh well - maybe this just isn't possible - it certainly isn't easy!!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width