Click here to Skip to main content
15,886,873 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: how to restrict loading of one form without closing another form in vb6.0 Pin
Moreno Airoldi29-Jul-09 23:10
Moreno Airoldi29-Jul-09 23:10 
QuestionExcel and forms Pin
veon cheng29-Jul-09 18:00
veon cheng29-Jul-09 18:00 
AnswerRe: Excel and forms Pin
Johan Hakkesteegt29-Jul-09 21:54
Johan Hakkesteegt29-Jul-09 21:54 
GeneralRe: Excel and forms Pin
veon cheng29-Jul-09 22:34
veon cheng29-Jul-09 22:34 
GeneralRe: Excel and forms Pin
Johan Hakkesteegt29-Jul-09 22:39
Johan Hakkesteegt29-Jul-09 22:39 
GeneralRe: Excel and forms Pin
veon cheng29-Jul-09 22:53
veon cheng29-Jul-09 22:53 
GeneralRe: Excel and forms Pin
Johan Hakkesteegt29-Jul-09 23:17
Johan Hakkesteegt29-Jul-09 23:17 
QuestionObject/Structure References [modified] Pin
Ed Hill _5_29-Jul-09 4:54
Ed Hill _5_29-Jul-09 4:54 
I am working on quite a large form that has become very slow to load, so i'm looking to take off some of the controls and have them loading on a background worker. The most used controls will remain, and a stack will be set up of controls to load in the background, loading the most recently requested control next.

As controls can only be added on the main thread there is also a queue of controls that have been loaded on the background worker and now need adding to the form. All of this works, the issue i have is it would be useful to keep a reference to each control, so you only load it once etc.

To stop code replication it would be great if i can create an object by sending in the System.Type of the object i want to make, the controlCollection it should be added to, some data it can use and the class/form level referance you want set to the control when it has loaded.

Most of this works, however, i cannot get the class level reference to point to object created on the background worker. I get the feeling it should be doable using byRef, but am not quite sure how. I'm thinking my C++ way of thinking is causing problems here, any advice would be appreciated.

Sorry for the large amount of code being posted, think it is all needed to solve the problem.
Public Class MyClass
    'class level declaration of control to load on the background worker
    Private _addresses As uscAddresses = Nothing

    Public Function Addresses() As uscAddresses
        If _addresses Is Nothing Then 'if its not been loaded we should put it on the top of the stack
            'don't worry if it is already on the stack as it will not get loaded twice
            _loadQueue.Push(New ComponentToLoad(GetType(uscAddresses), _addresses, Nothing, tpAddresses))
            'if we are already running the background worker no need to start it a second time
            If Not bgwLoader.IsBusy Then bgwLoader.RunWorkerAsync()
            'ideally won't need this next bit in the final solution
            While _addresses Is Nothing
                'Application.DoEvents()
                'Threading.Thread.Sleep(100)
            End While
        End If
        Return _addresses
    End Function

    Structure ComponentToLoad
        Public Type As System.Type 'type of object that we want to load
        '**************************************************************
        Public Target As UserControl 'the class level reference to use for the created object
        '**************************************************************
        Public Data As Object 'any data to pass to the object
        Public Parent As Control 'the control to add the new object to after its created

        Public Sub New(ByVal inType As System.Type, ByVal inTarget As UserControl, ByVal inData As Object, ByVal inParent As Control)
            Type = inType
            Target = inTarget
            Data = inData
            Parent = inParent
        End Sub
    End Structure

    'runs as a stack so most recent request is delt with first
    Private Shared _loadQueue As New Stack(Of ComponentToLoad)
    'could use another stack but a queue will do, user should see little difference
    Private Shared _controlsToAdd As New Queue(Of ComponentToLoad)

    Private Sub MatterManager_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'queue objects to load here if any are needed but not immediatly at form load
    End Sub

    'handles all loading, including checks that the object is not already loaded
    Public Sub LoadControl(ByVal onUIThread As Boolean, ByRef inComponent As ComponentToLoad)
        If onUIThread Then
            'only add the control if its not there already
            If Not DirectCast(inComponent.Parent, TabPage).Controls.Contains(inComponent.Target) Then
                DirectCast(inComponent.Parent, TabPage).Controls.Add(inComponent.Target)
            End If
        Else
            'only make the object if its not already made
            If inComponent.Target Is Nothing Then
                inComponent.Target = Activator.CreateInstance(inComponent.Type)
                inComponent.Target.Dock = DockStyle.Fill
            End If
            'could pass the data in to the object here
        End If
    End Sub

    Private Sub bgwLoader_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwLoader.DoWork
        'check there is something in the queue
        If _loadQueue.Count > 0 Then
            'make reference to what we are loading so we can add it to the ui queue
            Dim toLoad As ComponentToLoad = _loadQueue.Pop
            'load it
            LoadControl(False, toLoad)
            'stick it on the ui addition queue
            _controlsToAdd.Enqueue(toLoad)
        End If
    End Sub

    Private Sub bgwLoader_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwLoader.RunWorkerCompleted
        While _controlsToAdd.Count > 0
            'add all loaded controls
            LoadControl(True, _controlsToAdd.Dequeue())
        End While
        If _loadQueue.Count > 0 AndAlso Not bgwLoader.IsBusy Then
            'load the next object
            bgwLoader.RunWorkerAsync()
        End If
    End Sub
End Class


modified on Wednesday, July 29, 2009 12:24 PM

AnswerRe: Object/Structure References Pin
Johan Hakkesteegt29-Jul-09 21:32
Johan Hakkesteegt29-Jul-09 21:32 
GeneralRe: Object/Structure References Pin
Ed Hill _5_30-Jul-09 2:11
Ed Hill _5_30-Jul-09 2:11 
GeneralRe: Object/Structure References Pin
Johan Hakkesteegt30-Jul-09 2:38
Johan Hakkesteegt30-Jul-09 2:38 
GeneralRe: Object/Structure References Pin
Ed Hill _5_30-Jul-09 6:33
Ed Hill _5_30-Jul-09 6:33 
GeneralRe: Object/Structure References Pin
Johan Hakkesteegt30-Jul-09 21:28
Johan Hakkesteegt30-Jul-09 21:28 
AnswerRe: Object/Structure References Pin
Dave Kreskowiak30-Jul-09 2:19
mveDave Kreskowiak30-Jul-09 2:19 
AnswerRe: Object/Structure References Pin
Ed Hill _5_31-Jul-09 0:27
Ed Hill _5_31-Jul-09 0:27 
Questionhow to search for a word in a book programmatically in dot net? Pin
tadeze29-Jul-09 3:18
tadeze29-Jul-09 3:18 
AnswerRe: how to search for a word in a book programmatically in dot net? Pin
Johan Hakkesteegt29-Jul-09 3:41
Johan Hakkesteegt29-Jul-09 3:41 
QuestionFormat Number in VB 6.0 - Eg. 1 to "One", 222 to "Two Hundred and Twenty Two" Pin
CPK_201129-Jul-09 1:57
CPK_201129-Jul-09 1:57 
AnswerRe: Format Number in VB 6.0 - Eg. 1 to "One", 222 to "Two Hundred and Twenty Two" Pin
Vimalsoft(Pty) Ltd29-Jul-09 2:14
professionalVimalsoft(Pty) Ltd29-Jul-09 2:14 
AnswerRe: Format Number in VB 6.0 - Eg. 1 to "One", 222 to "Two Hundred and Twenty Two" Pin
Tim Carmichael29-Jul-09 2:54
Tim Carmichael29-Jul-09 2:54 
AnswerRe: Format Number in VB 6.0 - Eg. 1 to "One", 222 to "Two Hundred and Twenty Two" Pin
Dave Kreskowiak29-Jul-09 3:54
mveDave Kreskowiak29-Jul-09 3:54 
AnswerRe: Format Number in VB 6.0 - Eg. 1 to "One", 222 to "Two Hundred and Twenty Two" Pin
Christian Graus29-Jul-09 10:51
protectorChristian Graus29-Jul-09 10:51 
QuestionHolding collections in memory, good or bad idea? Pin
Jay Royall28-Jul-09 22:34
Jay Royall28-Jul-09 22:34 
AnswerRe: Holding collections in memory, good or bad idea? Pin
Mycroft Holmes28-Jul-09 23:32
professionalMycroft Holmes28-Jul-09 23:32 
GeneralRe: Holding collections in memory, good or bad idea? Pin
Jay Royall28-Jul-09 23:40
Jay Royall28-Jul-09 23:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.