VB.Net Tutorial/Thread/Thread Create — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:56, 26 мая 2010
Create Threads
Module Module1
Sub TaskA()
Dim I As Integer
For I = 0 To 250
System.Console.Write("A")
Next
End Sub
Sub TaskB()
Dim I As Integer
For I = 0 To 250
System.Console.Write("B")
Next
End Sub
Sub TaskC()
Dim I As Integer
For I = 0 To 250
System.Console.Write("C")
Next
End Sub
Sub Main()
Dim A As System.Threading.Thread = New Threading.Thread(AddressOf TaskA)
Dim B As System.Threading.Thread = New Threading.Thread(AddressOf TaskB)
Dim C As System.Threading.Thread = New Threading.Thread(AddressOf TaskC)
A.Start()
B.Start()
C.Start()
End Sub
End Module
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBACCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCAAAAAAAAAAAAAAAAAAAAA"
Create two threads
Imports System.Threading
public class Test
public Shared Sub Main
Dim thread1 As New Thread(AddressOf method1)
Dim thread2 As New Thread(AddressOf method2)
thread1.Start()
thread2.Start()
End Sub
Private Shared Sub method1()
Dim i As Integer
For i = 1 To 4
Console.WriteLine("Method1: " & i)
Thread.CurrentThread.Sleep(250)
Next
End Sub
Private Shared Sub method2()
Dim i As Integer
For i = 1 To 4
Console.WriteLine("Method2: " & i)
Thread.CurrentThread.Sleep(250)
Next
End Sub
End class
Method1: 1 Method2: 1 Method1: 2 Method2: 2 Method1: 3 Method2: 3 Method1: 4 Method2: 4
Read a file in a thread
Imports System.IO
Imports System.Text
Imports System.Threading
Public Class Tester
Public Shared Sub Main
Dim readThread As Thread
readThread = New Thread(AddressOf ReadFile)
readThread.Start()
End Sub
Private Shared Sub ReadFile()
Dim myFileStream As FileStream
Dim intByte As Integer
Dim bteRead(128) As Byte
Dim intloop As Integer = 0
Try
myFileStream = New FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Read)
Do
intByte = myFileStream.Read(bteRead, 0, 128)
Console.WriteLine(Encoding.ASCII.GetString(bteRead))
Thread.Sleep(500)
intloop += 1
Loop While Not (intByte = 0 Or intByte < 128)
myFileStream.Close()
Catch ex As IOException
Console.WriteLine(ex.Message)
End Try
End Sub
End Class