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!
CodePudding user response:
Your Interceptor()
method is the constructor of (what appears to be) the Form. So this is called when you create the form and the job of the constructor is initialisation of the form, settings defaults etc.
By placing your mc.myMethod()
within the constructor you are actually forcing it to start your business logic already. This means, it will be performed before the form is finished being created (and shown).
So this is not good and as your code in this method blocks the UI thread then this is why you have this problem, because you don't allow the form to finished being created before you are processing data and blocking the UI.
You should instead use Form.Load
event, or optionally the Form.Activate or Form.Shown events.
To understand the difference and which one suits you best, see this:
https://docs.microsoft.com/en-us/dotnet/desktop/winforms/order-of-events-in-windows-forms?view=netframeworkdesktop-4.8
Even though this would be a better approach, you still have the issue that the code is blocking the UI thread.
So then you need to look at using other methods to avoid this.
There are different ways to do this, but one is to use a backgroundworker.
//Initialise the worker and set the worker method
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork = bw_DoWork;
//Start the worker
bw.RunWorkerAsync();
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
//Put your myMethod code here
}
See this for more details and the full capability of the backgroundworker and better examples: https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker?view=net-6.0