资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

vb.net委托循环,vbnetfor循环

vb.net怎么无线循环编

一般多开线程写无限循环行为。

公司主营业务:网站设计制作、成都网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出同仁免费做网站回馈大家。

//定义线程

Dim th As New Threading.Thread(New Threading.ParameterizedThreadStart(AddressOf test))

th.Start("aaa")//开始线程,可以传参

//线程执行函数

Public Sub test(ob As Object)

While True

Console.WriteLine("线程正在运行中"  ob)

Threading.Thread.Sleep(1000)

End While

End Sub

vb.net do循环

你确信只循环一次吗?我看不像,在最后添加上一句msgbox jishu1,多半显示3。

是在第二块代码中有问题,你应该把chaxun4 = xlsheet.Range("l1").Value + 2分别放到两个条件句中初始chaxun4变量。

因为上一个Do Until atmbianhaop = xlsheet.Range("B" chaxun4).Value 循环退出后,下一个循环一开始就符合退出条件了,所以循环没有进行了。

vb.net的for each 循环问题

值传递和地址传递(引用)的而导致的问题。

在For Each 的时候

当对象是值对象的时候,等于获得到一个副本。

当对象是引用对象的时候,则是获得到一个指针。

而在For To 的时候

无论对象是什么,你都获得到这个集合指定位置的指针。

更详细的,可以请参考以下内容,或直接去官方的MSDN了解

关于值类型和引用类型:

如果数据类型在它自己的内存分配中存储数据,则该数据类型就是“值类型”。“引用类型”包含指向存储数据的其他内存位置的指针。

值类型

值类型包括:

所有数字数据类型

Boolean、Char 和 Date

所有结构,即使其成员是引用类型

枚举,因为其基础类型总是 SByte、Short、Integer、Long、Byte、UShort、UInteger

或 ULong

引用类型

引用类型包括:

String

所有数组,即使其元素是值类型

类类型,如 Form

委托

非类型的元素

以下编程元素未限定为类型,因为您无法将它们中的任何一个指定为声明元素的数据类型:

命名空间

模块

事件

属性和过程

变量、常数和字段

使用对象数据类型

可以将引用类型或值类型指派给 Object 数据类型的变量。Object

变量总是存储指向数据的指针,从不存储数据本身。然而,如果将值类型指派给 Object 变量,则 Object

变量将表现得像存储自己的数据一样。有关更多信息,请参见 Object 数据类型。

通过将 Object 变量传递给 Microsoft.VisualBasic

命名空间中 Information

类的 IsReference

方法,可以确定该变量是用作引用类型还是值类型。如果 Object 变量的内容表示引用类型,则 Information.IsReference 返回 True。

vb.net 中在模块(module)里如何实现委托

委托三个步骤

1、声明委托 用Delegate 声明一个委托 类型 参数要和 被委托的方法一样 例如 Delegate Function a(byval x as string) as string

2、实例化委托 dim t as new a(AddressOf Function Name)

3.通过 t(参数) 或者 t.Invoke(参数调用委托)

示例:

Module module1

Delegate Function a(ByVal x As Integer, ByVal y As Integer) As Integer '声明委托类型 委托可以使一个对象调用另一个对象的方法

Function sum(ByVal x As Integer, ByVal y As Integer) As Integer

Return (x + y)

End Function

Sub main()

Dim d As New a(AddressOf sum) '实例化委托

Dim s = 0

s = d.Invoke(1, 2) '执行委托

Console.WriteLine(s.ToString())

s = d(1, 2) '执行委托

Console.WriteLine(s.ToString())

MsgBox("")

End Sub

End Module

vb.net多线程,循环导致窗口界面假死的问题

同学。。你这里的写法其实根本没有用到多线程

原因很简单

你是定义了一个T的新线程,但是很可惜你在BeginDoSub这个独立的线程中又定义了一个deSomeSub的委托,并且直接用Me.Invoke把实际的执行任务提交给界面主线程运行了

所以你的图片的处理函数实际上是在主线程中运行的,新线程等于没用

正确的作法是,T定义为SomeSub这个函数为入口的线程,并启动它

在界面进度条更改的时候再使用Invoke来调用委托来进行界面更改。

自己再好好看看吧,时间问题我要走了。

vb.net中如何用事件和委托,会C#中的事件和委托,但不知VB.net中的语法,望给个简单的例子熟悉语法。

一委托:此示例演示如何将方法与委托关联然后通过委托调用该方法。

创建委托和匹配过程

创建一个名为 MySubDelegate 的委托。

Delegate Sub MySubDelegate(ByVal x As Integer)

声明一个类,该类包含与该委托具有相同签名的方法。

Class class1

Sub Sub1(ByVal x As Integer)

MsgBox("The value of x is: " CStr(x))

End Sub

End Class

定义一个方法,该方法创建该委托的实例并通过调用内置的 Invoke 方法调用与该委托关联的方法。

Protected Sub DelegateTest()

Dim c1 As New class1

' Create an instance of the delegate.

Dim msd As MySubDelegate = AddressOf c1.Sub1

' Call the method.

msd.Invoke(10)

End Sub

二、事件

