4

我正在为一家超市创建一个结帐系统。它由结帐、服务器和 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());
        }
    }
}

感谢您的任何见解。

4

1 回答 1

4

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

您可以尝试 a try/catch,或者您可以点击CTRL+ALT+E并检查Thrown ColumnforCommon Language Runtime Exceptions以查看错误。

更新:

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

ThreadStart ts = new ThreadStart(() => {
   try {
       scanner.Start(checkoutID);
   } catch {
       // Log error
   }
});
Thread t = new Thread(ts);
t.Start();
于 2012-04-28T12:00:54.040 回答