送你一個(gè)延遲函數(shù)單位毫秒
壽陽(yáng)網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),壽陽(yáng)網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為壽陽(yáng)超過(guò)千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的壽陽(yáng)做網(wǎng)站的公司定做!
Public Sub delay(ByRef Interval As Double)
On Error Resume Next
Dim time As DateTime = DateTime.Now
Dim Span As Double = Interval * 10000000 '因?yàn)闀r(shí)間是以100納秒為單位。
While ((DateTime.Now.Ticks - time.Ticks) Span)
Application.DoEvents()
End While
End Sub
試試這個(gè)怎么樣,添加在子進(jìn)程里面,就加在你批量傳輸代碼里的每一個(gè)傳輸后面,也就是大批量中的每傳輸一個(gè)數(shù)據(jù)就暫停一下,而不是每一個(gè)大批量才暫停一下
System.Threading.Thread.Sleep(10) '讓它走慢一點(diǎn)
首先在窗體上畫兩個(gè)控件:TextBox1和Button1
TextBox1用來(lái)輸入需要計(jì)算那個(gè)數(shù)的階乘
雙擊Button1進(jìn)入輸入代碼,代碼如下
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim Factorial As Integer = 1 '定義一個(gè)變量用來(lái)記錄階乘的結(jié)果
Dim count As Integer '定義一個(gè)變量用來(lái)記錄需要計(jì)算那個(gè)數(shù)的階乘
Dim i As Integer = 1 '定義一個(gè)數(shù)用來(lái)循環(huán)
count = Int(Val(Me.TextBox1.Text)) '把TextBox1的值賦值給count
Do While i = count '下面開始計(jì)算階乘
Factorial = Factorial * i '計(jì)算階乘
i += 1 '自增1
Loop
MessageBox.Show(Int(Val(Me.TextBox1.Text)) "的階乘是:" Factorial, "完成", MessageBoxButtons.OK) '彈出計(jì)算結(jié)果
Catch ex As Exception '出錯(cuò)提示
MessageBox.Show(Err.Description, "出錯(cuò)了", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
vb.net中如何結(jié)束一個(gè)線程
一般而言,如果您想終止一個(gè)線程,您可以使用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())
當(dāng)然,在調(diào)用Abort方法后,線程并不是立刻終止,要等線程的所有finally快中的代碼完成后才會(huì)完全終止. 所以在主線程中可以用Join方法來(lái)同步,當(dāng)線程還未完全終止時(shí),t.Join()將處于等待,直到t線程完全結(jié)束后再繼續(xù)執(zhí)行后面的語(yǔ)句。
Abort方法是會(huì)導(dǎo)致線程跳出一個(gè)異常錯(cuò)誤的,你需要在代碼中捕獲該異常。下面是一個(gè)比較完整的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()方法用來(lái)永久銷毀一個(gè)線程,而且將拋出ThreadAbortException異常。使終結(jié)的線程可以捕獲到異常但是很難控制恢復(fù),僅有的辦法是調(diào)用Thread.ResetAbort()來(lái)取消剛才的調(diào)用,而且只有當(dāng)這個(gè)異常是由于被調(diào)用線程引起的異常。因此,A線程可以正確的使用Thread.Abort()方法作用于B線程,但是B線程卻不能調(diào)用Thread.ResetAbort()來(lái)取消Thread.Abort()操作。
while為條件循環(huán),for為計(jì)數(shù)循環(huán)。一般情況下,WHILE可替代FOR,反之則不然。