[英]Help translation PYTHON to VB.NET
我正在VB.NET中编码发送短信的应用程序。
您是否可以将此代码和/或准则的PYTHON-> VB.NET翻译发布?
提前致谢!!!
import threading
class MessageThread(threading.Thread):
def __init__(self,msg,no):
threading.Thread.__init__(self)
self.msg = msg # text message
self.no = no # mobile number
def run(self):
# function that sends "msg" to "no"
send_msg(msg,no)
# records of users are retrived from database
# and (msg,no) tuples are generated
records = [(msg1,no1),(msg2, no2),...(msgN,noN)]
thread_list = []
for each in records:
t = MessageThread(each)
thread_list.append(t)
for each in thread_list:
each.start()
for each in thread_list:
each.join()
此代码为每个味精/无元组创建一个线程,并调用sendmsg。 第一个“ for each ... each.start()”启动线程(仅调用sendmsg),第二个“ for each ... each.join()”等待每个线程完成。 根据记录的数量,这可能会创建大量的线程(如果您要发送1000条SMS记录的话),尽管它是异步的,但不一定高效。
该代码相对简单并且是Python式的,而对于.NET,您可能希望使用ThreadPool或BackgroundWorker进行sendmsg调用。 您将需要创建一个与元组(msg,no)等效的.NET类,并且可能将sendmsg()函数放入该类本身中。 然后创建.NET代码以加载消息(Python代码中未显示)。 通常,您也会使用通用List <>来保存SMS记录。 然后,ThreadPool将所有项目排队,并调用sendmsg。
如果您想使代码与原始Python等效,则应查看IronPython 。
(sendmsg中的下划线导致文本使用斜体,因此我在响应中删除了下划线。)
这是IronPython代码(“ Python for .NET”),因此源代码使用.NET-Framework就像VB一样,所有类(甚至是System.Threading.Thread )都可以按照所示的相同方式使用。
一些技巧:
MessageThread
派生自Thread
, msg
, no
必须声明为类变量, __init__
是构造函数,成员函数中的self
参数未转码为VB(只需将其保留)。 使用List<Thread>
为thread_list
和定义在元组一个小结构records
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.