Solved

How do I set up multi-threading progress bars in my application?

Posted on 2014-07-22
11
211 Views
Last Modified: 2014-07-23
I have tried and research and tried and researched and am now just confused.  I need progress bars for all the action I have going on in my application.  I set up a regular one, and it showed up at the end and then I got into a danger-loop!  Please help!!  I'll even send cookies!!

By the way, I don't want start, pause, reset or any other button.  This application opens and installs begin.  That's it...

Here is my code:

Imports System.IO
Imports Microsoft.VisualBasic.FileIO
Imports System.Object
Imports Microsoft.VisualBasic.Devices.Network
Imports System.Threading
Imports System.Net
Imports System.Text.RegularExpressions


Public Class Form1
    Dim user As String = CStr(Environ("username"))
    Public isitLoad As Boolean = False
    Public fileLocation As String = ""
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        If My.Computer.FileSystem.DirectoryExists("C:\Program Files\Microsoft SQL Server\MSSQL11.DTH2012") Then
         
            System.Diagnostics.Process.Start("http://desktophousewife.com/desktopHousewife.application")
            Me.Close()

        Else
            Try

                '===========INSTALL SQL SERVER AND/OR HOME MANAGEMENT SYSTEM =========================
                installSQL()

            Catch ex As Exception
                MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub
    Private Sub installSQL()

        If My.Computer.FileSystem.FileExists("C:\DTHSQL2012\ExtractFile\setup.exe") = True Then

            'check to see if it has been installed
            If My.Computer.FileSystem.DirectoryExists("C:\Program Files\Microsoft SQL Server\MSSQL11.DTH2012") = True Then

                '====== download home management system  ============================
                System.Diagnostics.Process.Start("http://desktophousewife.com/desktopHousewife.application")

            Else

                '=======install SQL for desktop housewife applications ===========================

                '====================================== NEED A PROGRESS BAR HERE FOR THE SQL INSTALL ================================
                Dim processObj As Process = Process.Start("C:\DTHSQL2012\ExtractFile\setup.exe", "/q /Action=Install /IACCEPTSQLSERVERLICENSETERMS /Hideconsole /Features=SQLEngine /InstanceName=DTH2012 /SQLSYSADMINACCOUNTS=""NT AUTHORITY\SYSTEM"" /SQLSVCACCOUNT=""NT AUTHORITY\SYSTEM"" /BROWSERSVCSTARTUPTYPE=""Automatic""")
                '==========================================================================================

                If processObj.ExitCode = 0 Then


                    '====================================== NEED A PROGRESS BAR HERE FOR THE Home Management INSTALL ================================
                    ProgressBar1.Value = 0
                    ProgressBar1.Visible = True
                    System.Diagnostics.Process.Start("http://desktophousewife.com/desktopHousewife.application")

                Else

                    '==========send error file ============================

                    MsgBox("SQL 2012 Install Failed. . . Code: " & processObj.ExitCode.ToString & ".  Please forward the email to me.")
                    Dim mMessage As String = "SQL 2012 Install Failed. . . Code: " & processObj.ExitCode.ToString
                    System.Diagnostics.Process.Start("mailto:karen@desktophousewife.com&subject=Desktop Housewife&body= " & mMessage)
                End If
            End If

        Else

            '=================START THE FULL DOWNLOAD SQL EXPRESS AND HOME MANAGEMENT SYSTEM  =============================

            My.Computer.FileSystem.CreateDirectory("C:\DTHSQL2012")

            '====================================== NEED A PROGRESS BAR HERE FOR THE download of SQL from my website ================================
            Label1.Text = "Downloading SQL Express to your harddrive."
            My.Computer.Network.DownloadFile("http://desktophousewife.com/SQLEXPR_x64_ENU.exe", "C:\DTHSQL2012\SQLEXPR_x64_ENU.exe", "", "", True, 500, True)


            '=========EXTRACT THE FILES - NEED PROGRESS BAR =======================================

            Dim temppath As String = ControlChars.Quote & Path.Combine(Application.StartupPath, "C:\DTHSQL2012\SQLEXPR_x64_ENU.exe") & ControlChars.Quote
            Dim processObj As Process = Process.Start(temppath, " /Q /X:C:\DTHSQL2012\ExtractFile")

            '============INSTALL SQL - NEED PROGRESS BAR ===============================================
            processObj = Process.Start("C:\DTHSQL2012\ExtractFile\setup.exe", "/q /Action=Install /IACCEPTSQLSERVERLICENSETERMS /Hideconsole /Features=SQLEngine /InstanceName=DTH2012 /SQLSYSADMINACCOUNTS=""NT AUTHORITY\SYSTEM"" /SQLSVCACCOUNT=""NT AUTHORITY\SYSTEM"" /BROWSERSVCSTARTUPTYPE=""Automatic""")
            Me.Label1.Text = ("Please wait while installing SQL 2012...")

            If processObj.ExitCode = 0 Then

                Label1.Text = "SQL 2012 Successfully Installed!"
                '=======start download of home management system ===============================
                System.Diagnostics.Process.Start("http://desktophousewife.com/desktopHousewife.application")

            Else

                Label1.Text = "SQL 2012 Install Failed. . . Code: " & processObj.ExitCode.ToString
                Dim mMessage As String = "SQL 2012 Install Failed. . . Code: " & processObj.ExitCode.ToString
                System.Diagnostics.Process.Start("mailto:karen@desktophousewife.com&subject=Desktop Housewife&body= " & mMessage)
            End If

        End If

    End Sub

