Top bar controls missing

ideprize

Well-known member
Joined
Oct 19, 2011
Messages
97
Programming Experience
10+
Hi All

I have a vb.net Windows Form that starts running by virtue of the "form load". It runs continuously processing documents as it finds them on an interface file. I am using a "me.show" to present the form in the "form load" event. It appears to me that because of such I don't have the "top bar" set of controls, i.e. minimize, maximize, and close. It also has an instability regarding mouse clicks in the form - if inadvertently a click is experienced in the form it crashes. I have no "clickable" controls in the form. The form screen is strictly output showing the documents that have been processed. Any help would be greatly appreciated. I am thinking of placing a mouse click event trap in the form but it would be nice to understand what is going on. And the top bar issue is really perplexing - is the "activated" event somehow needed?

Respectfully,
Ideprize
 
Obviously you're doing it wrong but you haven't actually showed us what you're doing so we can't know what's wrong with it. You definitely shouldn't be calling Show in the Load event handler for a start. My guess is that you're doing everything in the Load event handler, which would be very wrong.

We'd need more details to know for sure but, from the sound of it, I'd suggest that you use a BackgroundWorker. You call RunWorkerAsync on the BackgroundWorker in the Load event handler and that's all. The event handler will then complete and the form will be displayed. You would handle the DoWork event of the BackgroundWorker and do your processing in there. Any time you need to update the UI, you call ReportProgress and handle the ProgressChanged event handler. Closing the form would be a two-step process because you must cancel the background task and then close the form. Here's an example:
Imports System.Threading

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        If Me.BackgroundWorker1.IsBusy Then
            'Don't let the form close while the background task is still running.
            e.Cancel = True
            Me.BackgroundWorker1.CancelAsync()
            Me.Label1.Text = "Please wait. The window will close when processing completes."
        End If
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim itemsProcessedCount = 0

        Do
            If Me.BackgroundWorker1.CancellationPending Then
                'End the task so the form can close.
                e.Cancel = True
                Exit Do
            End If

            'Process the next item.
            Thread.Sleep(5000)
            itemsProcessedCount += 1
            Me.BackgroundWorker1.ReportProgress(itemsProcessedCount)
        Loop
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        Me.TextBox1.AppendText(String.Format("{0} {1} processed{2}",
                                             e.ProgressPercentage,
                                             If(e.ProgressPercentage = 1, "item", "items"),
                                             Environment.NewLine))
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        'All done so close the form.
        Me.Close()
    End Sub

End Class
 
Hi JM

Wow, I got's some work to do! As a quick placation I added a checkbox (start) to the form since it must be restarted every morning anyway. With this in place I moved all of the code out of the "load" event to the "checkbox click" event. The lack of control still persists because of the tight cycling nature of the program - no events can fire because of the "spinning loop". I will implement the "threading scheme" you so graciously provided. This will be a "whole nother level" in my vb.net education. As always JM thanks a bunch.

Respectfully,
Ideprize
 
Back
Top