您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

.net编程时在线程里使用Form上的控件的问题

2024/5/30 21:01:11发布27次查看
在写vb.net时,遇到要在线程里改变form上的listview控件中的信息,动手做起来才发现并不是那么容易。因为微软的框架并不鼓励在线程里直接访问form上的控件。因为这样会改变ui进程的正常执行。如果必须这样做,需要在线程中使用invoke函数或者invokebegin函数。下面是一个例子:
imports system.threading
public class form1
delegate sub addlistitem(byval ipstring as string, byval scanport as integer)
public mydelegate as addlistitem
public openportcount as integer = 0
private sub form1_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
mydelegate = new addlistitem(addressof addlistitemmethod)
end sub
private sub start_button_click(byval sender as system.object, byval e as system.eventargs) handles start_button.click
dim mythread as thread
mythread = new thread(new threadstart(addressof threadfunction))
mythread.start()
end sub
private sub threadfunction()
dim mythread as thread
mythread = new thread(new threadstart(addressof doscanthread))
mythread.start()
end sub 'threadfunction
private sub doscanthread()
dim mythreadclassobject as new scanthreadclass(me)
mythreadclassobject.run()
end sub
public sub addlistitemmethod(byval ipstring as string, byval scanport as integer)
listview_result.items.add(ipstring, openportcount) 'scanip.tostring(), 0)
listview_result.items(openportcount).subitems.add(scanport.tostring())
openportcount += 1
end sub 'addlistitemmethod
end class
public class scanthreadclass
private myformcontrol1 as form1
public sub new(byval myform as form1)
myformcontrol1 = myform
end sub 'new
public sub run()
myformcontrol1.invoke(myformcontrol1.mydelegate, new object() {11, 123})
end sub
end class
主要注意的就是invoke的调用和delegate的定义。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product