End Class
0
Comment
Question by:Karen Wilson
  • 7
  • 4
11 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 40213080
You need to get that code out of the Load() event of the Form, and into the DoWork() handler of a BackgroundWorker() control.

Take a look at the example on MSDN to get an idea of how to do it...
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx

Also, instead of starting things from Load(), start the BackgroudWorker() from the Shown() event of the Form.
0
 

Author Comment

by:Karen Wilson
ID: 40213180
I do not have my install information correct, as it runs 10 times....  Thanks goodness because this morning's loop never ended.  

Here is the code:


    ' This event handler is where the time-consuming work is done.
    Private Sub backgroundWorker1_DoWork(ByVal sender As System.Object, _
    ByVal e As DoWorkEventArgs) Handles backgroundWorker1.DoWork
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim i As Integer

        For i = 1 To 10
            If (worker.CancellationPending = True) Then
                e.Cancel = True
                Exit For
            Else
                ' Perform a time consuming operation and report progress.
                System.Threading.Thread.Sleep(500)
                worker.ReportProgress(i * 10)

                '============================ what I inserted ==================================================================
                '================================================================================================================

                If My.Computer.FileSystem.DirectoryExists("C:\Program Files\Microsoft SQL Server\MSSQL11.DTH2012") Then

                    '=====THIS RUNS 10 TIMES WHICH IS THE INCREMENTS FROM ABOVE FOR I = 1 TO 10 ======================

                    System.Diagnostics.Process.Start("http://desktophousewife.com/desktopHousewife.application")
                Else
                    Try
                        '===========INSTALL SQL SERVER AND/OR HOME MANAGEMENT SYSTEM =========================
                        installSQL()

                    Catch ex As Exception
                        MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                End If
                '===================================================================================================================
                '===================================================================================================================
            End If
        Next
    End Sub

Also, is this correct:


    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        BackgroundWorker1.RunWorkerAsync()
    End Sub

Thanks!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40213234
The loop in the example was simply there so that it would have "steps" and it could report something.

You DO NOT need that loop in YOUR code!

Instead, after each step in your process, you'd manually report back the percentage complete.

Something like:
' Do some step
worker.ReportProgress(10)

' Do some other step
worker.ReportProgress(20)

' Do another step
worker.ReportProgress(30)

' ... etc ...

Open in new window


You'll have to decide how many "steps" are in YOUR process and adjust the values that are hardcoded into ReportProgress() accordingly.

