views:

387

answers:

2

I am coding an application in VB.NET that sends sms.

Would you please post PYTHON->VB.NET translation of this code and/or guidelines?

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: 

This is IronPython-code ("Python for .NET"), hence the source code uses the .NET-Framework just as VB and all classes (even System.Threading.Thread) can be used in the same way shown.

Some tips:

MessageThread derives from Thread, msg and no must be declared as class variables, __init__ is the constructor, the self-parameter in the member functions isn't transcoded to VB (just leave it out). Use a List<Thread> for thread_list and define a little structure for the tuples in records.

Dario
+1  A: 

This code creates a thread for every 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 complete. Depending on the number of records, this could create a significant number of threads (what if you were sending 1000 SMS records) which is not necessarily efficient although it is asynchronous.

The code is relatively simple and pythonic, whereas for .NET you would probably want to use a ThreadPool or BackgroundWorker to do the sendmsg calls. You will need to create a .NET class that is equivalent to the tuple (msg,no), and probably put the sendmsg() function in the class itself. Then create .NET code to load the messages (which is not shown in the Python code). Typically you would use a generic List<> to hold the SMS records as well. Then the ThreadPool would queue all of the items and call sendmsg.

If you are trying to keep the code as equivalent to the original Python, then you should look at IronPython.

(The underscore in sendmsg caused the text to use italics, so I removed the underscore in my response.)

Ryan