Question

How to correctly use a progress bar while a long task is running visual studio 2008

Asked by: tentavarious

Hello experts I have a task that runs when my programs main form loads.  The task queries our database and dynamically creates panels and adds them to a number of flowlayoutpanel controls.  This process can take some time so I want a progress bar to show up while the records are being loaded.  I can't quite get the progress bar to function, its like nothing will work until the main task is complete.  I am new to visual studio 2008, so am I doing something wrong.  In 2003, I could use a timer or a system.thread.timer.  Any ideas?

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
          tloading.Start() 'Timer that controls the progress bar
        record_loading_status = True
        Application.DoEvents()
        build_query() 'This is the main task didnt think i would need to show this procedure
        tloading.Stop()
        record_loading_status = False
        Me.ss_status_text.Text = "Ready!"
        ss_progress_bar.Visible = False
    End Sub
 
 Private Sub tloading_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tloading.Tick
        If record_loading_status Then
            If Me.ss_progress_bar.Value < ss_progress_bar.Maximum Then
                  ss_progress_bar.Increment(10)
            Else
                ss_progress_bar.Value = 0
            End If
        End If
    End Sub

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-04-09 at 07:28:33ID24309566
Tags

microsoft visual studio 2008

,

vb.net

,

.net 3.5

Topics

.NET Framework 3.x versions

,

Microsoft Visual Basic.Net

,

Visual Studio

Participating Experts
3
Points
350
Comments
22

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Progress Page
    I have an ASP that takes some time to load into the client. (PAGE1) I do not want the user to see ANY part of PAGE1 until it is fully loaded. I want to have a way of letting the user know that their request is currently being processed until the PAGE1 is fully loaded. ...
  2. Progress Bar
    Ok, I really feel like a moron having to ask this question but my brain is fried. I have a form with a progress bar and a timer. The form is just for visual. I want to simulate that the app is grading an exam so I am using a progress bar for the effect. I have the timer's int...
  3. VB6 using progress bar with timer control
    I have a splash screen that loads (form1) and it contains a timer control (timer1) and and progress bar (progressbar1). I want to set the timer control to 5 seconds and have the progress bar increment each second that the timer is counting. Once the timer is complete, it wi...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: CraigWagnerPosted on 2009-04-09 at 07:38:37ID: 24107624

The 'correct' way to accomplish this would be to run the long-running task on a background thread and use the foreground thread to update the progress bar. The simplest way to do that would be to use code similar to the following (it's in C# as it's the only example I had handy, should be very easy to translate).

            // spawn off a new thread to do the actual work
            Thread worker = new Thread(new ThreadStart(<methodToRunInBackground>));
            worker.Start();
 
            // update the progress bar while waiting for the
            //     other thread to get done with itself
            while (worker.IsAlive)
            {
                Thread.Sleep(1000);
                UpdateProgressBar();
            };
            
            progressBar1.Value = progressBar1.Minimum;
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

Select allOpen in new window

 

by: tentavariousPosted on 2009-04-09 at 08:05:11ID: 24107884

Yep, I tried that, but I get this error:  Cross-Thread operation not valid: Control 'flptube' accessed from a thread other then the thread it was created on.   The build_query, which is the main procedure, dynamically builds panels and adds them to that control flptube.  

I found something that is suppose to make the threading function more like .net 1.1  
Me.CheckForIllegalCrossThreadCalls = False.  This allows the cross threading, but then I get another error.  So I am kinda at a loss.

 

by: Idle_MindPosted on 2009-04-09 at 08:34:23ID: 24108215

With VB.Net 2008 you set the ProgressBar.Style to Marquee and it will continuosly move to show an operation is in progress:

    ss_progress_bar.Style = ProgressBarStyle.Marquee

Play with this simple example:

Public Class Form1
 
    Private WithEvents bgw As New System.ComponentModel.BackgroundWorker
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ProgressBar1.Style = ProgressBarStyle.Marquee
        bgw.WorkerReportsProgress = True
        bgw.RunWorkerAsync()
    End Sub
 
    Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
        ' this is running in a DIFFERENT thread
        For i As Integer = 1 To 5
            Dim btn As New Button
            btn.Location = New Point(10, i * 25)
            btn.Text = "#" & i
            AddHandler btn.Click, AddressOf btn_Click
            bgw.ReportProgress(0, btn)
            System.Threading.Thread.Sleep(2000) ' simulated "work" so you can see the progressbar "marquee" mode in action
        Next
    End Sub
 
    Private Sub bgw_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgw.ProgressChanged
        ' this is running in the main UI Thread
        Me.Controls.Add(e.UserState)
    End Sub
 
    Private Sub bgw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
        ' this is running in the main UI Thread
        ProgressBar1.Style = ProgressBarStyle.Blocks
        ProgressBar1.Visible = False
    End Sub
 
    Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        MessageBox.Show(sender.text)
    End Sub
 
