A method was created to be initialized with my Windows Form Application when I start it. However, when I call my method just below the InitializeComponent();, my whole Windows Form Application doesn't start, and it doesn't throw me any error.
Myclass mc = new Myclass();
public Interceptor()
{
InitializeComponent();
mc.myMethod();
>rest of the code
}
This is the class with the method:
public class Listener
{
public void myMethod() {
//Recieving the parameters to listen
Int32 port = 5000;
IPAddress ip = IPAddress.Parse("1.1.1.1");
TcpListener server = new TcpListener(ip, port);
//starting the server
server.Start();
//Accepting connection from the Client
Socket socket = server.AcceptSocket();
//Storing the data
byte[] bytes = new byte[4096];
int k = socket.Receive(bytes);
//???
ASCIIEncoding asen = new ASCIIEncoding();
string data = null;
data = Encoding.ASCII.GetString(bytes, 0, k);
//Reading all the data using the TextReader()
TextReader reader = new StringReader(data);
XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
MyObject obj = (MyObject)serializer.Deserialize(reader);
string json = JsonConvert.SerializeObject(icx, Formatting.Indented, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
string path = @"c:/temp";
string name = "test";
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
string fileName = $"{path}\\{name}.json";
File.WriteAllText(fileName, json);
}
}
As you guys can see, my method is deserializing a XML object and serializing to JSON object. When I add this method to a button, it works perfectly. I basically need this method to be initialized with my application, and the user doesn't need to press any button to run it. I spent thre days without any progress.
Hope I made myself clear.
Cheers!