下面的示例程序阐释如何在一个类中引发一个事件,然后在另一个类中处理该事件。AlarmClock 类定义公共事件 Alarm,并提供引发该事件的方法。AlarmEventArgs 类派生自 EventArgs,并定义 Alarm 事件特定的数据。WakeMeUp 类定义处理 Alarm 事件的 AlarmRang 方法。AlarmDriver 类一起使用类,将使用 WakeMeUp 的 AlarmRang 方法设置为处理 AlarmClock 的 Alarm 事件。

该示例程序使用事件和委托和引发事件中详细说明的概念。

示例

' EventSample.vb.

'

Option Explicit

Option Strict

Imports System

Imports System.ComponentModel

Imports Microsoft.VisualBasic

Namespace EventSample

' Class that contains the data for

' the alarm event. Derives from System.EventArgs.

'

Public Class AlarmEventArgs

Inherits EventArgs

Private _snoozePressed As Boolean

Private nrings As Integer

'Constructor.

'

Public Sub New(snoozePressed As Boolean, nrings As Integer)

Me._snoozePressed = snoozePressed

Me.nrings = nrings

End Sub

' The NumRings property returns the number of rings

' that the alarm clock has sounded when the alarm event

' is generated.

'

Public ReadOnly Property NumRings() As Integer

Get

Return nrings

End Get

End Property

' The SnoozePressed property indicates whether the snooze

' button is pressed on the alarm when the alarm event is generated.

'

Public ReadOnly Property SnoozePressed() As Boolean

Get

Return _snoozePressed

End Get

End Property

' The AlarmText property that contains the wake-up message.

'

Public ReadOnly Property AlarmText() As String

Get

If _snoozePressed Then

Return "Wake Up!!! Snooze time is over."

Else

Return "Wake Up!"

End If

End Get

End Property

End Class

' Delegate declaration.

'

Public Delegate Sub AlarmEventHandler(sender As Object, _

e As AlarmEventArgs)

' The Alarm class that raises the alarm event.

'

Public Class AlarmClock

Private _snoozePressed As Boolean = False

Private nrings As Integer = 0

Private stopFlag As Boolean = False

' The Stop property indicates whether the

' alarm should be turned off.

'

Public Property [Stop]() As Boolean

Get

Return stopFlag

End Get

Set

stopFlag = value

End Set

End Property

' The SnoozePressed property indicates whether the snooze

' button is pressed on the alarm when the alarm event is generated.

'

Public Property SnoozePressed() As Boolean

Get

Return _snoozePressed

End Get

Set

_snoozePressed = value

End Set

End Property

' The event member that is of type AlarmEventHandler.

'

Public Event Alarm As AlarmEventHandler

' The protected OnAlarm method raises the event by invoking

' the delegates. The sender is always this, the current instance

' of the class.

'

Protected Overridable Sub OnAlarm(e As AlarmEventArgs)

RaiseEvent Alarm(Me, e)

End Sub

' This alarm clock does not have

' a user interface.

' To simulate the alarm mechanism it has a loop

' that raises the alarm event at every iteration

' with a time delay of 300 milliseconds,

' if snooze is not pressed. If snooze is pressed,

' the time delay is 1000 milliseconds.

'

Public Sub Start()

Do

nrings += 1

If stopFlag Then

Exit Do

Else

If _snoozePressed Then

System.Threading.Thread.Sleep(1000)

If (True) Then

Dim e As New AlarmEventArgs(_snoozePressed, nrings)

OnAlarm(e)

End If

Else

System.Threading.Thread.Sleep(300)

Dim e As New AlarmEventArgs(_snoozePressed, nrings)

OnAlarm(e)

End If

End If

Loop

End Sub

End Class

' The WakeMeUp class has a method AlarmRang that handles the

' alarm event.

'

Public Class WakeMeUp

Public Sub AlarmRang(sender As Object, e As AlarmEventArgs)

Console.WriteLine((e.AlarmText + ControlChars.Cr))

If Not e.SnoozePressed Then

If e.NumRings Mod 10 = 0 Then

Console.WriteLine(" Let alarm ring? Enter Y")

Console.WriteLine(" Press Snooze? Enter N")

Console.WriteLine(" Stop Alarm? Enter Q")

Dim input As String = Console.ReadLine()

If input.Equals("Y") Or input.Equals("y") Then

Return

Else

If input.Equals("N") Or input.Equals("n") Then

CType(sender, AlarmClock).SnoozePressed = True

Return

Else

CType(sender, AlarmClock).Stop = True

Return

End If

End If

End If

Else

Console.WriteLine(" Let alarm ring? Enter Y")

Console.WriteLine(" Stop Alarm? Enter Q")

Dim input As String = Console.ReadLine()

If input.Equals("Y") Or input.Equals("y") Then

Return

Else

CType(sender, AlarmClock).Stop = True

Return

End If

End If

End Sub

End Class

' The driver class that hooks up the event handling method of

' WakeMeUp to the alarm event of an Alarm object using a delegate.

' In a forms-based application, the driver class is the

' form.

'

Public Class AlarmDriver

Public Shared Sub Main()

' Instantiates the event receiver.

Dim w As New WakeMeUp()

' Instantiates the event source.

Dim clock As New AlarmClock()

' Wires the AlarmRang method to the Alarm event.

AddHandler clock.Alarm, AddressOf w.AlarmRang

clock.Start()

End Sub

End Class

End Namespace


网站栏目:vb.net委托循环,vbnetfor循环
当前链接:http://cdkjz.cn/article/hcphdg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220