D1. Calcolare il tempo di esecuzione di un’applicazione
Con TimeSpan
Inserire il contenuto del Button1 all’inizio dell’app da controllare e il contenuto del Button2 alla fine.
D2. Impostare l’orario di esecuuzione di un’attività
Inserire in Form1: 5 Label, 1 Button, 2 NumericUpDown e un Timer.
Con StopWatch
Nell’esempio il programma da misurare è il ciclo For…Next
D1. Calculate the execution
time implementation of an
application
In the example the program to
measure is the cycle For...Next
Time & Timer
2. Setting the execution time
of an activity
Add in Form1: 5 Label, 1 Button, 2
NumericUpDown and a Timer.
Public Class Form1
Dim ora As Integer
Dim minuto As Integer
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim currhour As Integer
Dim currminute As Integer
Dim time As Date = Date.Now
currhour = time.Hour
currminute = time.Minute
Label3.Text = "ora corrente"
Label4.Text = currhour.ToString & " : " & currminute.ToString & "'"
If currhour = ora AndAlso currminute = minuto Then
Label5.Text = "E' giunta l'ora"
Timer1.Stop()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Start()
ora = NumericUpDown1.Value
minuto = NumericUpDown2.Value
End Sub
End Class
Public Class Form1
Dim stopWatch As New Stopwatch()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label1.Text = ""
Label2.Text = ""
stopWatch.Reset() 'Riporta a zero il tempo trascorso
stopWatch.Start()
For i = 0 To 10000
Label2.Text = i
Application.DoEvents()
Next
stopWatch.Stop()
Dim ts As TimeSpan = stopWatch.Elapsed
Dim elapsedTime As String = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours,
ts.Minutes, ts.Seconds, ts.Milliseconds / 10)
Label1.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10)
'Label1.Text = String.Format("{0:00}:{1:00}", ts.Minutes, ts.Seconds) 'Mostra solo i minuti e i secondi
End Sub
End Class
Dim tp1 As String
Dim ts1, ts2, intervallo As New TimeSpan
Dim tempo As String
Label1.Text = ("00:00")
Button2.Enabled = True
ts1 = New TimeSpan(Now.Ticks)
‘Qui inserire l’attività di cui volete misusrare il tempo di esecuzione
‘Here enter the activity you want to measure the execution time for
'Acquisisce il tempo finale e fa la differenza fra i due tempi
ts2 = New TimeSpan(Now.Ticks)
intervallo = ts2 - ts1
tempo = intervallo.ToString.Substring(3, 5)
Label1.Text = tempo
Button2.Enabled = False
D3. Crea pausa di xx secondi
Inserire in Form1: 2 Label, 4 Button, 1 TextBox
Nota: Utilizzando Thread.Sleep l'applicazione rimane congelata fino alla fine della pausa
----1° Esempio
Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Clear()
Application.DoEvents()
wait(5)
End Sub
Private Sub wait(ByVal pausa As Integer)
For i As Integer = 0 To pausa * 10
Threading.Thread.Sleep(100)
TextBox1.Text = "Pausa 5 sec."
Next
End Sub
------2° Esempio
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox1.Clear()
Application.DoEvents()
wait2()
End Sub
Private Sub wait2()
Dim intervallo As TimeSpan = New TimeSpan(0, 0, 2)
For i As Integer = 0 To 5 - 1
Threading.Thread.Sleep(intervallo)
TextBox1.AppendText("pausa 2 sec." & vbCr)
Next
End Sub
------3° Esempio
'Fa una pausa di 4 secondi e indica il tempo trascorso
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim stopWatch As Stopwatch = New Stopwatch()
Dim pausa As Integer = 4
stopWatch.Start()
Threading.Thread.Sleep(pausa * 1000)
stopWatch.[Stop]()
Dim ts As TimeSpan = stopWatch.Elapsed
Label1.Text = ts.ToString.Substring(0, 8)
End Sub
-------4° Esempio
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Wait3(6)
End Sub
'Questa sub attende per il tempo richiesto senza congelare l'applicazione nella quale è inserita.
Inserendo BreakCondition nel loop è possibile interromperlo.
Private Sub Wait3(ByVal Seconds As Double, Optional ByRef BreakCondition As Boolean = False)
Dim attendi As Date
attendi = Now.AddSeconds(Seconds)
Do Until Now > attendi
Application.DoEvents()
Loop
Label2.Text = ("pausa " & Seconds & " sec.")
End Sub
3. Create xx seconds pauses
Add in Form1: 2 Label, 4 Button, 1
TextBox
With Thread.Sleep, the application
remains frozen until the end of the
pause.
D4. Contasecondi
Esempio con TimeSpan
Inserire in Form1: 3 Button, 1 Label e 1 Timer
This sub waits for the required time
without freezing the application in
which it is inserted
4. Timer
Add in Form1: 1 Label, 3 Button, 1
Timer
Example with TimeSpan
Dim tspn As New TimeSpan()
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
tspn = tspn.Add(New TimeSpan(0, 0, 1))
Label1.Text = String.Format("{0}h : {1}m : {2}s", tspn.Hours, tspn.Minutes, tspn.Seconds)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Timer1.Stop()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
tspn = New TimeSpan(0, 0, 0)
Label1.Text = "0h : 0m : 0s"
End Sub
Esempio con Elapsed.Minute
Inserire in Form1: 3 Button, 2 Label e 1 Timer
Dim conteggio As New System.Diagnostics.Stopwatch()
Dim parziale As Boolean = True
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Start()
conteggio.Start()
Label2.Visible = False
End Sub
'Intertempo-First step
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
parziale = False
Label2.Visible = True
End Sub
'Tempo Finale- Final time
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Timer1.Stop()
conteggio.Stop()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If parziale = True Then
Label1.Text = conteggio.Elapsed.Minutes.ToString & " : " & conteggio.Elapsed.Seconds.ToString
End If
Label2.Text = conteggio.Elapsed.Minutes.ToString & " : " & conteggio.Elapsed.Seconds.ToString
End Sub
Example with Elapsed.Minute
Insert in Form1: 3 Button, 2 Label and
1 Timer
Folders
Backup
è
un’efficiente
applicazione
per
il
salvataggio
di
cartelle
o
intere partizioni*.
Offre
quanto
serve
per
garantire
una
corretta
gestione
dei
backup
con
una
notevole
semplicità
d’uso
e
un’interfaccia
che
permette
un’impostazione
alla
portata
dei
meno
esperti
e
che
fornisce
subito
tutte
le
informazioni
necessarie
per
avere
una
chiara
visione dello stato dei propri backups.
Folders
Backup
is
an
efficient
application
for saving folders or entire partitions*.
It
offers
everything
you
need
to
ensure
proper
backup
management
with
a
remarkable
ease
of
use
and
an
interface
that
allows
an
easy
and
proper
setting
and
provides
all
the
information
you
need
to
have
a clear view of the status of your backups.
Un esempio: usare TimeSpan per calcolare
la durata di un viaggio
An example: using TimeSpan to calculate
the duration of a trip
Dim Partenza As DateTime = New
DateTime(2020, 5, 10, 18, 30, 0)
Dim Arrivo As DateTime = New
DateTime(2020, 5, 12, 20, 45, 10)
Dim DurataViaggio As TimeSpan =
Arrivo.Subtract(Partenza)
Label1.Text = (DurataViaggio.Days & "
Giorni, " & DurataViaggio.Hours & " Ore, " &
DurataViaggio.Minutes & " Minuti")
Risulta
Result:
TimeSpan calcola un intervallo di tempo in
giorni, ore, minuti, secondi e frazioni di
secondo indipendentemente dalla data nel
qual caso occorre utilizzare la struttura
DateTime
TimeSpan calculates a time interval in days,
hours, minutes, seconds and fractions of a
second regardless of the date in which case
the DateTime structure must be used.
D5. Giorni fra due date
Inserire in Form1: 1 Button, 6 Label e 2 DateTimePicker
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim date1 As Date = Date.Now
Dim date2 As Date '= date1.AddDays(365.0#)
Dim dateOne = DateTimePicker1.Value
Dim dateTwo = DateTimePicker2.Value
Dim diff As TimeSpan = dateTwo.Subtract(dateOne)
Dim years As Double = diff.TotalDays / 365
Label11.Text = years.ToString("0.00")
Label12.Text = (years * 12).ToString("0.00")
Label13.Text = diff.TotalDays.ToString("0.00")
End Sub
5. Days between two dates
Add in Form1: 1 Button, 6 Label and
2 DateTimePicker
D6. Calcola nuova data
Inserire in Form1: 1 Button, 10 Label e 1 TextBox
Calcola una nuova data sulla base dei giorni aggiunti o tolti
D6. Calculate new date
Add in Form1: 1 Button, 10 Label e 1
TextBox
Calculate a new date based on days
added or removed
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = Today
TextBox1.Text = 100.2
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim date1 As Date = Date.Now
Dim date2 As Date '= date1.AddDays(365.0#)
date2 = date1.AddDays(TextBox1.Text)
Label4.Text = (date2.ToString("dd/MM/yyyy"))
Dim diff = date2 - date1 '
Dim giorni As Double = diff.TotalDays
Label7.Text = diff.ToString
Label5.Text = date2.ToString.Substring(11, 8)
Label9.Text = giorni
End Sub
D7. Converte numero e stringa in data
Imports System.Globalization as Namespace
D7. Convert number and
string to date
'Converte numero in data/Converte numero in data
Dim anno As Integer = 2018
Dim mese As Integer = 12
Dim giorno As Integer = 1
Dim ora As Integer = 18
Dim minuti As Integer = 25
Dim secondi As Integer = 30
Dim date1 As New Date(anno, mese, giorno, ora, minuti, secondi)
Dim date2 As Date = #12/13/2017# 'Mese/Giorno/Anno
Label1.Text = date1
Label2.Text = date2
'Converte stringa in data/Convert string to date
Dim dateString As String = "5/1/2008 8:30:52 AM"
Dim date0 As Date = Date.Parse(dateString, CultureInfo.InvariantCulture) 'Converte il testo in data
Label3.Text = date0
'Converte numero in mese/Convert number in month
Label6.Text = "11"
Dim intero As Integer = Label6.Text
Dim nameAbb As String = MonthName(intero)
Dim name As String = MonthName(intero, True)
Label4.Text = name
Label5.Text = nameAbb