网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

关于vb.netmutex的信息

vb.net中如何结束一个线程

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()

MessageBox.Show(t.ThreadState.ToString())

t.Join()

MessageBox.Show(t.ThreadState.ToString())

当然,在调用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

t.Start()

MsgBox("Are you ready to kill the thread?")

'Kill the child thread and this will cause the thread raise an exception

t.Abort()

' Wait for the thread to exit

t.Join()

MsgBox("The secondary thread has terminated.")

End Sub

Shared Sub MyThreadMethod()

Dim i As Integer

Try

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")

End Try

End Sub

End Class

Thread.Abort()方法用来永久销毁一个线程,而且将抛出ThreadAbortException异常。使终结的线程可以捕获到异常但是很难控制恢复,仅有的办法是调用Thread.ResetAbort()来取消刚才的调用,而且只有当这个异常是由于被调用线程引起的异常。因此,A线程可以正确的使用Thread.Abort()方法作用于B线程,但是B线程却不能调用Thread.ResetAbort()来取消Thread.Abort()操作。

vb net只运行一个程序

方法一:通过Diagnostics.Process.GetProcessesByName函数来检测程序是否已经启动

Imports System.Windows.Forms

Module Module1

Sub Main()

'检测多重启动

If Diagnostics.Process.GetProcessesByName( _

Diagnostics.Process.GetCurrentProcess.ProcessName).Length 1 Then

MessageBox.Show("已经一个实例的本程序正在运行。")

Return

End If

Application.Run(New Form())

End Sub

End Module

不过这个方法有个缺点,如果用户改了下exe的名字就检测不到了,所以更好的方法如下

方法2:使用Mutex

Imports System.Windows.Forms

Module Module1

Sub Main()

Dim createdNew As Boolean

' 创建mutex

Dim mutex As System.Threading.Mutex = _

New System.Threading.Mutex(True, "YourAppName", createdNew)

If createdNew = False Then

MessageBox.Show("已经一个实例的本程序正在运行。")

Return

End If

Application.Run(New Form())

' 释放mutex

mutex.ReleaseMutex()

End Sub

End Module

VB.NET 多重启动的问题

VB2008里的设置:

项目属性-应用程序-生成单个实例应用程序


分享文章:关于vb.netmutex的信息
分享路径:http://bjjierui.cn/article/doiheep.html

其他资讯