Introduction
In today’s article, we will look at a feature just introduced with Visual Studio 2019. This is the hot reload feature. In the past, if we made any code change in the backend code, we would need to restart the application to apply this change. This has now changed with the new hot reload feature. We can change our backend code on the fly while running the application, apply these changes, and see the results of the new code all while the application is running. Let us take a look at a simple implementation of this new feature.
Creating the VS 2019 application
For now, this feature is available with the Visual Studio 2019 Preview Version 16.11.0 Preview 1.0 version as below,
This feature should also be part of the upcoming Visual Studio 2022 version.
Let us create a Windows forms application. I am only creating this application as it is a simple way to demonstrate this new feature. The new hot reload feature can also be used with other types of applications like WPF, ASP.NET, etc.
Here, we will add a button and label it to the form. When we click on the button, we simply change the text as below,
- using System.Threading.Tasks;
- using System.Windows.Forms;
-
- namespace WinFormsAppHotReload
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void button_Click(object sender, EventArgs e)
- {
- label.Text = "The value is set to one";
- }
- }
- }
Now, we run the application and click on the button,
Now suppose we want to change the message. Previously we would need to stop the application, change the code, and run it again. However, with the new hot reload feature, we can do this while the application is running.
While the application is running, open the code file and make the change as below,
Then click as below,
Now, if you click the button again, you get the updated code message as below,
Summary
In this article, we looked at the new hot reload feature introduced in Visual Studio 2019. This will certainly make the development process much quicker and robust. Microsoft states as this is a new feature it might still contain some bugs. However, we can expect it to be very solid by the time Visual Studio 2022 is released.
Happy coding!