【问题标题】:C# WPF app hangs in debug mode, works perfect in VS 2015 breakpoint modeC# WPF 应用程序在调试模式下挂起,在 VS 2015 断点模式下完美运行
【发布时间】:2016-09-12 17:35:15
【问题描述】:

我在 Visual Studio 2015 中创建了 C# Windows 窗体应用程序。应用程序用于计算一些数据,然后将其分配给预定义的 int 变量并在输出控制台中显示它们。

问题是应用程序在断点模式下遍历所有代码而没有错误,直到整个代码结束并在控制台输出中给我我想要的。但是,如果我通过调试模式(F5)甚至没有调试模式(Ctrl+F5)运行程序,那么我没有输出并且程序挂起。一段时间后,它会引发 ContentDeadlockException。

感谢您的回答。

编辑:

好的,这是我的应用程序未完成的 GUI:picture 当我单击“运行算法”按钮时,程序在调试模式下挂起。如果我尝试在断点模式下进行调试,请在语句后运行语句,它会正常运行。

有我的那种形式的代码:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication3 { public partial class IterationAlgorithm : Form { //Random random; List<IZadPrac> workersList = new List<IZadPrac>(); List<IZadPrac> jobsList = new List<IZadPrac>(); public bool[,] generateTable(bool[,] tableGiven) { bool isRight = false; while (!isRight) { Array.Clear(tableGiven, 0, tableGiven.Length); for (int i = 0; i < tableGiven.GetLength(0); i++) { Random random = new Random(); tableGiven[i, random.Next(tableGiven.GetLength(1))] = true; } int jobsTimeSum; for (int i = 0; i < workersList.Count; i++) { jobsTimeSum = 0; for (int j = 0; j < jobsList.Count; j++) { if (tableGiven[j, i]) jobsTimeSum += jobsList[j].showJobTime(); if (workersList[i].showWorkerTime() < jobsTimeSum) { j = jobsList.Count; i = workersList.Count; isRight = false; } else { isRight = true; } } } } return tableGiven; } public int FindFunctionMin(bool[,] tableGiven) { int goalFunction = 0; for(int i = 0; i < jobsList.Count; i ++) { for(int j = 0; j < workersList.Count; j++) { if (tableGiven[i, j]) { goalFunction += workersList[j].showCompetitions()[i] * jobsList[i].showJobTime(); j = workersList.Count; } } } return goalFunction; } public IterationAlgorithm(List<IZadPrac> ListaPrac, List<IZadPrac> ListaZad) { InitializeComponent(); this.workersList = ListaPrac; this.jobsList = ListaZad; bool[,] boolTable = new bool[jobsList.Count, workersList.Count]; Array.Clear(boolTable, 0, boolTable.Length); //List<bool[,]> listOfTables = new List<bool[,]>(); //int counter = 0; //int goalFunctionTemp = 0; List<int> funkcjaCelu = new List<int>(); List<IZadPrac> workListCopy = new List<IZadPrac>(); List<IZadPrac> jobListCopy = new List<IZadPrac>(); int goalFunctionTemp = 999999; workListCopy = jobsList; jobListCopy = workersList; } private void button1_Click(object sender, EventArgs e) { bool[,] booltable = new bool[jobsList.Count, workersList.Count]; Array.Clear(booltable, 0, booltable.Length); int counter = 0; int max; int functionmin = 999999; bool error = false; List<int> funkcjaCelu = new List<int>(); if(textBox1.Text != String.Empty) { max = Int32.Parse(textBox1.Text); while (counter < max) { int number = FindFunctionMin(generateTable(booltable)); if (functionmin > number) functionmin = number; counter++; } label2.Text = "The smallest value of function " + functionmin.ToString(); } } } }

当我在调试模式下按下暂停按钮时,然后 breapoint 处于“GenerateTable”功能。

感谢您的回答。

【问题讨论】:

  • 给我们一些代码。这就像在黑暗中射击。据我所知,您的应用程序中有并行线程的竞争条件

标签: c# windows forms debugging exception


【解决方案1】:

好的,我找到了解决方案...我的 Random 实例在 for 循环中创建得太快了,所以我将它移到了 while 循环之上,程序现在可以正常工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-04
    • 2017-03-15
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 2020-08-22
    相关资源
    最近更新 更多