vb.net多线程同步调用实例
2016-02-24 11:33
120 查看
以前一直感觉多线程比较难懂,最近由于需要,没办法必须克服,这不看了几遍书,终于理解差不多了,先把最开始的一个实例弄上来吧。
Imports
System.Threading '看名字就知道,这个类干什么用的,多线程应用程序都要用这个类
Public Delegate Function
BinaryOp(ByVal x As Integer, ByVal y As Integer) As Integer
Module Module1
Sub Main()
Console.WriteLine("***** Synch Delegate Review *****")
' Print
out the ID of the executing thread.
Console.WriteLine("Main() invoked on thread {0}.",
Thread.CurrentThread.ManagedThreadId)
' Invoke
Add() in a synchronous manner.
Dim b As
New BinaryOp(AddressOf Add)
' Could
also write b.Invoke(10, 10); 注意区别
同步方式是invoke,已不是beginInvoke
Dim answer
As Integer = b(10, 10)
' These
lines will not execute until
' the
Add() method has completed.
Console.WriteLine("Doing more work in Main()!")
Console.WriteLine("10 + 10 is {0}.", answer)
Console.ReadLine()
End Sub
Private Function Add(ByVal x As Integer, ByVal y
As Integer) As Integer
' Print
out the ID of the executing thread.
Console.WriteLine("Add() invoked on thread {0}.",
Thread.CurrentThread.ManagedThreadId)
' Pause to
simulate a lengthy operation.
Thread.Sleep(5000)
Return x +
y
End Function
End Module
不知道大家怎么理解这个同步和异步。反正这个不大好理解,我觉得主要就看是否有阻塞或者尽可能小的阻塞。特别是同步调用方式和同步调用线程的区别,特别是很多翻译过来的东西,最好还是看原文和实例。
---------------------------一以下异步调用代码----------------------------------------------------
Imports System.Threading
'看名字就知道,这个类干什么用的,多线程应用程序都要用这个类
Public Delegate Function
BinaryOp(ByVal x As Integer, ByVal y As Integer) As Integer
Module Module1
Sub main()
Console.WriteLine("***** Async Delegate Invocation *****")
' Print
out the ID of the executing thread.
Console.WriteLine("Main() invoked on thread {0}.",
Thread.CurrentThread.ManagedThreadId)
Dim b As
New BinaryOp(AddressOf Add)
Dim iftAR
As IAsyncResult = b.BeginInvoke(10, 10, Nothing, Nothing)
' This
message will keep printing until
' the
Add() method is finished.
Do While
Not iftAR.IsCompleted
Console.WriteLine("Doing more
work in Main()!")
Thread.Sleep(1000)
Loop
' Now we
know the Add() method is complete.
Dim answer
As Integer = b.EndInvoke(iftAR)
Console.WriteLine("10 + 10 is {0}.", answer)
Console.ReadLine()
End Sub
Private Function Add(ByVal x As Integer, ByVal y
As Integer) As Integer
' Print
out the ID of the executing thread.
Console.WriteLine("Add() invoked on thread {0}.",
Thread.CurrentThread.ManagedThreadId)
' Pause to
simulate a lengthy operation.
Thread.Sleep(5000)
Return x +
y
End Function
End Module
Imports
System.Threading '看名字就知道,这个类干什么用的,多线程应用程序都要用这个类
Public Delegate Function
BinaryOp(ByVal x As Integer, ByVal y As Integer) As Integer
Module Module1
Sub Main()
Console.WriteLine("***** Synch Delegate Review *****")
out the ID of the executing thread.
Console.WriteLine("Main() invoked on thread {0}.",
Thread.CurrentThread.ManagedThreadId)
' Invoke
Add() in a synchronous manner.
Dim b As
New BinaryOp(AddressOf Add)
' Could
also write b.Invoke(10, 10); 注意区别
同步方式是invoke,已不是beginInvoke
Dim answer
As Integer = b(10, 10)
' These
lines will not execute until
' the
Add() method has completed.
Console.WriteLine("Doing more work in Main()!")
Console.WriteLine("10 + 10 is {0}.", answer)
Console.ReadLine()
End Sub
Private Function Add(ByVal x As Integer, ByVal y
As Integer) As Integer
out the ID of the executing thread.
Console.WriteLine("Add() invoked on thread {0}.",
Thread.CurrentThread.ManagedThreadId)
' Pause to
simulate a lengthy operation.
Thread.Sleep(5000)
Return x +
y
End Function
End Module
不知道大家怎么理解这个同步和异步。反正这个不大好理解,我觉得主要就看是否有阻塞或者尽可能小的阻塞。特别是同步调用方式和同步调用线程的区别,特别是很多翻译过来的东西,最好还是看原文和实例。
---------------------------一以下异步调用代码----------------------------------------------------
Imports System.Threading
'看名字就知道,这个类干什么用的,多线程应用程序都要用这个类
Public Delegate Function
BinaryOp(ByVal x As Integer, ByVal y As Integer) As Integer
Module Module1
Sub main()
Console.WriteLine("***** Async Delegate Invocation *****")
out the ID of the executing thread.
Console.WriteLine("Main() invoked on thread {0}.",
Thread.CurrentThread.ManagedThreadId)
Dim b As
New BinaryOp(AddressOf Add)
Dim iftAR
As IAsyncResult = b.BeginInvoke(10, 10, Nothing, Nothing)
' This
message will keep printing until
' the
Add() method is finished.
Do While
Not iftAR.IsCompleted
Console.WriteLine("Doing more
work in Main()!")
Thread.Sleep(1000)
Loop
' Now we
know the Add() method is complete.
Dim answer
As Integer = b.EndInvoke(iftAR)
Console.WriteLine("10 + 10 is {0}.", answer)
Console.ReadLine()
End Sub
Private Function Add(ByVal x As Integer, ByVal y
As Integer) As Integer
out the ID of the executing thread.
Console.WriteLine("Add() invoked on thread {0}.",
Thread.CurrentThread.ManagedThreadId)
' Pause to
simulate a lengthy operation.
Thread.Sleep(5000)
Return x +
y
End Function
End Module
相关文章推荐
- vb document.execCommand()语法格…
- vb.net批量查询已删除域名
- VB.net怎么最小化到系统托盘
- vb.net 判断是否按下某个按键
- vb截取任意两个特征字符之间的字符…
- vb.net webbrowser填充textarea
- vb.net 不会无响应的延时函数delay
- vb 读写.ini文件
- vb.net 数组去除重复
- vb webbrowser 保存网页源码的各种…
- VB6 XArrayDB | Xarray ReDim 用法
- 使用正则表达式vb从网址提取根域名
- Vb.net 分组控件学习 checkedlistb…
- vb.net 如何选择文件夹 不是文件
- vb.net timer 定时器
- vb2008弹出菜单位置
- Vb.net 获取IP地址
- vb 监听回车键
- telnet客户端源码 vb6
- .net vb socket 官网示例