End Class
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:

Select allOpen in new window

 

by: CraigWagnerPosted on 2009-04-09 at 08:40:25ID: 24108292

That CheckForIllegalCrossThreadCalls property in .NET 1.1 was a bit of a cheat. In .NET 2.0 and up you need to check to see if an invoke is required and if so marshall the call back to the UI thread. I've attached some code that does this.

In this case the method that I'm calling (BuildDeployList) doesn't take any argument so I can use the Action delegate that is built into the framework. If your method (the one that adds the panel to the UI) takes argument you'll need to declare a delegate for it and use it in place of Action, or use the generic Action delegate. For example:

        private delegate void SetToolStripItemEnabledStateDelegate( ToolStripItem item, bool state );

or

       Action<int, string>

private void BuildDeployList()
{
    if( this.InvokeRequired )
    {
        Action action = BuildDeployList;
        this.Invoke( action );
    }
    else
    {
        AddTestsToListBox( lstAvailableTests, Directory.GetFiles( DeployFolder, "*.zip" ) );
        SetControlStates();
    }
}

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

Select allOpen in new window

 

by: tentavariousPosted on 2009-04-09 at 11:03:52ID: 24109811

Ok, I am a little confused on how to get this to work

Here is the thread that executes the query: build_query, and updates the progress bar.  Within the the build_query I call another procedure that interacts with the control and gets passed 8 paramaters.  I am unsure how to make this work.
I took out a lot of code that involves query the db and adding some stuff that doesnt matter with my question.  If you look at the build_panel procedure the last line where I try to add the controls is where the error occurs.  I am not sure how to declare the delegate.

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      Dim worker As New System.Threading.Thread(AddressOf build_query)
        worker.Start()
        While worker.IsAlive
            Threading.Thread.Sleep(900)
            If Me.ss_progress_bar.Value < ss_progress_bar.Maximum Then
                ss_progress_bar.Increment(10)
            Else
                ss_progress_bar.Value = 0
            End If
        End While
        ss_progress_bar.Value = ss_progress_bar.Minimum
    End Sub
 
  Sub build_query()
        Dim data_adap As OracleDataAdapter
        Dim ds As New DataSet
        Dim dr As DataRow
        Dim job, item, description, qty, temp_qty As String
        Dim col_name As String
        Dim sched_start_date As Date
        ssql = "select columns here from table name"
        data_adap = New OracleDataAdapter(ssql, ora_conn)
        Try
            data_adap.Fill(ds, "Table1")
            For Each dr In ds.Tables(0).Rows
                job = dr.Item(0).ToString
                item = dr.Item(1).ToString
                description = dr.Item(2).ToString
                sched_start_date = dr.Item(3)
                qty = dr.Item(4).ToString
                For i As Integer = 5 To ds.Tables(0).Columns.Count - 1
                    Dim l As New Label
                    Dim v As Array
 col_name = ds.Tables(0).Columns.Item(i).ColumnName                     
v = Me.Controls.Find("flp" & col_name.ToLower, True)
                    If v.Length >= 1 Then
                        temp_qty = dr.Item(i + 1).ToString
