@m_adam said in How to get mouse drag info from external through Python API?:
Hi @lotusab12
In such a thread you need to call TestBreak() in order to test if Cinema 4D tell your thread to stop.
This will give us something like:
def Main(self):
self.sock.bind(("localhost", 20015))
while(True):
if self.TestBreak():
server.close()
self.End()
data, addr = self.sock.recvfrom(1024)
def End(self):
self.sock.close()
Another and probably safer version also is to define a timeout this way you can shut down your server if nothing happens since a given time lapse.
Cheers,
Maxime.
import c4d
import socket
from c4d.threading import C4DThread
class A(C4DThread):
sock = socket.socket(socket.AF_INIT, socket.SOCK_DGRAM)
def Main(self):
self.sock.bind(("localhost", 20015))
while(True):
if self.TestBreak():
self.End()
data, addr = self.sock.recvfrom(1024)
def End(self):
self.sock.close()
if __name__ == "__main__":
thread = A()
thread.Start()
Hi Maxime, I tried TestBreak(), above are my code, but after I close Cinema4D, CINEMA4D.exe was still hunging up in Windows Task Monitor, it can not be killed properly, I tried to remove line "data, addr = self.sock.recvfrom(1024)" and add a "break" in "if TestBreak():", CINEMA4D.exe can be killed in Task Monitor, so please help me pointing out where I was wrong, it seems something interup with thread itself?
PS: I don't know what does the line "server.close()" in your code mean, does it equal to self.sock.close()?