Solved

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

Posted on 2014-07-22
11
203 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now