C# Windows窗体加载后退出

C# Windows窗体加载后退出,c#,winforms,wcf,C#,Winforms,Wcf,我正在为一家超市创建一个结账系统。它由一个签出、服务器和MIS程序组成,并在它们之间运行WCF服务。我遇到的问题是,签出程序是一个windows窗体,它在应用程序加载方法中执行了一些必要的操作,然后就退出了 代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; usi

我正在为一家超市创建一个结账系统。它由一个签出、服务器和MIS程序组成,并在它们之间运行WCF服务。我遇到的问题是,签出程序是一个windows窗体,它在应用程序加载方法中执行了一些必要的操作,然后就退出了

代码如下:

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());
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows.Forms;
命名空间签出
{
静态类程序
{
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
运行(newtheform());
}
}
}

感谢您的帮助。

在WinForms中,如果您的表单加载引发异常,它将退出而不显示任何内容。很烦人,但我猜这就是问题所在

您可以尝试使用
try/catch
,也可以点击
CTRL+ALT+E
并检查
抛出列中的
公共语言运行时异常
,以查看错误

更新:

根据注释,下面是在另一个线程上执行某些操作的示例方法

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

啊,我确实想到了,但我期待着一个未处理的异常。我查一下,谢谢。我怀疑它在尝试初始化其中一个服务时可能引发异常。Try-Catch未发现任何异常。事实上,catch块中的断点也没有被触发。嗯……第一行代码上的断点真的被击中了吗?当我注释出scanner.Start(x)时,检查CTRL+ALT+E(也可以在调试/异常菜单上找到)中的“抛出”列是否幸运;打电话的形式是好的。我假设这必须在一个单独的线程上调用,因为在某个点上它关闭了表单操作的同一个线程?很难从我的角度判断scanner.Start在做什么。但是,如果它是一个阻塞调用,您可能希望将其包装在一个线程中。我将用您可以尝试的语法更新我的答案。您能显示打开表单(show/showdialog)的代码吗?如中所示,program.cs的内容?在@bugfixr answer中查看您的注释不再需要了。很明显,你的表格显示。因此,现在尝试在scanner\u allitemscanned事件中设置断点。只是为了确保扫描程序启动时不会执行某些事件。该窗体上是否有数据绑定控件?