给你个udp多播例子,广播不是很清楚,呵呵
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Inherits System.Windows.Forms.Form
Dim port As String
Dim ipadd As String
Dim ipend As IPEndPoint
Dim sendudp As New UdpClient()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim ipadress As IPAddress
ipadress = IPAddress.Parse(TextBox1.Text)
'ipend = New IPEndPoint(ipadress, sendport)
Try
sendudp.JoinMulticastGroup(ipadress)
MessageBox.Show("启动完成!")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
PrintDocument1.Print()
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim?test?As?String
Public?Sub?New(ByVal?_test?As?String)
test?=?_test
End?Sub
End?Class
但在 VB.NET 中,没必要这么麻烦,只需要声明为 Public,即可直接方法,如:
Public?test?As?String
就
是
这
个
了
!
Hand类的代码:
Public MustInherit Class Hand
Protected gp As GraphicsPath = New GraphicsPath()
Protected gpBase As GraphicsPath = Nothing
'构造器,得到窗体中心位置
Public Sub New(ByVal theForm As Form1)
MustOverride Sub Transform(ByVal d As DateTime)
'绘制指针路径
Overridable Sub Draw(ByVal g As Graphics)
g.DrawPath(aPen, gp)
g.FillPath(Brushes.Black, gp)
aPen.Dispose()
'使用矩阵实现路径(gp)的旋转
Public Sub Rotate(ByVal angle As Double)
gp = CType(gpBase.Clone(), GraphicsPath)
Dim mTransform As Matrix = New Matrix()
mTransform.RotateAt(CType(angle,Single),NewPointF(midX,midY))
gp.Transform(mTransform)
End Class
为了节省篇幅,上面的代码省略了引入命名空间的语句.
下面是分针(MinuteHand)类的定义:
Public Class MinuteHand
Inherits Hand
'构造器,生成绘制分针的路径(gp)
Public Sub New(ByVal myForm As Form1)
MyBase.New(myForm)
gpBase = CType(gp.Clone(), GraphicsPath)
'Transform方法取得系统当前时间,并旋转时钟指针.
Public Overrides Sub Transform(ByVal d As DateTime)
Rotate(angle)
对所有的指针旋转的方法都是相同的,所以呢在基类中实现.由于时针和秒针的实现与分针相似,所不同者,只在于构造器中绘制的指针路径不同和Transform方法中转动的角度不同,今天这一节就不在赘述了.
Public Sub Draw(ByVal g As Graphics)
DrawClockFace(g)
DrawImage(g)
DrawNumbers(g)
DrawPin(g)
下面是ClockFace类的属性:
Private ClockRectangle As Rectangle
Private midPoint As Point
Private ClockImage As Bitmap
DrawClockFace方法用来画时钟表面:
Private Sub DrawClockFace(ByVal g As Graphics)
然后用Graphics对象的DrawImage方法画出米老鼠的图片:
Private Sub DrawImage(ByVal g As Graphics)
Dim nWidth As Integer = ClockImage.Width
Dim nHeight As Integer = ClockImage.Height
g.DrawImage(ClockImage, destRect)
数字在时钟上的位置是用sin和cos函数计算的:
Private Sub DrawNumbers(ByVal g As Graphics)
Dim count As Integer = 1
Dim a As Double
g.DrawString(Convert.ToString(count), ClockFont, Brushes.Black, CType(x, Single), CType(y, Single), New StringFormat())
count += 1
Next
最后是窗体文件(Form1.vb):
Private MyMinuteHand As MinuteHand
Private MyHourHand As HourHand
Private MySecondHand As SecondHand
Private TheClockFace As ClockFace
Private FirstTick As Boolean = False
'在窗体的OnPaint事件中取得Graphics对象
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
If (FirstTick = False) Then Exit Sub
Dim g As Graphics = e.Graphics
TheClockFace.Draw(g)
MyHourHand.Draw(g)
MyMinuteHand.Draw(g)
MySecondHand.Draw(g)
TheClockFace.DrawPin(g)
'计时器事件
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
MySecondHand.Transform(DateTime.Now)
MyHourHand.Transform(DateTime.Now)
MyMinuteHand.Transform(DateTime.Now)
FirstTick = True
Invalidate()
vb.net中如何结束一个线程
一般而言,如果您想终止一个线程,您可以使用System.Threading.Thread类的Abort方法. 例如:
Dim worker As ThreadStart = New ThreadStart(AddressOf workerthreadmethod)
Dim t As Thread = New Thread(worker)
t.Start()
MessageBox.Show("Wait for a while for the thread to start.")
MessageBox.Show(t.ThreadState.ToString())
t.Abort()
t.Join()
当然,在调用Abort方法后,线程并不是立刻终止,要等线程的所有finally快中的代码完成后才会完全终止. 所以在主线程中可以用Join方法来同步,当线程还未完全终止时,t.Join()将处于等待,直到t线程完全结束后再继续执行后面的语句.
Abort方法是会导致线程跳出一个异常错误的,你需要在代码中捕获该异常.下面是一个比较完整的VB.NET线程例子:
Imports System
Imports System.Threading
Public Class MyTestApp
Public Shared Sub Main()
Dim t As New Thread(New ThreadStart(AddressOf MyThreadMethod))
'Start the thread
MsgBox("Are you ready to kill the thread?")
'Kill the child thread and this will cause the thread raise an exception
' Wait for the thread to exit
MsgBox("The secondary thread has terminated.")
Shared Sub MyThreadMethod()
Dim i As Integer
Do While True
Thread.CurrentThread.Sleep(1000)
Console.WriteLine("This is the secondary thread running.")
Loop
Catch e As ThreadAbortException
MsgBox("This thread is going to be terminated by the Abort method in the Main function")
Thread.Abort()方法用来永久销毁一个线程,而且将抛出ThreadAbortException异常.使终结的线程可以捕获到异常但是很难控制恢复,仅有的办法是调用Thread.ResetAbort()来取消刚才的调用,而且只有当这个异常是由于被调用线程引起的异常.所以呢,A线程可以正确的使用Thread.Abort()方法作用于B线程,但是B线程却不能调用Thread.ResetAbort()来取消Thread.Abort()操作.
以上就是土嘎嘎小编大虾米为大家整理的相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!