build_panel(job, item, description, sched_start_date, qty, temp_qty, v(0), dr.Item(i).ToString)
                        i += 1
                    End If
                Next
                row_count += 1
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
 
 Sub build_panel(ByVal job_number As String, ByVal item_number As String, ByVal description As String, ByVal shed_date As Date, ByVal total_qty As String, ByVal run_qty As String, ByVal temp_panel As FlowLayoutPanel, ByVal part_number As String)
        Dim ptemp As New Panel
        Dim l_desc As New Label
        Dim l_part_num As New Label
        Dim l_qty As New Label
        Dim l_date As New Label
           With l_part_num
            .Text = part_number
            .ForeColor = Color.Black
            .AutoSize = True
            .Font = New Font(Font.Name, 8)
            .Location = New Point(ptemp.Location.X + 22, ptemp.Location.Y - 1)
        End With
        With l_desc
            .Text = description            
            .ForeColor = Color.Black
            .Font = New Font(Font.Name, 5.5, FontStyle.Regular)
            .AutoSize = True
            .Location = New Point(ptemp.Location.X + 1, ptemp.Location.Y + 13)
        End With
        With l_date
            .Text = shed_date.ToString("M/d")
            .ForeColor = Color.Black
            .AutoSize = True
            .Font = New Font(Font.Name, 7)
            .Location = New Point(ptemp.Location.X - 2, ptemp.Location.Y - 1)
        End With
         With l_qty
            .Text = run_qty & "/" & total_qty
            .ForeColor = Color.Black()
            .BackColor = Color.White
            .AutoSize = True
            .RightToLeft = Windows.Forms.RightToLeft.Inherit
            .Location = New Point(ptemp.Location.X + 27, ptemp.Location.Y + 21)
            .Font = New Font(Font.Name, 7)
            .TextAlign = ContentAlignment.MiddleCenter
        End With
          ptemp.Controls.Add(l_part_num)
            ptemp.Controls.Add(l_date)
            ptemp.Controls.Add(l_desc)
            ptemp.Controls.Add(l_qty)
        temp_panel.Controls.Add(ptemp)
    End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:

Select allOpen in new window

 

by: CraigWagnerPosted on 2009-04-09 at 11:22:41ID: 24109993

Because it takes eight parameters you won't be able to use Action, it maxes out at four parameters.

I don't use VB.NET, I'm developing in C#, so I don't know the syntax for declaring a delegate off the top of my head in VB.NET.

