使用 Visual Studio 进行调试:执行重循环时程序崩溃
我目前正在尝试提取用 GZip 压缩的文件。 我想在使用 Windows Mobile 6 的移动设备上以编程方式执行此操作。 我使用 Compact Framework 3.5 和 C#。
压缩后的文件大小约为4MB。原始文件(文本文件)的大小约为 42 MB。 我的算法在提取过程中崩溃了。每次解压后的文件大小达到33.2MB左右时就会崩溃。
我的算法是否会因为文件大小限制而崩溃? 我知道紧凑框架的内存限制约为 32 MB,但鉴于我使用缓冲区来提取文件,不应满足此内存限制。 我说得对吗?
算法如下:
private void Extract(string GZippedFile, string TargetFile)
{
int BUFFER_SIZE = 32768;
using (FileStream InStream = new FileStream(GZippedFile, FileMode.Open, FileAccess.Read))
{
using (GZipStream GZipStream = new GZipStream(InStream, CompressionMode.Decompress))
{
using (FileStream OutStream =
new FileStream(TargetFile, FileMode.Create, FileAccess.Write))
{
byte[] tempBytes = new byte[BUFFER_SIZE];
int i;
while ((i = GZipStream.Read(tempBytes, 0, tempBytes.Length)) != 0)
{
OutStream.Write(tempBytes, 0, i);
}
}
}
}
}
此外,通常,当 Windows Mobile 上达到内存限制时,会显示一条系统消息。当该程序崩溃时,情况并非如此。
此外,设备上还有足够的存储内存和程序内存(通过系统上的“设置”>“内存”显示)。
你有什么想法吗?
编辑:抱歉,我想我错了。事实上,只有当我用Visual Studio调试程序时,才会出现这个问题。我正在使用 TCP/Connect 来链接 Visual Studio 和我的设备。
我认为真正的问题如下:
文件提取占用大量设备资源;
因为设备太忙,无法与 Visual Studio 保持 tcp 连接;
Visual Studio 与设备之间的连接丢失,这使得整个程序崩溃,并且不会向设备上的用户发出任何警告(在运行 Visual Studio 的 PC 上,会显示一条消息,警告连接已断开)丢失)。
当我不使用 Visual Studio 调试器时,一切正常。
编辑: 由于 cackle 评论,该问题不再出现。 提示是执行
System.Threading.Thread.Sleep(1);
这条指令,在循环期间为 Visual Studio 提供句柄,并防止程序崩溃。
I am currently trying to extract a file compressed with GZip.
I want to do it programmaticaly on a mobile device using Windows Mobile 6.
I use the Compact Framework 3.5, and C#.
The compressed file size is about 4 MB. And the size of the original file (which is a text file) is about 42 MB.
My algorithm crashes during the extraction. Each time, it crashes when the decompressed file size reachs about 33.2 MB.
Does my algorithm crash because of a file size limit ?
I know that the compact framework has a memory limit of about 32 MB, but this memory limit should not be met, given that i use buffers to extract the file.
Am i right ?
Here is the algorithm :
private void Extract(string GZippedFile, string TargetFile)
{
int BUFFER_SIZE = 32768;
using (FileStream InStream = new FileStream(GZippedFile, FileMode.Open, FileAccess.Read))
{
using (GZipStream GZipStream = new GZipStream(InStream, CompressionMode.Decompress))
{
using (FileStream OutStream =
new FileStream(TargetFile, FileMode.Create, FileAccess.Write))
{
byte[] tempBytes = new byte[BUFFER_SIZE];
int i;
while ((i = GZipStream.Read(tempBytes, 0, tempBytes.Length)) != 0)
{
OutStream.Write(tempBytes, 0, i);
}
}
}
}
}
Besides, usually, when the memory limit is reached on Windows Mobile, a system message is displayed. This is not the case when this program crashes.
Also, there is enough storage memory and program memory left on the device (displayed through Settings>>Memory on the system).
Do you have any idea ?
EDIT: Sorry, I think i was wrong. In fact, this problem happened only when i am debugging the program with Visual Studio. And i am using TCp/Connect to link Visual Studio and my device.
I think the real problem is the following :
the file extraction take a lot of the device ressources ;
because the device is too busy, it can't maintain the tcp connection with Visual Studio;
the connection between Visual Studio and the device is lost, which make the entire program to crash without any warning to the user on the device (on the PC where Visual Studio is running, a message is displayed, warning that the connection was lost).
When i don't use the visual studio debugger, everything works just fine.
EDIT :
Because of the ctackle comment, the problem does not appear anymore.
The tip is to execute a
System.Threading.Thread.Sleep(1);
This instruction give the handle to visual studio during the loop and prevent the program from crashing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许考虑使用第三方工具?
http://www.icsharpcode.net/OpenSource/SharpZipLib/
Maybe consider using a 3rd party tool?
http://www.icsharpcode.net/OpenSource/SharpZipLib/