【问题标题】:Windows form loads then quitsWindows 窗体加载然后退出
【发布时间】:2012-04-28 11:56:56
【问题描述】:

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

感谢您的任何见解。

【问题讨论】:

  • 能否展示打开表单的代码(show/showdialog)?
  • 如中,program.cs的内容?
  • 在@bugfixr 回答中看到你的cmets 没有必要了。很明显,您的表格显示。所以现在尝试在 scanner_AllItemsScanned 事件中放置一个断点。只是为了确保当您的扫描仪启动时不会执行您的某些事件。
  • 该表单上是否有数据绑定控件?

标签: c# winforms wcf


【解决方案1】:

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

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

更新:

基于 cmets,下面是在另一个线程上执行某事的示例方法。

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

【讨论】:

  • 啊,我确实想到了,但我期待一个未处理的异常。我会检查这个,谢谢。我怀疑它在尝试初始化其中一项服务时可能会引发异常。
  • Try-Catch 没有发现任何异常。事实上,catch 块中的断点也没有被触发。嗯..
  • 第一行代码的断点真的被命中了吗?检查 CTRL+ALT+E 中的“Thrown”列(也可以在 Debug/Exceptions 菜单上找到)是否幸运?
  • 当我注释掉scanner.Start(x);调用表格很好。我假设这必须在单独的线程上调用,因为在某些时候它会关闭表单正在运行的同一线程?
  • 我很难说出scanner.Start在做什么。如果它是一个阻塞调用,你可能想把它包装在一个线程中。我会用你可以尝试的语法更新我的答案。
猜你喜欢
  • 2013-04-05
  • 1970-01-01
  • 2010-12-26
  • 2023-02-09
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 2013-04-12
相关资源
最近更新 更多