Lee
Lee

Reputation: 3969

Windows form loads then quits

I'm creating a checkout system for a supermarket. It consists of a checkout, server and MIS program an operates WCF services between them. The problem I have is that the checkout program, which is a windows form, does a few neccessaries in it's application_load method and then just quits.

Here's the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CheckoutLibrary;
using Checkout.ServerLibraryService;
using Checkout.MarketService;

namespace Checkout
{
    public partial class theForm : Form
    {
        private static int checkoutID = 3;
        private Product[] allProducts;

        public theForm()
        {
            InitializeComponent();
        }

        private void theForm_Load(object sender, EventArgs e)
        {
            // First cache all products
            SupermarketServiceSoapClient marketService = new SupermarketServiceSoapClient();
            allProducts = marketService.GetAllProducts();
            // Load the service provided by the server
            ServiceClient serverService = new ServiceClient();
            // Load the event handlers for the bar code scanner
            BarcodeScanner scanner = new BarcodeScanner();
            scanner.ItemScanned += new BarcodeScanner.ItemScannedHandler(scanner_ItemScanned);
            scanner.AllItemsScanned += new BarcodeScanner.AllItemsScannedHandler(scanner_AllItemsScanned);
            scanner.Start(checkoutID);
        }

        void scanner_AllItemsScanned(EventArgs args)
        {
            throw new NotImplementedException();
        }

        void scanner_ItemScanned(ScanEventArgs args)
        {
            itemTextBox.Text = "Scanned " + GetItemName(args.Barcode);
        }

        private void scanItemButton_Click(object sender, EventArgs e)
        {
            scanner_ItemScanned(new ScanEventArgs(GetRandBarcode()));
        }

        // A barcode -> product name look up method
        public string GetItemName(int barcode)
        {
            return allProducts[barcode].Description + " @ " + allProducts[barcode].Price;
        }

        // Method to grab a random barcode for simulation
        private int GetRandBarcode()
        {
            Random rand = new Random();
            return rand.Next(0,500);
        }
    }
}

And program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Checkout
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new theForm());
        }
    }
}

Thanks for any insight.

Upvotes: 4

Views: 231

Answers (1)

bugfixr
bugfixr

Reputation: 8077

In WinForms, if your form_load throws an exception, it quits without displaying anything. Annoying, but I'm guessing that's the problem.

You can try a try/catch, or you can hit CTRL+ALT+E and check the Thrown Column for Common Language Runtime Exceptions to see the error.

UPDATE:

Based on comments, here's a sample way to execute something on another thread.

ThreadStart ts = new ThreadStart(() => {
   try {
       scanner.Start(checkoutID);
   } catch {
       // Log error
   }
});
Thread t = new Thread(ts);
t.Start();

Upvotes: 4

Related Questions