Michele
Michele

Reputation: 57

converting ASP .Net WPF Application to Run Without the Window (Form)

I need to convert my app so it runs by itself, not when the user clicks the submit button on the small form

Here's my code:

{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        static readonly HttpClient http = new HttpClient();

        public MainWindow()
        {
            InitializeComponent();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            string accountName = "foo";
            string userName = "foo1";
            string password = "foo2";
            string key = "=foo3";



            // Query the DB
            // SELECT DISTINCT Column FROM View
            // List<string> orderNumbers
            // foreach(orderNumber in orderNumbers){

                // Get the invoice records from the database
                List<DBInvoiceModel> invoiceRecords = GetInvoiceRecords(102348);

                // Calculate the XML request and send it
                string UpdateOrderXML = await UpdateOrder(accountName, password, userName, invoiceRecords);
                //string ordersXML = await QueryOrders(key, accountName);  
            //}
        }



Do you have any suggestions? I need to know how to modify the MainWindow so that it will run unattended, in a case where it runs via stored procedure or batch job.

Upvotes: 1

Views: 82

Answers (1)

mm8
mm8

Reputation: 169200

You could move your code out of the Button_Click event handler into an async method and call it from both the Click handler and a Loaded event handler:

public partial class MainWindow : Window
{
    static readonly HttpClient http = new HttpClient();

    public MainWindow()
    {
        InitializeComponent();
        Loaded += async (e, s) => await UpdateOrder();
    }

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        await UpdateOrder();
    }

    private async Task UpdateOrder()
    {
        string accountName = "foo";
        string userName = "foo1";
        string password = "foo2";
        string key = "=foo3";

        // Query the DB
        // SELECT DISTINCT Column FROM View
        // List<string> orderNumbers
        // foreach(orderNumber in orderNumbers){

        // Get the invoice records from the database
        List<DBInvoiceModel> invoiceRecords = GetInvoiceRecords(102348);

        // Calculate the XML request and send it
        string UpdateOrderXML = await UpdateOrder(accountName, password, userName, invoiceRecords);
        //string ordersXML = await QueryOrders(key, accountName);  
        //}
    }
}

Upvotes: 2

Related Questions