【问题标题】:C# / Windows Forms: application runs but unresponsiveC#/Windows 窗体:应用程序运行但无响应
【发布时间】:2009-11-30 06:36:54
【问题描述】:

所以我一直在把这个图形转换程序放在一起,突然之间我无法弄清楚的一些变化使应用程序没有响应。菜单不再起作用,它应该在其中一个面板上绘制轴和网格......什么都没有。有什么想法吗?

 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 System.IO;

namespace Transformer
{
    public partial class Transformer : Form
    {
        /* Initialize parameters */
        private bool drawAxes = true;
        private bool drawGrid = true;

        private List<ObjectSettings> dispObjects = new List<ObjectSettings>();


        /* Initialize form */

        public Transformer()
        {
            InitializeComponent();
        }

        private void Transformer_Load(object sender, EventArgs e)
        {
            // Populate available objects listbox
            string currentDir = Directory.GetCurrentDirectory();
            string[] fileEntries = Directory.GetFiles(currentDir + @"\Objects");
            foreach (string s in fileEntries) {
                int start = s.LastIndexOf(@"\");
                int end = s.LastIndexOf(@".");
                availObjectsListBox.Items.Add(s.Substring(start + 1, end - start - 1));
            } // end foreach
        }



        /* Paint graphics */

        // Paint main form
        private void Transformer_Paint(object sender, PaintEventArgs e)
        {
        }

        // Paint graphics panel
        private void splitContainer2_Panel1_Paint(object sender, PaintEventArgs e)
        {
            Rectangle r = splitContainer2.Panel1.ClientRectangle;
            Graphics g = splitContainer2.Panel1.CreateGraphics();
            Pen axisPen = new Pen(Color.Gray, 2.0f);
            Pen gridPen = new Pen(Color.Gray, 1.0f);

            g.Clear(Color.White);

            if (drawAxes) {
                g.DrawLine(axisPen, r.Left + 0.5f * r.Width, r.Top, r.Left + 0.5f * r.Width, r.Bottom);
                g.DrawLine(axisPen, r.Left, r.Top + 0.5f * r.Height, r.Right, r.Top + 0.5f * r.Height);
            }

            if (drawGrid) {
                // Vertical lines
                int xVal = 0;
                int xCenter = r.Width / 2;
                g.DrawLine(gridPen, xCenter, r.Top, xCenter, r.Bottom);
                for (int i = 0; i < 10; i++) {
                    xVal += r.Width / 20;
                    g.DrawLine(gridPen, xCenter + xVal, r.Top, xCenter + xVal, r.Bottom);
                    g.DrawLine(gridPen, xCenter - xVal, r.Top, xCenter - xVal, r.Bottom);
                }

                // Horizontal lines
                int yVal = 0;
                int yCenter = r.Height / 2;
                g.DrawLine(gridPen, r.Left, yCenter, r.Right, yCenter);
                for (int i = 0; i < 10; i++) {
                    yVal += r.Height / 20;
                    g.DrawLine(gridPen, r.Left, yCenter + yVal, r.Right, yCenter + yVal);
                    g.DrawLine(gridPen, r.Left, yCenter - yVal, r.Right, yCenter - yVal);
                }
            }



            // foreach object in displayed objects
            // keep list of displayed objects and their settings (make struct)


            g.Dispose();
            axisPen.Dispose();
            gridPen.Dispose();
        }


        /* File menu */

        private void saveImageToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }


        /* Options menu */

        private void axesOnoffToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (drawAxes == true)
                drawAxes = false;
            else
                drawAxes = true;
        }

        private void gridOnoffToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (drawGrid == true)
                drawGrid = false;
            else
                drawGrid = true;
        }


        /* Help menu */

        private void helpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox dlg = new AboutBox();
            dlg.ShowDialog();
        }


        /* Other stuff */

        private void timer1_Tick(object sender, EventArgs e)
        {
            Invalidate();
        }

        // ">>" button
        private void availToDispButton_Click(object sender, EventArgs e)
        {
            dispObjectsListBox.Items.Add(availObjectsListBox.SelectedItem);
        }

        // "<<" button
        private void dispToAvailButton_Click(object sender, EventArgs e)
        {
            availObjectsListBox.Items.Add(dispObjectsListBox.SelectedItem);
            dispObjectsListBox.Items.Remove(dispObjectsListBox.SelectedItem);
        }

        // Clear all button
        private void clearAllButton_Click(object sender, EventArgs e)
        {

        }

        // Update preview box
        private void availObjectsListBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


    }
}

谢谢!

【问题讨论】:

  • “我无法弄清楚的一些变化导致应用程序无响应”——这就是版本控制的用途。 ;)
  • 如果您还有工作副本的组装,您可以使用反射器工具对其进行逆向工程。比较两者,看看你是否能找到差异。

标签: c# windows forms


【解决方案1】:

尝试(分别)注释掉“加载”和“绘制”代码,看看哪个是问题所在。

如果问题出在油漆上...我想知道-与其创建自己的Graphics,不如使用给您的那个?即e.Graphics。请注意,您没有创建它,因此Dispose() 它不是您的工作(所以不要这样做)。我还将在字段中缓存Pen 等,而不是每次都创建它们。请注意,如果您确实在方法中创建了Pen(等),那么usingDispose() 的更好方法。

在绘制代码中还有一条 foreach 注释表明某些内容已被删除 - 这可能与问题有关...

【讨论】:

  • 负载和绘画似乎都不是问题......甚至菜单事件处理程序也受到影响。它们下拉并看起来被选中,但是当我单击它们时什么也没做
【解决方案2】:

如果在加载时发生这种情况,那么显然目录中的文件数量可能会导致 GUI 线程挂起。

除此之外,我的快速浏览只会让我想到检查您用于控制绘图的布尔值,并确保您使用绘制事件的面板实际上是可见的。

您还应该检查您的计时器是否真的在计时,并检查它的时间间隔。

我还会考虑使用 using 语句,或者至少为您的 dispose 使用 finally 块。但这不是你的问题。

其中大部分是显而易见的,您可能在发布之前已经检查了所有内容,但我想我会提出一些东西以防您错过。希望我有机会更详细地查看并发现其他东西。

【讨论】:

  • 我检查了计时器、能见度等。一切都很好。奇怪的是,菜单会下拉,但点击 File->Exit 什么都不做,因为那里有一个事件处理程序。
  • 事件处理程序接线有问题吗?检查将事件处理程序连接到事件的代码是否仍然存在。如果菜单等工作正常,则意味着 GUI 线程未挂起,因此您可能丢失了所有事件连接。
  • 非常好!嗯,我已经重新启动了这个项目(有一些 copypasta),但你是对的,所以我会给你投票。每个事件连线到底怎么会停止工作?
  • 您使用的是哪个版本的 Visual Studio?我记得在 2003 年有某种错误导致所有事件连线无缘无故消失。我从来没有找到原因。
猜你喜欢
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-25
  • 2016-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多