Windows窗体加载然后退出

时间:2012-04-28 11:56:57

标签: c# winforms wcf

我正在为超市创建一个结账系统。它由结账,服务器和MIS程序组成,它们之间运行WCF服务。我遇到的问题是结帐程序,它是一个Windows窗体,在它的application_load方法中做了一些必要的事情,然后就退出了。

以下是代码:

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);
        }
    }
}

和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());
        }
    }
}

感谢您的任何见解。

1 个答案:

答案 0 :(得分:4)

在WinForms中,如果你的form_load抛出异常,它将退出而不显示任何内容。很烦人,但我猜这就是问题所在。

您可以尝试使用try/catch,也可以点击CTRL+ALT+E并查看Thrown Column Common Language Runtime Exceptions以查看错误。

更新:

根据评论,这是在另一个线程上执行某些操作的示例方法。

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