首先引入那個(gè)項(xiàng)目的命名空間,在代碼最上方用Imports語(yǔ)句,比如
專注于為中小企業(yè)提供網(wǎng)站建設(shè)、成都做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)閔行免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
Imports Project1
然后在需要用的地方寫
Dim aform As New MyForm1()
然后通過aform對(duì)象的Show方法(彈出普通窗口)或ShowDialog(彈出窗口時(shí)后方窗口無響應(yīng))方法來顯示窗口,最后調(diào)用Dispose方法釋放對(duì)象內(nèi)存。
如果是程序沒關(guān)閉的情況下,就把全局變量定義在模塊里,如果程序關(guān)閉再啟動(dòng)的話,就只能存放在文件里了
Imports System.Threading
Public Class Form1
Dim t1, t2 As Thread
Dim tt1
Private Delegate Sub voiddelegate(ByVal i As Integer)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Form2.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
t1 = New Thread(AddressOf test)
tt1 = 1
t1.Start()
tt1 = 2
t2 = New Thread(AddressOf test)
t2.Start()
' Timer1.Start()
End Sub
Private Sub updateui(ByVal i As Integer)
Form2.Label1.Text = i.ToString
Form2.Label2.Text = i.ToString
End Sub
'把循環(huán)放到線程中運(yùn)行會(huì)好一些,其實(shí)主要事務(wù)是對(duì)窗體控件的操作時(shí)多線程意義不大,因?yàn)樵L問窗體的代碼依然要在窗體線程中運(yùn)行。
Private Sub test()
Dim d As voiddelegate = AddressOf updateui
For i = 0 To 9000
Me.Invoke(d, i)
Next
End Sub
'如果放在計(jì)時(shí)器中運(yùn)行效果好的多
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static i As Integer = 0
i += 1
If i 9000 Then Timer1.Stop() : i = 0
Form2.Label1.Text = i.ToString
Form2.Label2.Text = i.ToString
End Sub
End Class
'如果在另一線程中用Application.run 打開窗體確實(shí)能行,不過窗體間相互操作不起作用,這個(gè)尚待研究中。