开发者

Blackberry socket connection and threads

开发者 https://www.devze.com 2023-04-10 05:18 出处:网络
I am developing a program which have 2 buttons : Start and End. When i click Start it will start to get Locations in every 30sec. and send them to a server using socket connection. When 开发者_如何学编

I am developing a program which have 2 buttons : Start and End. When i click Start it will start to get Locations in every 30sec. and send them to a server using socket connection. When 开发者_如何学编程i click the End button it will stop all works. Can i do this without using thread? If i need to use threads what is the best way to implement it. Should i listen LocationProvider with LocationListener to get locations every 30sec and send them in a thread or should i create connection thread and get location in every and send.


Unfortunatelly this is a partial answer (it's difficult for me to understand the rest of your question):

Can i do this without using thread?

This is impossible. If you start doing this on the main UI thread, then your app UI will have to wait untill all is done (networking is a time-consuming undertaking). So the only way is to do this on a background thread.


If you make a connection in the UI main thread, you application will become unresponsive until the connection is terminated, which is wrong!!! You have to create a thread for each connection you establish, this is strongly recommended.

You will have to create a thread that will sleep for 30 seconds each time and then do the work that you said.

Your work should be done in threads, do not use the main thread for these things


You must use another thread besides the main UI thread (EDT) for network transactions. You can not use the main thread for network transactions because this is a time consuming action that will make the main thread unresponsive and your application will eventually crash.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号