Help in translating PYTHON to VB.NET
I am coding an application in VB.NET that sends sms.
Could you please send a message to PYTHON-> VB.NET a translation of this code and / or recommendations?
Thanks in advance!
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()
a source to share
This code creates a stream for each msg / no tuple and calls sendmsg. The first "for each ... each.start ()" starts the thread (which only calls sendmsg), and the second "for each ... each.join ()" waits for each thread to finish. Depending on the number of records, this can create a significant number of threads (which if you are sending 1000 SMS records), which is not necessarily efficient, although it is asynchronous.
The code is relatively simple and pythonic, whereas for .NET you probably want to use ThreadPool or BackgroundWorker to make sendmsg calls. You will need to create a .NET class that is equivalent to the (msg, no) tuple and probably put the sendmsg () function in the class itself. Then create .NET code to load messages (which are not shown in the Python code). Usually, you should use the general list <> to store SMS records. Then ThreadPool enqueues all the items and calls sendmsg.
If you are trying to keep the code as equivalent to the original Python, then you should take a look at IronPython .
(The underscore in sendmsg caused the text to use italics, so I removed the underline in my answer.)
a source to share
This is IronPython code ("Python for .NET"), so the source code uses the .NET Framework just like VB and all classes (even System.Threading.Thread ) can be used just like shown.
Some tips:
MessageThread
comes from Thread
, msg
and no
must be declared as class variables, __init__
is a constructor, self
-parameter in member functions is not recoded in VB (just leave that). Use List<Thread>
for thread_list
and define a small structure for tuples in records
.
a source to share