Accoding to this page (http://www.developerfusion.com/article/5251/delegates-in-vbnet) the declaration is the same as any other variable in VB.NET.

Private Delegate Sub MyDelSub()

The delegate is a variable that represents your method, so it must match the method in terms of arguments and return type. In your case:

Private Delegate Sub BuildPanelDelegate(ByVal job_number As String, ByVal item_number As String, ByVal description As String, ByVal shed_date As Date, ByVal total_qty As String, ByVal run_qty As String, ByVal temp_panel As FlowLayoutPanel, ByVal part_number As String)

Create an if/else statement with a condition checking the form for InvokeRequired. Put all the existing code in the else block. In the if block, declare an instance of the delegate and invoke it.

Some of the syntax may be wrong, as I mentioned I don't use VB.NET and I'm hand-coding this without benefit of Intellisense.

If Me.InvokeRequired Then
    ' Declare a variable to hold a pointer to the function
    Dim theDelegate As BuildPanelDelegate
    ' Assign the variable the address of the function to call
    theDelegate = new BuildPanelDelegate(AddressOf build_panel)
    ' Marshall the function call back to the UI thread
    theDelegate.Invoke( job_number, item_number, etc... the rest of the arguments)
Else
    ' all the existing code goes here
End If

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:

Select allOpen in new window

 

by: tentavariousPosted on 2009-04-09 at 16:14:46ID: 24112536

I set up the delegate and it loads the procedure fine, but I still get the same error when it tries to add controls to the panel.  Not sure what i am doing wrong.

Public Delegate Sub delegate_panel(ByVal job_number As String, ByVal item_number As String, ByVal description As String, ByVal shed_date As Date, ByVal total_qty As String, ByVal run_qty As String, ByVal temp_panel As FlowLayoutPanel, ByVal part_number As String)
 
 
        If Me.InvokeRequired Then
            Dim objdele As delegate_panel
            objdele = AddressOf delegate_panel_build
            objdele.Invoke(job_number, item_number, description, shed_date, total_qty, run_qty, temp_panel, part_number)
        Else
           'other work here
   end if
 
 Sub delegate_panel_build(ByVal job_number As String, ByVal item_number As String, ByVal description As String, ByVal shed_date As Date, ByVal total_qty As String, ByVal run_qty As String, ByVal temp_panel As FlowLayoutPanel, ByVal part_number As String)
        Dim ptemp As New Panel
        Dim l_desc As New Label
        Dim l_part_num As New Label
        Dim l_qty As New Label
        Dim l_date As New Label
        With l_part_num
            .Text = part_number
            .ForeColor = Color.Black
            .AutoSize = True
            .Font = New Font(Font.Name, 8)
            .Location = New Point(ptemp.Location.X + 22, ptemp.Location.Y - 1)
        End With
        With l_desc
            .Text = description
            .ForeColor = Color.Black
            .Font = New Font(Font.Name, 5.5, FontStyle.Regular)
            .AutoSize = True
            .Location = New Point(ptemp.Location.X + 1, ptemp.Location.Y + 13)
        End With
        With l_date
            .Text = shed_date.ToString("M/d")
            .ForeColor = Color.Black
            .AutoSize = True
            .Font = New Font(Font.Name, 7)
            .Location = New Point(ptemp.Location.X - 2, ptemp.Location.Y - 1)
        End With
        If run_qty = "" Then run_qty = 123
        With l_qty
            .Text = run_qty & "/" & total_qty
            .ForeColor = Color.Black()
            .BackColor = Color.White
            .AutoSize = True
            .RightToLeft = Windows.Forms.RightToLeft.Inherit
            .Location = New Point(ptemp.Location.X + 27, ptemp.Location.Y + 21)
            .Font = New Font(Font.Name, 7)
            .TextAlign = ContentAlignment.MiddleCenter
        End With
        If part_number <> "" Then
            ptemp.Controls.Add(l_part_num)
            ptemp.Controls.Add(l_date)
            ptemp.Controls.Add(l_desc)
            ptemp.Controls.Add(l_qty)
            ptemp.Tag = job_number & "~" & item_number & "~" & part_number
            ptemp.Width = temp_panel.Width - 5
            ptemp.Height = 34
            ptemp.BorderStyle = BorderStyle.None
            ptemp.BackColor = Color.Yellow
            AddHandler ptemp.Click, AddressOf Ptemp_Click
            AddHandler l_desc.Click, AddressOf Ptemp_Click
            AddHandler l_qty.Click, AddressOf Ptemp_Click
            AddHandler l_part_num.Click, AddressOf Ptemp_Click
            AddHandler l_date.Click, AddressOf Ptemp_Click
        Else
            ptemp.Tag = "0"
            ptemp.Width = temp_panel.Width
            ptemp.Height = 34
            ptemp.BorderStyle = BorderStyle.None
            ptemp.BackColor = Color.Transparent
        End If
        temp_panel.Controls.Add(ptemp)
    End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:

Select allOpen in new window

 

by: CraigWagnerPosted on 2009-04-09 at 16:51:41ID: 24112663

A delegate is a variable declaration, not a method declaration. It looks like you declared your delegate and then gave a method body to it. If what you pasted is your actual code, I'm a little surprised it even compiled, but as I've said before, I don't use VB.NET.

See below for how it should be set up.

Private Delegate Sub delegate_panel(ByVal job_number As String, ByVal item_number As String, ByVal description As String, ByVal shed_date As Date, ByVal total_qty As String, ByVal run_qty As String, ByVal temp_panel As FlowLayoutPanel, ByVal part_number As String)
 
Sub delegate_panel_build(ByVal job_number As String, ByVal item_number As String, ByVal description As String, ByVal shed_date As Date, ByVal total_qty As String, ByVal run_qty As String, ByVal temp_panel As FlowLayoutPanel, ByVal part_number As String)
    If Me.InvokeRequired Then
        Dim objdele As delegate_panel
        objdele = AddressOf delegate_panel_build
        objdele.Invoke(job_number, item_number, description, shed_date, total_qty, run_qty, temp_panel, part_number)
    Else
        Dim ptemp As New Panel
        Dim l_desc As New Label
        Dim l_part_num As New Label
        Dim l_qty As New Label
        Dim l_date As New Label
        With l_part_num
            .Text = part_number
            .ForeColor = Color.Black
            .AutoSize = True
            .Font = New Font(Font.Name, 8)
            .Location = New Point(ptemp.Location.X + 22, ptemp.Location.Y - 1)
        End With
        With l_desc
            .Text = description
            .ForeColor = Color.Black
            .Font = New Font(Font.Name, 5.5, FontStyle.Regular)
            .AutoSize = True
            .Location = New Point(ptemp.Location.X + 1, ptemp.Location.Y + 13)
        End With
        With l_date
            .Text = shed_date.ToString("M/d")
            .ForeColor = Color.Black
            .AutoSize = True
            .Font = New Font(Font.Name, 7)
            .Location = New Point(ptemp.Location.X - 2, ptemp.Location.Y - 1)
        End With
        If run_qty = "" Then run_qty = 123
        With l_qty
            .Text = run_qty & "/" & total_qty
            .ForeColor = Color.Black()
            .BackColor = Color.White
            .AutoSize = True
            .RightToLeft = Windows.Forms.RightToLeft.Inherit
            .Location = New Point(ptemp.Location.X + 27, ptemp.Location.Y + 21)
            .Font = New Font(Font.Name, 7)
            .TextAlign = ContentAlignment.MiddleCenter
        End With
        If part_number <> "" Then
            ptemp.Controls.Add(l_part_num)
            ptemp.Controls.Add(l_date)
            ptemp.Controls.Add(l_desc)
            ptemp.Controls.Add(l_qty)
            ptemp.Tag = job_number & "~" & item_number & "~" & part_number
            ptemp.Width = temp_panel.Width - 5
            ptemp.Height = 34
            ptemp.BorderStyle = BorderStyle.None
            ptemp.BackColor = Color.Yellow
            AddHandler ptemp.Click, AddressOf Ptemp_Click
            AddHandler l_desc.Click, AddressOf Ptemp_Click
            AddHandler l_qty.Click, AddressOf Ptemp_Click
            AddHandler l_part_num.Click, AddressOf Ptemp_Click
            AddHandler l_date.Click, AddressOf Ptemp_Click
        Else
            ptemp.Tag = "0"
            ptemp.Width = temp_panel.Width
            ptemp.Height = 34
            ptemp.BorderStyle = BorderStyle.None
            ptemp.BackColor = Color.Transparent
        End If
        temp_panel.Controls.Add(ptemp)
    End If
End Sub
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:

Select allOpen in new window

 

by: tentavariousPosted on 2009-04-09 at 17:13:37ID: 24112733

Ok, I changed it to what you had, now it gets into a never ending loop when it checks the condition

  If Me.InvokeRequired Then
             Dim objdele As delegate_panel
            objdele = AddressOf delegate_panel_build
            objdele.Invoke(job_number, item_number, description, shed_date, total_qty, run_qty, temp_panel, part_number)

else

end if


Something is not right I dont think i am calling the procedure right.  When the form loads it create the worker thread which calls the build_query procedure.  Within the build_query procedure, I loop the contents of a dataset, then call the delegate_panel_build procedure which contains the delegate call and invoke.  Is this right so far, if it is something is wrong.

  Private Delegate Sub delegate_panel(ByVal job_number As String, ByVal item_number As String, ByVal description As String, ByVal shed_date As Date, ByVal total_qty As String, ByVal run_qty As String, ByVal temp_panel As FlowLayoutPanel, ByVal part_number As String)
 
 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim worker As New System.Threading.Thread(AddressOf build_query)
        worker.Start()
        While worker.IsAlive
            Threading.Thread.Sleep(700)
            If Me.ss_progress_bar.Value < ss_progress_bar.Maximum Then
                ss_progress_bar.Increment(10)
            Else
                ss_progress_bar.Value = 0
            End If
        End While
        ss_progress_bar.Value = ss_progress_bar.Minimum
    End Sub
 
   Sub build_query()
         Dim data_adap As OracleDataAdapter
        Dim ds As New DataSet
        Dim row_count As Integer = 0 'keeps track of the rows
        Dim dr As DataRow
        Dim job, item, description, qty, temp_qty As String
        Dim col_name As String
        Dim sched_start_date As Date
        ssql = "select here"
        data_adap = New OracleDataAdapter(ssql, ora_conn)
        Try
            data_adap.Fill(ds, "Table1")
            For Each dr In ds.Tables(0).Rows
                job = dr.Item(0).ToString
                item = dr.Item(1).ToString
                description = dr.Item(2).ToString
                sched_start_date = dr.Item(3)
                qty = dr.Item(4).ToString
                For i As Integer = 5 To ds.Tables(0).Columns.Count - 1
                    'This loop is for looping all the cylinder components
                    Dim l As New Label
                    Dim v As Array
                    col_name = ds.Tables(0).Columns.Item(i).ColumnName 'Get the column name so they can be sorted by component
                    v = Me.Controls.Find("flp" & col_name.ToLower, True)
                    If v.Length >= 1 Then
                        temp_qty = dr.Item(i + 1).ToString
                        delegate_panel_buildjob, item, description, sched_start_date, qty, temp_qty, v(0), dr.Item(i).ToString)
                        i += 1
                    End If
                Next
                row_count += 1
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
 
        End Try
    End Sub
 
