我在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处于&#34; GenerateTable&#34;功能
感谢您的回答。
答案 0 :(得分:0)
好的我找到了解决方案......我的随机实例在for循环中创建得太快,所以我将它移到了while循环之上,程序现在工作正常。