.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Home >> Forum >> Windows Application >> Post New QuestionBookmark and Share Subscribe to Forum

Progress Bar with Threading in Win App

Posted By: Suderson Sarathy     Posted Date: November 25, 2010    Points:2   Category :Windows Application
In C# Win App

I'm comapring 2 Big Excel Sheets. It is taking a long process. I want to display the progrrss bar accordingly. I don't know how long the process time is.

I don't know how to display the progress bar without knowing the time.

Author: krishana singh             
Posted Date: December 07, 2010     Points: 5   

Create a Visual Basic .NET or Visual Basic 2005 application with threads
Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
Create a new Visual Basic Windows Application project named ThreadWinApp.
Add a Button control to the form. The button is named Button1 by default.
Add a ProgressBar component to the form. The progress bar is named ProgressBar1 by default.
Right-click the form, and then click View Code.
Add the following statement to the beginning of the file:
Imports System.Threading

Add the following Click event handler for Button1:
Private Sub Button1_Click( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click

MessageBox.Show("This is the main thread")
End Sub

Add the following variable to the Form1 class:
Private trd As Thread

Add the following method to the Form1 class:
Private Sub ThreadTask()
Dim stp As Integer
Dim newval As Integer
Dim rnd As New Random()

stp = ProgressBar1.Step * rnd.Next(-1, 2)
newval = ProgressBar1.Value + stp
If newval > ProgressBar1.Maximum Then
newval = ProgressBar1.Maximum
ElseIf newval < ProgressBar1.Minimum Then
newval = ProgressBar1.Minimum
End If

ProgressBar1.Value = newval

End Sub

NOTE: This is the code that underlies the thread. This code is an infinite loop that randomly increments or decrements the value in ProgressBar1 and then waits 100 milliseconds before it continues.
Add the following Load event handler for Form1. This code creates a new thread, makes the thread a background thread, and then starts the thread.
Private Sub Form1_Load( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load

trd = New Thread(AddressOf ThreadTask)
trd.IsBackground = True
End Sub

Back to the top
Verify that it works
Build and run the application. Notice that the value in the progress bar changes randomly. This is the new thread in operation.
To demonstrate that the main thread is independent of the thread that changes the ProgressBar value, click the button on the form. A message box displays the following message:
This is the main thread

Wait for input. Notice that the value in the progress bar continues to change.

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend