加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

VB.NET 编写过程中的注意事项

发布时间:2020-12-17 07:50:06 所属栏目:百科 来源:网络整理
导读:如下`代码中运行起来好像不响应,主要不弹出带“ok”内容的消息框 ,也不会传输数据。 这是因为,IEnumerable接口找不到了实例对象导致的。 只有对IEnumerable对象引用实例对象迭代器才有效。 好了,继续解决如下程序问题。 如下程序是多线程模式下运行。所

如下`代码中运行起来好像不响应,主要不弹出带“ok”内容的消息框 ,也不会传输数据。


这是因为,IEnumerable接口找不到了实例对象导致的。


只有对IEnumerable对象引用实例对象迭代器才有效。


好了,继续解决如下程序问题。


如下程序是多线程模式下运行。所以,必须留下中间一个函数。这是必要的。


如下中间函数MyFunc2过程里处理IEnumerble接口的实例引用。

这样保证迭代器有效执行。

迭代器是IEnumerable接口的附属物,只有返回类型为IEnumerable对象上使用。

也可以lambda表达式中使用,注意的是只能Function上执行。


好了,继续处理问题。

插入代码处理如下;

PrivateFunctionMyFunc2()AsIEnumerable(OfInteger)

Dim Func1 As New Func(OfIEnumerable(Of Integer ))( AddressOf Me .MyFunc1)
Dim List1 As New List(Of Integer)(Func1.Invoke)
ReturnList1.AsEnumerable
End Function

如上这样已经处理完成了。

或者 MySub过程里实例引用处理;

Dim List1 As New List(Of Integer)(Task1.Result)这样也可以执行。


处理前的代码;

Public Class Class1
Sub MySub()
Dim Task1 As New Task(OfIEnumerable(Of Integer ))( AddressOf Me .MyFunc2)
Task1.Start()
Task1.Wait()
End Sub
Private Iterator Function MyFunc1() As IEnumerable(Of Integer )
MsgBox( "ok" )
Yield5
End Function
Private Function MyFunc2() As IEnumerable(Of Integer )
Dim Func1 As New Func(OfIEnumerable(Of Integer ))( AddressOf Me .MyFunc1)
Return Func1.Invoke
End Function
End Class
Public Class Form1
Private Sub Button1_Click(sender As Object ,e As EventArgs) Handles Button1.Click
Dim Cla1 As New Class1
Cla1.MySub()
End Sub
End Class

处理后的代码如下;

Public Class Class1
Sub MySub()
Dim Task1 As New Task(OfIEnumerable(Of Integer ))( AddressOf Me .MyFunc2)
Task1.Start()
Task1.Wait()
End Sub
Private Iterator Function MyFunc1() As IEnumerable(Of Integer )
MsgBox( "ok" )
Yield5
End Function

PrivateFunctionMyFunc2()AsIEnumerable(OfInteger)

Dim Func1 As New Func(OfIEnumerable(Of Integer ))( AddressOf Me .MyFunc1)
Dim List1 As New List(Of Integer)(Func1.Invoke)
ReturnList1.AsEnumerable
End Function
End Class
Public Class Form1
Private Sub Button1_Click(sender As Object ,e As EventArgs) Handles Button1.Click
Dim Cla1 As New Class1
Cla1.MySub()
End Sub
End Class

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读