Hope that helps...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Karen Wilson
ID: 40214742
I put a timer in the system to perform increments of percent done.  It is working.  It is chugging along well until it's time to install SQL, then I get an error that it can't find the file.  I look in the directory, and it's there.  Everything is named correctly.

 ' This event handler is where the time-consuming work is done.
    Private Sub backgroundWorker1_DoWork(ByVal sender As System.Object, _
    ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        Try

            Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)

            'Perform a time consuming operation and report progress.
            System.Threading.Thread.Sleep(500)

            'START THE FULL DOWNLOAD SQL EXPRESS AND HOME MANAGEMENT SYSTEM

            My.Computer.FileSystem.CreateDirectory("C:\HEREWEGO")
            My.Computer.Network.DownloadFile("http://desktophousewife.com/SQLEXPR_x64_ENU.exe", "C:\HEREWEGO\SQLEXPR_x64_ENU.exe", "", "", True, 500, True)


            Dim temppath As String = ControlChars.Quote & Path.Combine(Application.StartupPath, "C:\HEREWEGO\SQLEXPR_x64_ENU.exe") & ControlChars.Quote
            Dim processObj As Process = Process.Start(temppath, " /Q /X:C:\HEREWEGO\ExtractFile")

            '========HERE IT STOPS========
            processObj = Process.Start("C:\HEREWEGO\ExtractFile\setup.exe", "/q /Action=Install /IACCEPTSQLSERVERLICENSETERMS /Hideconsole /Features=SQLEngine /InstanceName=HEREWEGO /SQLSYSADMINACCOUNTS=""NT AUTHORITY\SYSTEM"" /SQLSVCACCOUNT=""NT AUTHORITY\SYSTEM"" /BROWSERSVCSTARTUPTYPE=""Automatic""")

            'Start download of home management system
            System.Diagnostics.Process.Start("http://desktophousewife.com/desktopHousewife.application")

        Catch ex As Exception
            MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

Any suggestions?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40214803
You don't need the Sleep() calls:

    'Perform a time consuming operation and report progress.
    System.Threading.Thread.Sleep(500)

That is used to SIMULATE "work" in example code that doesn't actually do anything (otherwise the code would just run too fast and you wouldn't see that the progress bar works).

This line seems suspect to me:

    Dim temppath As String = ControlChars.Quote & Path.Combine(Application.StartupPath, "C:\HEREWEGO\SQLEXPR_x64_ENU.exe") & ControlChars.Quote

The SECOND part being supplied to Combine() already has a root in it "C:\", so it probably won't combine well with Application.StartupPath!
0
 

Author Comment

by:Karen Wilson
ID: 40214825
That process works though.......  everything is in the directory.  I'm going to try making the sql install a new process in the code.
0
 

Author Comment

by:Karen Wilson
ID: 40214871
That did not work.  I paused the program right before the start of
processObj = Process.Start("C:\HEREWEGO2\ExtractFile\setup.exe"  and it's there!!  

So then I deleted all the code before the line and it jumped past it and went to
  System.Diagnostics.Process.Start("http://desktophousewife.com/desktopHousewife.application") and it ran.  

Next trick - I ran the code, before it got to System.Diagnostics.Process.Start("http://desktophousewife.com/desktopHousewife.application"), I paused it.  Now SQL is installing and I have the instance listed in Microsoft SQL Server.  

I guess I need to pause the processes until they complete?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40214886
You can try WaitForExit()...though results vary based on how the external application was designed.

Something like:

        processObj = Process.Start("C:\HEREWEGO2\ExtractFile\setup.exe")
        processObj.WaitForExit()
0
 

Author Comment

by:Karen Wilson
ID: 40214892
I am trying that right now.
0
 

Author Comment

by:Karen Wilson
ID: 40214908
I think it worked!!  Now to try the whole thing on my dad.  :O)  I'll let you know if I have success!!  Thanks very much in the mean time.  I always cheer when I get Idle_Mind to respond.
0
 

Author Closing Comment

by:Karen Wilson
ID: 40215011
My dad isn't available for troubleshoot but you have answered the question.  Thanks so much for your time!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Import Data from Multiple Text Files in Excel 12 85
Iteration Help (Asp.net VB) 5 36
ModalPopup  question 22 38
Find date of 2nd Thursday of each month 3 33
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question