Do Process while Progress Bar is Running - vb.net

I know that knowledge is expensive, but is there who want to help me
i want to run process when progress bar is running,
i try with this code
Public Class Form2
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Call Prcss()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Progress.Value < 100 Then
Progress.Value += 2
ElseIf Progress.Value = 100 Then
Timer1.Stop()
Form1.Show()
Me.Hide()
End If
End Sub
Private Sub Prcss()
With Progress
.Value = 0
Threading.Thread.Sleep(450)
Label1.Text = "Renewing Custom Content"
.Value = 20
Threading.Thread.Sleep(450)
Label1.Text = "Getting Information"
.Value = 50
Threading.Thread.Sleep(450)
Label1.Text = "Downloading Udpdate"
.Value = 70
Threading.Thread.Sleep(450)
Label1.Text = "Ready to Start"
.Value = 100
End With
End Sub
i don't know where is my mistake, i read this on my book.

Try using a BackgroundWorker or a Thread:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
For i As Integer = 1 To 1000
BackgroundWorker1.ReportProgress(CInt(i / 10))
Threading.Thread.Sleep(500)
Next
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
MessageBox.Show("Finished!")
End Sub
This is the basic example and require you to add a BackgroundWorker to the form. You can see help here: https://msdn.microsoft.com/es-es/library/cc221403(v=vs.95).aspx

Related

ProgressBar showing pourcentage

I wanted to show the pourcentage within the ProgressBar but it shows 1% for one file spotted instead of showing 100%.
screenshot
I understand why it shows me 1% and I know I've to change Label1Text to another value but I don't see which one would solve the problem.
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Timer1.Start()
Dim dossier = "C:\Users\adm\Desktop\A"
Dim lesFichiers() As System.IO.FileInfo
Dim dirinfo As New System.IO.DirectoryInfo(dossier)
lesFichiers = dirinfo.GetFiles("*", IO.SearchOption.AllDirectories)
For Each chaqueFichier In lesFichiers
ProgressBar1.Maximum = lesFichiers.Length
ProgressBar1.Value += 1
Next
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(2)
label1.Text = ProgressBar1.Value & " %"
If ProgressBar1.Value = ProgressBar1.Maximum Then
Timer1.Stop()
End If
End Sub
Private Sub Label1_Click(sender As System.Object, e As System.EventArgs)
Label1.BackColor = Color.Transparent
End Sub
Private Sub ProgressBar1_Click(sender As System.Object, e As System.EventArgs) Handles ProgressBar1.Click
End Sub
End Class

combobox multiple thread error

