Cheers Mark (and everyone else).

Several good ideas there. (Though I have to admit I
rather like the OnPaint() suggestions from various people
as a quick-and-easy fix).

Whatever happened to the OnInitialUpdate() of MFC? <g>

Simon

---------------------------------------------------------------
Simon Robinson
http://www.SimonRobinson.com
---------------------------------------------------------------
----- Original Message -----
From: "Mark Boulter" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, May 24, 2002 4:02 PM
Subject: Re: [DOTNET] Displaying info on loading forms


As per previous posts the right way to do this to do the work on a
background thread and signal completion to the UI thread. If you want to
do this make sure to marshall your call back to the UI thread using
Control.Invoke. The cheap and diryt way to do this is to async invoke in
the Load event and then do you work in the invoked method.

Assuming you have a form with a big "please wait" label plastered across
it your code would look something like this:

        private void MainForm_Load(object sender, System.EventArgs e) {
            this.BeginInvoke(new MethodInvoker(this.DoWork));
        }


        private void DoWork () {

            this.Cursor = Cursors.WaitCursor;
            Application.DoEvents();

            try {

                Do lots of work

            } finally {
                this.Cursor = Cursors.Default;
                this.label1.Visible = false;
            }

        }

This will get your form display before doing the work however while "do
lots of work" is going on the form will not repaint if you move another
window over it.......

Using another thread would look something like:

    Public Delegate Sub UpdateUI(ByRef newDS As DataSet)

    'Call Web Service asynchronously
    Private Sub LoadAuthors()
        Application.DoEvents()
        Me.Cursor = Cursors.WaitCursor
        Try
            StatusBar1.Text = "Loading..."
            Dim ws As New MyServices.AuthorInformationService()
            Dim callBack As New AsyncCallback(AddressOf
LoadAuthorsComplete)
            ws.BeginListAuthors(callBack, 0)
        Catch ex1 As Exception
            MsgBox(ex1.ToString)
            StatusBar1.Text = "Error Loading author information"
            Me.Cursor = Cursors.Default
        End Try

    End Sub

    'Call back from Web Service on non-UI thread
    Private Sub LoadAuthorsComplete(ByVal ar As IAsyncResult)
        Dim ds As DataSet
        Try
            Dim ws As New MyServices.AuthorInformationService()
            ds = ws.EndListAuthors(ar)
        Catch ex1 As Exception
            MsgBox(ex1.ToString)
            Throw ex1
        End Try

        Dim updateUIDelegate As New UpdateUI(AddressOf
Me.LoadAuthorsUpdateUI)
        Dim args As Object() = {ds}
        Me.Invoke(updateUIDelegate, args)

    End Sub

    'Call from Web Service callback to UI thread
    Private Sub LoadAuthorsUpdateUI(ByRef newDs As DataSet)
        Try
            StatusBar1.Text = "Complete"
            If Not (newDs Is Nothing) Then
                Me.AuthorsDataSet1.Merge(newDs)
                StatusBar1.Text = "Record 1"
            End If
        Catch ex1 As Exception
            MsgBox(ex1.ToString)
            StatusBar1.Text = "Error Loading author information"
        Finally
            Me.Timer1.Stop()
            Me.Cursor = Cursors.Default

            While (Me.ProgressBar1.Value < Me.ProgressBar1.Maximum)
                Me.ProgressBar1.Value += Me.ProgressBar1.Step
            End While
            Me.ProgressBar1.Visible = False
            Me.ProgressBar1.Value = 0

        End Try

    End Sub





-----Original Message-----
From: Simon Robinson [mailto:[EMAIL PROTECTED]]
Sent: Friday, May 24, 2002 3:51 AM
To: [EMAIL PROTECTED]
Subject: [DOTNET] Displaying info on loading forms

Two purposes to this post:
1. Ask a question
2. Mention some rather amusing behavior in Windows Forms.

Questions runs... Is there any event in Windows Forms that
is raised after a Form has been loaded? My form does a lot
of processing at startup and I'd like it to display itself then
do its processing, reporting to the user on its progress as it
does it.  I tried the Load event but that seems to get invoked
just before the form is displayed.

Amusing behavior runs... While looking for solutions to the
above question I tried using the Activated event. The form
contains a Panel and progress report would be written in the panel
using Graphics.DrawString().  The result?
1. The main title bar of the form gets displayed (but not
the rest of the form)
2. The calls to Graphics.DrawString() write the text over
whatever happened to be on the screen where the form is
going to be displayed. You end up with VS.NET sitting
there with all this writing scrawled over it.
3. The form appears - but now without the writing.
This quite tickled me - though I'm intrigued as to why
it happened. I bet the Windows Forms team didn't
intend it.

Simon
---------------------------------------------------------------
Simon Robinson
http://www.SimonRobinson.com
---------------------------------------------------------------

You can read messages from the DOTNET archive, unsubscribe from DOTNET,
or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to