圖畫就是上圖轉來轉去的這一張ㄚ^^
我和我的好朋友、寶貝學生互動及學習使用Blogger的地方。
Imports System
Imports System.Threading
Module Module1
'修改此參數可改變等候時間數, 下面例子為5秒
Dim waitN As New TimeSpan(0, 0, 5)
Sub Main()
Dim newThread As New Thread(AddressOf Waiting)
newThread.Start()
Console.WriteLine("開始...")
If newThread.Join(TimeSpan.op_Addition(waitN, waitN)) Then
Console.WriteLine("5秒時間到!")
End If
Console.ReadKey()
End Sub
Sub Waiting()
Thread.Sleep(waitN)
End Sub
End Module
如何搭配 Sleep 方法來使用 TimeSpan 值的作法。
Visual Basic
Imports System
Imports System.Threading
Public Class Test
Shared waitTime As New TimeSpan(0, 0, 1)
<MTAThread> _
Shared Sub Main()
Dim newThread As New Thread(AddressOf Work)
newThread.Start()
If newThread.Join( _
TimeSpan.op_Addition(waitTime, waitTime)) Then
Console.WriteLine("New thread terminated.")
Else
Console.WriteLine("Join timed out.")
End If
End Sub
Shared Sub Work()
Thread.Sleep(waitTime)
End Sub
End Class
'Dictionary Sample 2
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim str1 = ""
Dim dict1 As New Dictionary(Of String, String)
dict1.Add("A", "優")
dict1.Add("B", "佳")
dict1.Add("C", "可")
dict1.Add("D", "差")
str1 = str1 & vbNewLine
'KeyValuePair
For Each kvp As KeyValuePair(Of String, String) In dict1
str1 = str1 & kvp.Key & Space(3) & kvp.Value & vbNewLine
Next kvp
'assign value
dict1("D") = "劣"
'ValueCollection
Dim valueColl As Dictionary(Of String, String).ValueCollection = dict1.Values
str1 = str1 & vbNewLine
For Each s As String In valueColl
str1 = str1 & "Value = " & s & vbNewLine
Next s
'KeyCollection
Dim keyColl As Dictionary(Of String, String).KeyCollection = dict1.Keys
str1 = str1 & vbNewLine
For Each s As String In keyColl
str1 = str1 & "Key = " & s & vbNewLine
Next s
'Query
str1 = str1 & vbNewLine
str1 = str1 & dict1("C") & vbNewLine
'Remove
str1 = str1 & vbNewLine & "Remove(""C"")"
dict1.Remove("C")
'ContainsKey
str1 = str1 & vbNewLine
If Not dict1.ContainsKey("C") Then
str1 = str1 & "Key ""C"" is not found."
End If
MsgBox(str1)
End Sub
End Class
'Dictionary Sample
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim str1 = ""
Dim dict1 As New Dictionary(Of Integer, String)
dict1.Add(0, "優")
dict1.Add(1, "佳")
dict1.Add(2, "可")
dict1.Add(3, "差")
str1 = str1 & vbNewLine
'KeyValuePair
For Each kvp As KeyValuePair(Of Integer, String) In dict1
str1 = str1 & kvp.Key & Space(3) & kvp.Value & vbNewLine
Next kvp
'assign value
dict1(3) = "劣"
'ValueCollection
Dim valueColl As Dictionary(Of Integer, String).ValueCollection = dict1.Values
str1 = str1 & vbNewLine
For Each s As String In valueColl
str1 = str1 & "Value = " & s & vbNewLine
Next s
'KeyCollection
Dim keyColl As Dictionary(Of Integer, String).KeyCollection = dict1.Keys
str1 = str1 & vbNewLine
For Each s As String In keyColl
str1 = str1 & "Key = " & s & vbNewLine
Next s
'Query
str1 = str1 & vbNewLine
str1 = str1 & dict1(2) & vbNewLine
'Remove
str1 = str1 & vbNewLine & "Remove(2)"
dict1.Remove(2)
'ContainsKey
str1 = str1 & vbNewLine
If Not dict1.ContainsKey(2) Then
str1 = str1 & "Key ""2"" is not found."
End If
MsgBox(str1)
End Sub
End Class
Dim days = New Dictionary(Of Integer, String)
days.Add(0, "Sunday")
days.Add(1, "Monday")