I am having a pretty weird issue, earlier today I had a large amount of code working and running in Visual Studio, my form worked flawlessly. I went for lunch came back and tried to open it and nothing. It runs, there are 0 errors, it uses memory but it won't display the form. I have not changed anything as far as I am aware.
I have created a new Windows Form Application and re-written the code line by line, I have found what breaks it however I cannot for the life of me figure out why it is breaking it.
Form1.cs broken
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RestSharp;
using Newtonsoft;
using Newtonsoft.Json;
namespace RestApiViewerWUG
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var client = new RestClient("http://notrelevent.whocares.com/api/v1/token");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Basic cmVzdDo5RGJSIypkQDQ=");
request.AddHeader("Content-Type", "text/plain");
request.AddParameter("text/plain", "userName=rest&password=xxxxxx&grant_type=password", ParameterType.RequestBody);
IRestResponse response = client.Execute(request); //THIS LINE BREAKS IT
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
The issue
Simply removing the line "IRestResponse response = client.Execute(request);" fixes it and allows the form to appear.
What have I tried?
Other info
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RestApiViewerWUG
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
The form:
Literally just textBox with the exact name of textBox1
The short story is, you shouldn't be doing this type of thing in the constructor, do it after the form loads.
Then, if you there is a timeout, you aren't waiting for the form to load and can give feedback of the fact it's in progress, or if there is an exception
you aren't tearing the form down.
You would also catch any exceptions, and give some notification of the failure modes that might happen.
Background
A forms constructor runs before a form is loaded (rendered), so it's best not to do anything expensive in them, in fact this is true for any constructor. A constructor is for initializing the minimum state the class/struct needs to function.
In regards to a forms constructor, if there is a long running process that happens during the constructor, the form load is directly effected and won't show leaving the user (and you) to scratch your head wondering why the form isn't showing. Even worse, if there is an unhandled exception your form never loads