Sub delegate_panel_build(ByVal job_number As String, ByVal item_number As String, ByVal description As String, ByVal shed_date As Date, ByVal total_qty As String, ByVal run_qty As String, ByVal temp_panel As FlowLayoutPanel, ByVal part_number As String)
        Dim ptemp As New Panel
        Dim l_desc As New Label
        Dim l_part_num As New Label
        Dim l_qty As New Label
        Dim l_date As New Label
        If Me.InvokeRequired Then
             Dim objdele As delegate_panel
            objdele = AddressOf delegate_panel_build
            objdele.Invoke(job_number, item_number, description, shed_date, total_qty, run_qty, temp_panel, part_number)
        Else
            With l_part_num
                .Text = part_number
                .ForeColor = Color.Black
                .AutoSize = True
                .Font = New Font(Font.Name, 8)
                .Location = New Point(ptemp.Location.X + 22, ptemp.Location.Y - 1)
            End With
            With l_desc
                .Text = description.Replace("CYL ASSY", "")
                .ForeColor = Color.Black
                .Font = New Font(Font.Name, 5.5, FontStyle.Regular)
                .AutoSize = True
                .Location = New Point(ptemp.Location.X + 1, ptemp.Location.Y + 13)
            End With
            With l_date
                .Text = shed_date.ToString("M/d")
                .ForeColor = Color.Black
                .AutoSize = True
                .Font = New Font(Font.Name, 7)
                .Location = New Point(ptemp.Location.X - 2, ptemp.Location.Y - 1)
            End With
            If run_qty = "" Then run_qty = 123
            With l_qty
                .Text = run_qty & "/" & total_qty
                .ForeColor = Color.Black()
                .BackColor = Color.White
                .AutoSize = True
                .RightToLeft = Windows.Forms.RightToLeft.Inherit
                .Location = New Point(ptemp.Location.X + 27, ptemp.Location.Y + 21)
                .Font = New Font(Font.Name, 7)
                .TextAlign = ContentAlignment.MiddleCenter
            End With
            If part_number <> "" Then
                ptemp.Controls.Add(l_part_num)
                ptemp.Controls.Add(l_date)
                ptemp.Controls.Add(l_desc)
                ptemp.Controls.Add(l_qty)
                ptemp.Tag = job_number & "~" & item_number & "~" & part_number
                ptemp.Width = temp_panel.Width - 5
                ptemp.Height = 34
                ptemp.BorderStyle = BorderStyle.None
                ptemp.BackColor = Color.Yellow
                AddHandler ptemp.Click, AddressOf Ptemp_Click
                AddHandler l_desc.Click, AddressOf Ptemp_Click
                AddHandler l_qty.Click, AddressOf Ptemp_Click
                AddHandler l_part_num.Click, AddressOf Ptemp_Click
                AddHandler l_date.Click, AddressOf Ptemp_Click
            Else
                ptemp.Tag = "0"
                ptemp.Width = temp_panel.Width
                ptemp.Height = 34
                ptemp.BorderStyle = BorderStyle.None
                ptemp.BackColor = Color.Transparent
            End If
            temp_panel.Controls.Add(ptemp)
        End If
    End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:

