Visual_Basic 板


LINE

※ 引述《kenit (很闲的人)》之铭言: : 小弟最近刚开始学用.NET 2003写vb程式 : 一方面是兴趣一方面是公事上的需要 : 我需要写一个用ping的方式来确定公司的电脑 : 是不是有开机的程式 : 程式写完之後很简单 : 只用到了网路上的一个叫做pivo ping的元件 : 还有dataset和DataAdapter来读取和更新资料库 : 然後用while和thread.sleep()来让它在固定的相隔时间 : 重复执行 : 程式码全部加起来不到50行 : 可是执行的时候 : 却需要花到30mb左右的记忆体 : 这还只是它在sleep的时候占用的大小 : 似乎太夸张了点 : 於是小弟参考了网路上的建议 : 把没必要的变数都舍去不用 : 在程式sleep的时候 : 把不用保留的变数和dataset的资源都释放掉 : 可是执行时还是需要25mb左右的大小..orz : 小弟想问一下 : .net的程式是不是都特别耗记忆体 : 小弟看我那开了五 六个分页的firefox : 不也才使用了30几mb的记忆体 : 虽然现在记忆体动辄就是giga起跳的 : 可是这样浪费记忆体也不是个办法 : 希望各位前辈给小弟一些建议 : 感激不尽~ 我只有VB6的范例,不知能否帮到你 '========================================================================== Dim ECHO As ICMP_ECHO_REPLY Dim pos As Long Dim success As Long If SocketsInitialize() Then 'ping the ip passing the address, text 'to send, and the ECHO structure. success = Ping(("168.95.1.1"), ("1234567890123456"), ECHO) 'display the results MSGBOX "网路反应时间:" & ECHO.RoundTripTime & " ms" SocketsCleanup Else MsgBox "Windows Sockets for 32 bit Windows " & _ "environments is not successfully responding." End IF '以下程式在模组 Private Const IP_SUCCESS As Long = 0 Private Const IP_STATUS_BASE As Long = 11000 Private Const IP_BUF_TOO_SMALL As Long = (11000 + 1) Private Const IP_DEST_NET_UNREACHABLE As Long = (11000 + 2) Private Const IP_DEST_HOST_UNREACHABLE As Long = (11000 + 3) Private Const IP_DEST_PROT_UNREACHABLE As Long = (11000 + 4) Private Const IP_DEST_PORT_UNREACHABLE As Long = (11000 + 5) Private Const IP_NO_RESOURCES As Long = (11000 + 6) Private Const IP_BAD_OPTION As Long = (11000 + 7) Private Const IP_HW_ERROR As Long = (11000 + 8) Private Const IP_PACKET_TOO_BIG As Long = (11000 + 9) Private Const IP_REQ_TIMED_OUT As Long = (11000 + 10) Private Const IP_BAD_REQ As Long = (11000 + 11) Private Const IP_BAD_ROUTE As Long = (11000 + 12) Private Const IP_TTL_EXPIRED_TRANSIT As Long = (11000 + 13) Private Const IP_TTL_EXPIRED_REASSEM As Long = (11000 + 14) Private Const IP_PARAM_PROBLEM As Long = (11000 + 15) Private Const IP_SOURCE_QUENCH As Long = (11000 + 16) Private Const IP_OPTION_TOO_BIG As Long = (11000 + 17) Private Const IP_BAD_DESTINATION As Long = (11000 + 18) Private Const IP_ADDR_DELETED As Long = (11000 + 19) Private Const IP_SPEC_MTU_CHANGE As Long = (11000 + 20) Private Const IP_MTU_CHANGE As Long = (11000 + 21) Private Const IP_UNLOAD As Long = (11000 + 22) Private Const IP_ADDR_ADDED As Long = (11000 + 23) Private Const IP_GENERAL_FAILURE As Long = (11000 + 50) Private Const MAX_IP_STATUS As Long = (11000 + 50) Private Const IP_PENDING As Long = (11000 + 255) Private Const PING_TIMEOUT As Long = 500 Private Const WS_VERSION_REQD As Long = &H101 Private Const MIN_SOCKETS_REQD As Long = 1 Private Const SOCKET_ERROR As Long = -1 Private Const INADDR_NONE As Long = &HFFFFFFFF Private Const MAX_WSADescription As Long = 256 Private Const MAX_WSASYSStatus As Long = 128 Private Type ICMP_OPTIONS Ttl As Byte Tos As Byte Flags As Byte OptionsSize As Byte OptionsData As Long End Type Public Type ICMP_ECHO_REPLY Address As Long 'Contains the replying IP address. status As Long 'Contains the reply IP_STATUS RoundTripTime As Long 'RTT in milliseconds. DataSize As Long 'Contains the reply data size in bytes Reserved As Integer 'Reserved for system use DataPointer As Long 'Contains a pointer to the reply data Options As ICMP_OPTIONS 'String containing reply options Data As String * 250 End Type Private Type WSADATA wVersion As Integer wHighVersion As Integer szDescription(0 To MAX_WSADescription) As Byte szSystemStatus(0 To MAX_WSASYSStatus) As Byte wMaxSockets As Long wMaxUDPDG As Long dwVendorInfo As Long End Type Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long Private Declare Function IcmpCloseHandle Lib "icmp.dll" _ (ByVal IcmpHandle As Long) As Long Private Declare Function IcmpSendEcho Lib "icmp.dll" _ (ByVal IcmpHandle As Long, _ ByVal DestinationAddress As Long, _ ByVal RequestData As String, _ ByVal RequestSize As Long, _ ByVal RequestOptions As Long, _ ReplyBuffer As ICMP_ECHO_REPLY, _ ByVal ReplySize As Long, _ ByVal Timeout As Long) As Long Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long Private Declare Function WSAStartup Lib "WSOCK32.DLL" _ (ByVal wVersionRequired As Long, _ lpWSADATA As WSADATA) As Long Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long Private Declare Function gethostname Lib "WSOCK32.DLL" _ (ByVal szHost As String, _ ByVal dwHostLen As Long) As Long Private Declare Function gethostbyname Lib "WSOCK32.DLL" _ (ByVal szHost As String) As Long Private Declare Sub CopyMemory Lib "kernel32" _ Alias "RtlMoveMemory" _ (xDest As Any, _ xSource As Any, _ ByVal nbytes As Long) Private Declare Function inet_addr Lib "WSOCK32.DLL" _ (ByVal s As String) As Long Public Function GetStatusCode(status As Long) As String Dim msg As String Select Case status Case IP_SUCCESS: msg = "ip success" Case INADDR_NONE: msg = "inet_addr: bad IP format" Case IP_BUF_TOO_SMALL: msg = "ip buf too_small" Case IP_DEST_NET_UNREACHABLE: msg = "ip dest net unreachable" Case IP_DEST_HOST_UNREACHABLE: msg = "ip dest host unreachable" Case IP_DEST_PROT_UNREACHABLE: msg = "ip dest prot unreachable" Case IP_DEST_PORT_UNREACHABLE: msg = "ip dest port unreachable" Case IP_NO_RESOURCES: msg = "ip no resources" Case IP_BAD_OPTION: msg = "ip bad option" Case IP_HW_ERROR: msg = "ip hw_error" Case IP_PACKET_TOO_BIG: msg = "ip packet too_big" Case IP_REQ_TIMED_OUT: msg = "ip req timed out" Case IP_BAD_REQ: msg = "ip bad req" Case IP_BAD_ROUTE: msg = "ip bad route" Case IP_TTL_EXPIRED_TRANSIT: msg = "ip ttl expired transit" Case IP_TTL_EXPIRED_REASSEM: msg = "ip ttl expired reassem" Case IP_PARAM_PROBLEM: msg = "ip param_problem" Case IP_SOURCE_QUENCH: msg = "ip source quench" Case IP_OPTION_TOO_BIG: msg = "ip option too_big" Case IP_BAD_DESTINATION: msg = "ip bad destination" Case IP_ADDR_DELETED: msg = "ip addr deleted" Case IP_SPEC_MTU_CHANGE: msg = "ip spec mtu change" Case IP_MTU_CHANGE: msg = "ip mtu_change" Case IP_UNLOAD: msg = "ip unload" Case IP_ADDR_ADDED: msg = "ip addr added" Case IP_GENERAL_FAILURE: msg = "ip general failure" Case IP_PENDING: msg = "ip pending" Case PING_TIMEOUT: msg = "ping timeout" Case Else: msg = "unknown msg returned" End Select GetStatusCode = CStr(status) & " [ " & msg & " ]" End Function Public Function Ping(sAddress As String, sDataToSend As String, ECHO As ICMP_ECHO_REPLY) As Long 'If Ping succeeds : '.RoundTripTime = time in ms for the ping to complete, '.Data is the data returned (NULL terminated) '.Address is the Ip address that actually replied '.DataSize is the size of the string in .Data '.Status will be 0 ' 'If Ping fails .Status will be the error code Dim hPort As Long Dim dwAddress As Long 'convert the address into a long representation dwAddress = inet_addr(sAddress) 'if a valid address.. If dwAddress <> INADDR_NONE Then 'open a port hPort = IcmpCreateFile() 'and if successful, If hPort Then 'ping it. Call IcmpSendEcho(hPort, _ dwAddress, _ sDataToSend, _ Len(sDataToSend), _ 0, _ ECHO, _ Len(ECHO), _ PING_TIMEOUT) 'return the status as ping succes and close Ping = ECHO.status Call IcmpCloseHandle(hPort) End If Else 'the address format was probably invalid Ping = INADDR_NONE End If End Function Public Sub SocketsCleanup() If WSACleanup() <> 0 Then MsgBox "Windows Sockets error occurred in Cleanup.", vbExclamation End If End Sub Public Function SocketsInitialize() As Boolean Dim WSAD As WSADATA SocketsInitialize = WSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS End Function --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.133.214.220







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:iOS站内搜寻

TOP