I'm getting data from UDP port and there is no problem to show it in console. I've got a problem trying to send this data to Windows Form and use it there. Here is my code:
using System;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
namespace ConsoleApp2 {
class Program {
static void Main(string[] args) {
Console.WriteLine();
Thread t = new Thread(FormDrawing);
t.Start(UDPListener());
}
// Listening Port
static byte[] UDPListener() {
Console.WriteLine("Waiting for client");
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 162);
EndPoint ep = (EndPoint)ipep;
socket.Bind(ep);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0);
byte[] indata = new byte[1024];
IPEndPoint peer = new IPEndPoint(IPAddress.Any, 0);
EndPoint inep = (EndPoint)peer;
int inlen = socket.ReceiveFrom(indata, ref inep);
socket.Close();
Console.WriteLine(indata.ToString());
return indata;
}
// Drawing Form
static void FormDrawing(object ob) {
byte[] obj = (byte[])ob;
Form mainForm = new Form();
if (obj != null) {
Button MS2 = new Button {
Location = new Point(0, 0),
Size = new Size(80, 80),
Text = obj.ToString(),
BackColor = Color.Green
};
mainForm.Controls.Add(MS2);
}
mainForm.Show();
Thread.Sleep(5000);
}
}
}
Window opens with some kind of button. So, obj is not null. But this button is white. Form closes after 5 seconds. What can be the problem?