Select allOpen in new window

 

by: Idle_MindPosted on 2009-04-09 at 17:21:46ID: 24112763

Change:

        If Me.InvokeRequired Then
            Dim objdele As delegate_panel
            objdele = AddressOf delegate_panel_build
            objdele.Invoke(job_number, item_number, description, shed_date, total_qty, run_qty, temp_panel, part_number)

To:

        If Me.InvokeRequired Then
            Me.Invoke(New delegate_panel(AddressOf delegate_panel_build),  New Object() {job_number, item_number, description, shed_date, total_qty, run_qty, temp_panel, part_number})

 

by: tentavariousPosted on 2009-04-09 at 17:29:01ID: 24112791

Tried that as well and it just keeps thinking nothing happens.

 

by: tentavariousPosted on 2009-04-10 at 17:09:06ID: 24120329

When the program tries to execute this line of code, the thread just locks up and nothing happens, any ideas?  Am I still not invoking the the delegate property.

 Me.Invoke(New delegate_panel(AddressOf delegate_panel_build), New Object() {job_number, item_number, description, shed_date, total_qty, run_qty, temp_panel, part_number})

 

by: Idle_MindPosted on 2009-04-10 at 17:39:48ID: 24120407

I haven't studied your code...I'll take a CLOSER look later...

 

by: CraigWagnerPosted on 2009-04-10 at 19:32:21ID: 24120706

I've been playing around with it. The problem is the while loop that updates the progress bar. I've been messing with just using marquee mode of the progress bar. It starts properly, but it's not stopping right now. As it's Friday night and guests have arrived, I can't take any more time right now. Here's the hacked up code that I've got working (had to hack around not having an actual database handy).

Public Class Form1
    Private Delegate Sub delegate_panel(ByVal job_number As String, ByVal item_number As String, ByVal description As String, ByVal shed_date As Date, ByVal total_qty As String, ByVal run_qty As String, ByVal temp_panel As FlowLayoutPanel, ByVal part_number As String)
 
    Sub panel_build(ByVal job_number As String, ByVal item_number As String, ByVal description As String, ByVal shed_date As Date, ByVal total_qty As String, ByVal run_qty As String, ByVal temp_panel As FlowLayoutPanel, ByVal part_number As String)
        If Me.InvokeRequired Then
            Dim objdele As delegate_panel
            objdele = AddressOf panel_build
            'objdele.Invoke(job_number, item_number, description, shed_date, total_qty, run_qty, temp_panel, part_number)
            Me.Invoke(New delegate_panel(AddressOf panel_build), New Object() {job_number, item_number, description, shed_date, total_qty, run_qty, temp_panel, part_number})
        Else
            Dim ptemp As New Panel
            Dim l_desc As New Label
            Dim l_part_num As New Label
            Dim l_qty As New Label
            Dim l_date As New Label
            With l_part_num
                .Text = part_number
                .ForeColor = Color.Black
                .AutoSize = True
                .Font = New Font(Font.Name, 8)
                .Location = New Point(ptemp.Location.X + 22, ptemp.Location.Y - 1)
            End With
            With l_desc
                .Text = description
                .ForeColor = Color.Black
                .Font = New Font(Font.Name, 5.5, FontStyle.Regular)
                .AutoSize = True
                .Location = New Point(ptemp.Location.X + 1, ptemp.Location.Y + 13)
            End With
            With l_date
                .Text = shed_date.ToString("M/d")
                .ForeColor = Color.Black
                .AutoSize = True
                .Font = New Font(Font.Name, 7)
                .Location = New Point(ptemp.Location.X - 2, ptemp.Location.Y - 1)
            End With
            If run_qty = "" Then run_qty = 123
            With l_qty
                .Text = run_qty & "/" & total_qty
                .ForeColor = Color.Black()
                .BackColor = Color.White
                .AutoSize = True
                .RightToLeft = Windows.Forms.RightToLeft.Inherit
                .Location = New Point(ptemp.Location.X + 27, ptemp.Location.Y + 21)
                .Font = New Font(Font.Name, 7)
                .TextAlign = ContentAlignment.MiddleCenter
            End With
            If part_number <> "" Then
                ptemp.Controls.Add(l_part_num)
                ptemp.Controls.Add(l_date)
                ptemp.Controls.Add(l_desc)
                ptemp.Controls.Add(l_qty)
                ptemp.Tag = job_number & "~" & item_number & "~" & part_number
                ptemp.Width = temp_panel.Width - 5
                ptemp.Height = 34
                ptemp.BorderStyle = BorderStyle.None
                ptemp.BackColor = Color.Yellow
            Else
                ptemp.Tag = "0"
                ptemp.Width = temp_panel.Width
                ptemp.Height = 34
                ptemp.BorderStyle = BorderStyle.None
                ptemp.BackColor = Color.Transparent
            End If
            temp_panel.Controls.Add(ptemp)
        End If
    End Sub
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim worker As New System.Threading.Thread(AddressOf build_query)
        worker.Start()
    End Sub
 
    Sub start_progress()
        If Me.InvokeRequired Then
            Dim dele As Action
            dele = AddressOf start_progress
            Me.Invoke(dele)
        Else
            ss_progress_bar.Style = ProgressBarStyle.Marquee
        End If
    End Sub
 
    Sub stop_progress()
        If Me.InvokeRequired Then
            Dim dele As Action
            dele = AddressOf start_progress
            Me.Invoke(dele)
        Else
            ss_progress_bar.Style = ProgressBarStyle.Blocks
        End If
    End Sub
 
    Sub build_query()
        Try
            Dim dt As DataTable = New DataTable()
            Dim s As String = "fred"
            Dim job As String
            Dim item As String
            Dim description As String
            Dim sched_start_date As DateTime
            Dim qty As String
            Dim col_name As String
            Dim temp_qty As String
 
            start_progress()
 
            dt.Columns.Add("First", s.GetType())
            dt.Columns.Add("Second", s.GetType())
 
            For i As Integer = 1 To 1000
                job = i.ToString
                item = i.ToString
                description = i.ToString
                sched_start_date = DateTime.Now
                qty = i.ToString
                For j As Integer = 0 To dt.Columns.Count - 1
                    'This loop is for looping all the cylinder components
                    Dim l As New Label
                    Dim v As Array
                    col_name = dt.Columns.Item(j).ColumnName 'Get the column name so they can be sorted by component
                    v = Me.Controls.Find("flp" & col_name, True)
                    If v.Length >= 1 Then
                        temp_qty = (i + 1).ToString
                        panel_build(job, item, description, sched_start_date, qty, temp_qty, v(0), i.ToString)
                        i += 1
                    End If
                Next
            Next
 
            stop_progress()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        Finally
        End Try
    End Sub
