【发布时间】:2013-08-03 03:48:39
【问题描述】:
我正在创建一个从文件中读取信息并显示从所述文件中提取的图像的工具。它可以很好地读取信息和所有内容,但是在显示图像时它会冻结程序而不会出现错误。该程序只是变得没有响应。调试器什么也没说,除非过了一段时间它会说线程已经退出,但程序仍然没有响应。
我需要重命名一些东西,因为我不想惹上麻烦
这是我用来提取和显示图像的代码:
try
{
global.meta.filetype = STFSRead.readString_C(0, 4);
textBox2.Text = global.meta.filetype;
textBox3.Text = STFSRead.detectType(global.meta.contype.ToString("X4"));
textBox4.Text = global.meta.metaversion.ToString();
textBox5.Text = global.meta.id1;
textBox6.Text = global.meta.version.ToString("X");
textBox7.Text = global.meta.version2.ToString("X");
textBox8.Text = global.meta.id2;
textBox9.Text = global.meta.id3;
textBox10.Text = global.meta.id4;
textBox11.Text = global.meta.id5;
textBox12.Text = global.meta.displayname;
textBox13.Text = global.meta.titlename;
textBox14.Text = STFSRead.detectSomeInfo(global.meta.aflag.ToString("X2"));
pictureBox1.Image = STFSRead.loadImage();
}
catch
{
throw new Exception("What did you do?\n All this is suppose to do is read a file, how did you screw that up?");
}
public static Image loadImage()
{
Exception failed = new Exception("LoadImage failed. Contact a developer");
string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "/appname/cache/";
string imgname = global.meta.id.Replace(" ", "") + ".png";
if (Directory.Exists(@path))
{
if (File.Exists(@path + imgname))
{
using (Image img = Image.FromFile(@path + imgname))
{
return img;
}
throw failed;
}
else if (!File.Exists(@path + imgname))
{
if (extractData(imgname, 0x171A, 0x4000))
{
using (Image img = Image.FromFile(@path + imgname))
{
return img;
}
throw failed;
}
else
throw failed;
}
else
throw failed;
}
else if(!Directory.Exists(@path)){
Directory.CreateDirectory(@path);
if (File.Exists(@path + imgname))
{
using (Image img = Image.FromFile(@path + imgname))
{
return img;
}
throw failed;
}
else if (!File.Exists(@path+imgname))
{
if (extractData(imgname, 0x171A, 0x4000))
{
using (Image img = Image.FromFile(@path + imgname))
{
return img;
}
throw failed;
}
else
throw failed;
}
else
throw failed;
}
else
throw failed;
}
public static bool extractData(string filename, long startpos, long length)
{
string data = STFSRead.readString_A(startpos, length);
string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)+"/appname/cache/";
if (Directory.Exists(@path))
{
using (StreamWriter file = new StreamWriter(@path + filename))
{
file.Write(data);
file.Close();
}
}
else if (!Directory.Exists(@path))
{
Directory.CreateDirectory(@path);
using (StreamWriter file = new StreamWriter(@path + filename))
{
file.Write(data);
file.Close();
}
}
if (File.Exists(@path + filename))
return true;
else
throw new Exception("Couldn't extract file "+filename+" at location "+startpos+" with a length of "+length);
}
public static string readString_A(long startpos, long length)
{
string s = "";
for (long i = startpos; i < startpos + length; i = i++)
{
global.fs.Position = i;
byte[] buf = new byte[1];
global.fs.Read(buf, 0, 1);
if (buf[0] == 0x00) // Null symbol
{
}
else
{
char c = Convert.ToChar(buf[0]);
s += c;
}
}
if (s == "" || s == " ")
{
s = "";
}
return s;
}
我检查了我的 appdata 文件夹,在创建目录时,图像没有创建,所以我认为它在提取过程中失败。我的异常也没有被触发
编辑:关于异常......当程序开始搞砸时,我添加了所有异常,看看它是否会触发它们并缩小错误可能出现的位置。我不希望它处理这些异常。可能不是最好的方法,但我仍在学习 c#,所以我不认为它是最好的方法。如果有更好的方法,请告诉我。
【问题讨论】:
-
这很可能是我见过的最糟糕的异常/错误处理代码。我意识到这并不完全有建设性或有帮助,但是……哇。声称程序在这样的情况下“没有错误”挂起并不完全可靠。当你调试这个时,它哪里出错了?是否有系统挂起的特定声明?一些无限的逻辑路径?一些未处理的异常?
-
我不知道他在说什么 7
-
请提供更多关于错误或异常的详细信息,以便大家提供帮助..
-
@David,我知道它有一些糟糕的异常处理......主要是因为我不希望它处理它们。我添加了所有这些异常以尝试让它触发它们,以便我可以看到错误更可能发生的位置。至于调试器是否说什么,没有。没有说什么意外。当我尝试加载图像时,程序变得无响应。它会读取其他所有内容,因为我让它在调试器中输出这些内容,但在它到达图像时停止。
-
@user1425470:显然,这种“查看错误更可能发生的位置”的方法不起作用。这种方法在许多层面上都存在缺陷。更重要的是,当您在调试器中逐行执行此操作时,它在哪里失败?不要只运行整个应用程序并依赖此“错误处理”代码。实际上在调试器中单步执行代码,看看它在哪里失败。如果它真的“只是变得没有响应”并且从未真正到达任何此代码,那么问题就必须在其他地方。如果确实到达此代码,请在调试器中单步执行并查找失败的位置。
标签: c#