Verity
Verity

Reputation: 11

How to rebuild a windows application when it is running

I build a windows application as a POS system, and I want to add a function that let the users add new button in the windows. So after user input information, such as item name and price, the application will generate code to create a new button and add the code into MainForm.cs and MainForm.Designer.cs.

However, the change in the source code would not take effect until I stop the application and re-run compiled exe. Is that possible to make the application rebuild itself when it is still running? (i.e. the user can see the new button show up on the windows right after he/she input the information)

I have tried using Application.Restart(); and this.Refresh();, but it didn't work.

Upvotes: 1

Views: 1069

Answers (3)

Dai
Dai

Reputation: 155648

In a word, no - because rebuilding an application replaces its executable image file on-disk, which is usually locked by the running process.

Visual Studio and other IDEs support "Edit and Continue" which allows for the replacement of code at runtime while the program is waiting in a breakpoint, but only in a limited number of cases (such as amending code in an existing method). It isn't flexible enough for wholesale feature or GUI changes (and if you're using XAML Resources, it's quite impossible).

If you want the kind of flexibility you're looking for, then you'll have to dig-deep for an old-school Mainframe-style system (such as an IBM Z-series) that supports these kinds of features.

Upvotes: 0

Kek
Kek

Reputation: 3195

I think this is possible... but there are limitations. You can create an AppDomain and load a dll code (this dll containing your application form). then, you unload the app domain, build the dll and recreate a new AppDomain.

Of course, you have to close the windows you are in and have a DLL architecture quite different thant just a simple WinForms application.

this is the kind of things I would recommend for auto-update for example

Upvotes: 0

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174457

You are not using the correct approach here.

You shouldn't modify the code of your application based on user input.
Rather, save the data the user entered in a database or file and use this information to dynamically display the buttons.

Upvotes: 6

Related Questions