End Class
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:

Select allOpen in new window

 

by: tentavariousPosted on 2009-04-13 at 07:50:45ID: 24130218

Nice,
It looks like it worked.  Could you explain these 2 procedures I am not familiar with the Action delegate, is that new?
I am not familiar
 Sub start_progress()
        If Me.InvokeRequired Then
            Dim dele As Action
            dele = AddressOf start_progress
            Me.Invoke(dele)
        Else
            ss_progress_bar.Style = ProgressBarStyle.Marquee
        End If
    End Sub

    Sub stop_progress()
        If Me.InvokeRequired Then
            Dim dele As Action
            dele = AddressOf start_progress
            Me.Invoke(dele)
        Else
            ss_progress_bar.Style = ProgressBarStyle.Blocks
        End If
    End Sub

 

by: CraigWagnerPosted on 2009-04-13 at 08:06:50ID: 24130315

They just use Maquee mode, which keeps the progress bar moving all the time. When the long-running process starts it puts the bar into Marquee mode, when it's done it puts it back into regular mode.

As far as the Action delegate goes,

http://msdn.microsoft.com/en-us/library/system.action.aspx

"Encapsulates a method that takes no parameters and does not return a value."

 

by: tentavariousPosted on 2009-04-13 at 08:08:12ID: 24130326

The stop_process is not working, the progress bar never leaves marquee mode.

 

by: tentavariousPosted on 2009-04-13 at 08:22:56ID: 24130421

I got the stop_process working, but now I need to execute the worker thread every halfhour.  

 

by: tentavariousPosted on 2009-04-15 at 12:50:29ID: 24151862

Ok, i thought I had everything working, I created a timer thread that runs every 10 seconds.  It was working fine but I watched it for a while, then all of a sudden i got this error:  Error Creating Window Handle,  what is this?

I increased the points because this was a lot more work then I initionally thought.

   Dim duetime As New TimeSpan(0, 0, 1) 'how long to wait before starting timer
    Dim ticktime As New TimeSpan(0, 0, 0, 0, 10000) 'Number of seconds in milleseconds between each tick
  
 
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim frm_login As New Login
        frm_login.ShowDialog()
        If login_status Then
                  thtimer = New System.Threading.Timer(New System.Threading.TimerCallback(AddressOf build_query), Nothing, duetime, ticktime)
         End If
    End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen in new window

 

by: tentavariousPosted on 2009-04-15 at 12:56:20ID: 24151932

Figured out that it loops through 9 times, then it crashes.  Do i need to do some cleanup?

 

by: tentavariousPosted on 2009-04-15 at 14:06:48ID: 24152547

Getting closer here, I found out that i basically need to do some garbage cleanup before creating all the new panels.  I created a loop that should go through and dispose of all existing controls before creating new ones.  I can't quite get it to work and I am not sure where I should put the procedure call

 Sub clear_controls(ByVal ctrl As Control) 
        Dim frmctrl As Control
        Dim flow_p_controls As Control
        For Each frmctrl In ctrl.Controls
            If TypeOf frmctrl Is FlowLayoutPanel Then
                If frmctrl.HasChildren Then
                    For Each flow_p_controls In frmctrl.Controls
                        flow_p_controls.Dispose()
                    Next
                End If
            End If
            If frmctrl.HasChildren Then
                clear_controls(frmctrl)
            End If
        Next
    End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:

Select allOpen in new window

 

by: knowltonPosted on 2009-09-21 at 16:05:40ID: 25388486

I found this very helpful, thx.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...