I have a problem with my code. I keep getting Multiple thread Error with backgroundworker, because of the combobox item display. Please look at my code below its a very simple code which I am planning to use on big scale, all I want it to do is "If item "1" selected show item "1" in label1. I can only assume that problem exists because Combobox runs in different thread....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.runworkerasync()
BackgroundWorker1.WorkerReportsProgress = True
Me.Cursor = Cursors.WaitCursor 'Cursor changes to wait
End Sub
Public Structure controlwithtext
Public controlname As Control
Public text As String
Public Sub New(ByVal ctrl As Control, ByVal text As String)
Me.controlname = ctrl
Me.text = text
End Sub
End Structure
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
If comboBox1.SelectedItem = "1" then
BackgroundWorker1.ReportProgress(5, New controlwithtext(Label1, ComboBox1.SelectedItem))
End If
End Sub
Private Sub SetBackgroundWorker_ProgressChanged(ByVal sender As Object,
ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
If TypeOf e.UserState Is controlwithtext Then
Dim cwt As controlwithtext = CType(e.UserState, controlwithtext)
cwt.controlname.Text = cwt.text
End If
End Sub
Here's an example of how to read from and write to controls from the BackgroundWorker thread:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
While True
System.Threading.Thread.Sleep(250)
Dim selection As String = Me.Invoke(Function()
If Not IsNothing(ComboBox1.SelectedItem) Then
Return ComboBox1.SelectedItem.ToString
Else
Return String.Empty
End If
End Function).ToString
If selection = "1" Then
Me.Invoke(Sub()
Label1.Text = ComboBox1.SelectedItem.ToString
End Sub)
Else
Me.Invoke(Sub()
Label1.Text = "something else"
End Sub)
End If
End While
End Sub

Button Click Event Within Another

I have two buttons Start button and Stop button .I run my program by clicking on start button. I want to stop the program during the start button . But the program will not responde until the start buttun finish its job. How i can do check the stop buttun during the start. i heard about threading but i do not know how i can do it.
Private Sub Button_Start (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
//my code
//check always if the user push stop if no continue if yes go to this sub
//my code
end sub
Private Sub Button_Stop (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
stopClick = True
Dim Response As Integer
Response = MessageBox.Show("Do you really want to exit?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Response = vbYes Then
Me.Close()
End If
End Sub
you can use threading put button1 code in a function and use the thread .
you can refer to this example
'Thread created to handle the Background process for start_function
Dim t As System.Threading.Thread
Private Sub start_function()
While True
'your code here for Eg:
Dim i As Integer
i = i + 1
End While
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
t = New System.Threading.Thread(AddressOf Me.start_function)
t.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
t.Abort()
End Sub
Drag a backgroundworker component onto your form.
Imports System.ComponentModel
Public Class Form1
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Me.Text = "Busy Doing Work"
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
Me.Text = "Asking to Cancel"
BackgroundWorker1.CancelAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
While Not BackgroundWorker1.CancellationPending
System.Threading.Thread.Sleep(1000)
End While
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Me.Text = "Cancelled"
End Sub
End Class

Timer acting weird

Hi guys I have another problem which is all about the timer..the form that I would want to show for 3 seconds is only showing for about half a second. Please help me thanks in advance
Main form code:
Private Sub submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles submit.Click
'question 1
If Label1.Text = "Who invented the airplane?" And TextBox1.Text = "third" Then
Label2.Text = (Label2.Text) + 1
correctmsg.Show()
correctmsg.Hide()
Label1.Text = "Who invented the telephone?"
Return 'Don't do any more checks this time around
ElseIf Label1.Text = "Who invented the airplane?" Then
'Reason ElseIf (In case the question was 'who invented the telephone' then the first errormessage should not not be shown)
wrongmsg.Show()
Return
End If
Splash form code:
Public Class correctmsg
Private Sub correctmsg_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim mysplash As New correctmsg
mysplash.Timer1.Interval = 3000
End Sub
End Class
Something like this:
Public Class correctmsg
' correctmsg == mysplash ??? so this is a form??
Private Sub correctmsg_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 3000 ' could be a timer here
End Sub
Public Sub ShowMsg
Timer1.Enabled = true
me.Visible = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
Me.Visible = False
End Sub
End Class
I am just showing the form by making it visible. No need to make a new form. When the timer expires, hide the form and disable the timer. To use it:
correctmsg.ShowMsg
' this was hiding the form as soon as it shows:
'correctmsg.Hide()

Close form after set time

The code below allows me to fade in and out when it opens and closes, which is what I want. However, I would like my form to remain open for 10 seconds before the fading starts. I am struggling to get that part done.
Here is what I have so far:
Public Class frmDefinitions
Private Sub Button1_Click(sender As Object, e As EventArgs) _
Handles Button1.Click
tmr_out.Enabled = True
End Sub
Private Sub frmDefinitions_Load(sender As Object, e As EventArgs) _
Handles MyBase.Load
Me.Opacity = 100
tmr_in.Enabled = True
End Sub
Private Sub tmr_in_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles tmr_in.Tick
Me.Opacity += 0.05
If Me.Opacity = 1 Then
tmr_in.Enabled = False
End If
End Sub
Private Sub tmr_out_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles tmr_out.Tick
Me.Opacity -= 0.05
If Me.Opacity = 0 Then
tmr_out.Enabled = False
Me.Close()
End If
End Sub
End Class
You will need to setup a third Timer to delay the start of your tmr_out Timer. I would trigger the delay as soon as your tmr_in is disabled. You should then get your 10 second delay before you start your fade out. You could also try to use the Form's Shown event to start the Delay but you would need to adjust the 10 seconds to accommodate the fade in delay.
Public Class Form1
Dim tmrDelay As New Timer()
Public Sub New()
InitializeComponent()
tmrDelay.Interval = 10000
AddHandler tmrDelay.Tick, AddressOf tmrDelay_Tick
End Sub
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Me.Opacity = 0
tmr_in.Enabled = True
End Sub
Private Sub tmrDelay_Tick(sender As System.Object, e As System.EventArgs)
tmrDelay.Stop()
tmr_out.Start()
End Sub
Private Sub tmr_in_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_in.Tick
Me.Opacity += 0.05
If Me.Opacity = 1 Then
tmr_in.Enabled = False
tmrDelay.Start() 'Start Your 10 second delay here.
End If
End Sub
Private Sub tmr_out_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_out.Tick
Me.Opacity -= 0.05
If Me.Opacity = 0 Then
tmr_out.Enabled = False
Me.Close()
End If
End Sub
End Class