locked
Threading in .NET CF RRS feed

  • Question

  • Hi,


    I am using VS.NET 2003 for developing .NET CF application. I have many forms in the application which i want to load when user starts my application. I want to load all the forms in the start of application to provide fast switching between multiple screens. I have used singelton pattern to avoid creating multiple instance of the forms.

    The requirement of my application is to load some job related forms when user opens a job. Loading some of job related forms includes creating instance of form (if it is not created) + loading field values from the database. Some Job related forms i load at the time user opens the Job while other forms i load when user navigates to a screen where it is required to load all the additional forms for processing the calculation depending on the data stored in the forms. The process of loading these forms and calculation is time consuming, user has to wait for 10-20 seconds to get all the forms loaded in the memory with the field values.

    Now I thought to call subLoadAdditionalServices in a thread when user opens a job itself to make it a rich experience for user to not wait after navigating to a screen where they are required. I use the Thread class to start a thread with Thread.Start method giving the address of subLoadAdditionalServices method. I have a separate class for ThreadWorker and in that class i create a new instance of thread in the AdditionalServiceThread method. This method has code as
    Try
                AdditionalThread = New Thread(AddressOf ogui.subLoadAdditionalServices)
                AdditionalThread.Start()
                AdditionalThread.Priority = ThreadPriority.BelowNormal
            Catch exx As ThreadStateException
                MsgBox(exx.ToString)
                If AdditionalThread.CurrentThread Is AdditionalThread Then
                    While Not AdditionalThread.CurrentThread Is Nothing
                        'wait for some time to get the thread stopped
                    End While
                End If
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try



    Everything looks ok when i start the application and opens a job, it loads the additional forms properly. But when I exit from that Job (i make sure that the running thread is completed from the subLoadAdditionalServices before quiting from Job) and try to open another job, it gives a native exception. Sometimes I get Out of Memory errors. The same time i checked that in Settings>Memory it was showing 10MB free memory for running programs.

    I don't want to dispose the additional Job forms while exiting from the job to free up some memory. Here I am creating a new instance of AdditionalThread so I guess this thread object should not take too much memory that i need to free up after the thread call finishes.  What can be the possible way to overcome this problem?

    Thanks in advance,


    Wednesday, January 24, 2007 12:56 PM

Answers

  • 1. You can't create forms on the thread, they have to be created on UI thread which has message pump. You can load data from database though.

    2. There's no point in starting a thread and waiting till it's done, just do it on the same thread.

     

    Wednesday, January 24, 2007 5:49 PM