Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static ConcurrentDictionary<string, JoystickProcess> clientToDevice;
- private Thread listenThread;
- public MainForm()
- {
- InitializeComponent();
- Network network = new Network(this);
- clientToDevice = new ConcurrentDictionary<string, JoystickProcess>();
- listenThread = new Thread(network.receive);
- // Start threads in background
- listenThread.IsBackground = true;
- listenThread.Start();
- }
- public JoystickProcess getClient(string client)
- {
- if (clientList.InvokeRequired)
- {
- // Causes System.ObjectDisposedException
- return (JoystickProcess) Invoke(new Func<JoystickProcess>(() => getClient(client)));
- }
- else
- {
- JoystickProcess process;
- if (clientToDevice.TryGetValue(client, out process))
- return process;
- return null;
- }
- }
- public void receive()
- {
- int port = 1608;
- UdpClient client = new UdpClient(port);
- IPEndPoint end = new IPEndPoint(IPAddress.Any, port);
- String addr;
- byte[] recv;
- JoystickProcess process;
- while (true)
- {
- // Wait for data to be received
- recv = client.Receive(ref end);
- addr = end.Address.ToString();
- if (ipList.Contains(addr))
- {
- // Causes System.ObjectDisposedException
- process = main.getClient(addr);
- if (process != null && process.getDevice() != -1)
- {
- Task.Run(() => process.updateDevice(recv));
- }
- }
- else
- {
- ipList.Add(addr);
- main.addClient(addr);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement