我的应用程序出了什么问题 ---- 大小为 0,但我预期为 46806!

发布于 2024-09-13 07:37:21 字数 1896 浏览 11 评论 0原文

我是一名 C# 程序员。

现在,我使用 ICSharpCode.SharpZipLib.dll 在当前项目中创建一个 zip 文件。但我突然想到,当我在第二次处单击按钮执行创建zip文件的函数时,应用程序会抛出异常,友好而严肃地告诉我“大小为零,但我预计是 46086”。

我很困惑,我想知道为什么?当我第一次点击按钮时,我可以成功完成,没有任何错误。

我的相关代码如下:

internal void ThreadProc()
{
    try
    {
        ZipHelper.CreateZip(backupZipFile, Constants.HomeConstant, true);

        // do other things
    }

}

CreateZip()函数的实现如下:

public static void CreateZip(string zipFileName, string sourceDirectory, bool recurse)
{
 FastZip zip = new FastZip();

 if (File.Exists(zipFileName))
 {
         File.Delete(zipFileName);
 }

 zip.CreateZip(zipFileName, sourceDirectory, true, "");
}

现在给大家展示一下递归调用过程:

  1. 调用“ActiveCheckManager”类中的“UpdateAppAsync”方法
public void UpdateAppAsync(string masterConfig)
{
    this.masterConf = masterConfig;

    线程实际线程 = new Thread(new ThreadStart(UpdateApp));
    实际线程.IsBackground = true;
    实际线程.CurrentCulture = 线程.CurrentThread.CurrentCulture;
    实际线程.CurrentUICulture = 线程.CurrentThread.CurrentUICulture;
    实际线程.Start();
}
  1. 异步调用UpdateApp函数,在UpdateApp方法中,只会简单的调用UpdateDetail函数。
private void UpdateDetail(字符串 masterConfig, 字符串类别)
{
    IUpdate 工作者 = new HP.ActiveCheckLocalMode.UpdateEngine.UpdateManager();
    Worker.UpdateApp(masterConf);
}
  1. worker.UpdateApp 将仅调用 UpdateDetail(string, UpdateCategory) 。
private void UpdateDetail(string masterConfig, UpdateCategory cat)
{
    UpdateThread updateThread = new UpdateThread(this, cat);
    updateThread.MasterConfig = masterConfig;
    updateThread.ThreadProc();
}

这就是调用过程。当我第二次点击更新按钮时,它会抛出异常,你能帮我吗?非常感谢。

I'm a C# programmer.

Now, I'm using the ICSharpCode.SharpZipLib.dll to create a zip file in my current project. But it occurs to me that when I click the button at the SECOND TIME to execute a function to create a zip file, the application will throw an exception, friendly and seriously told me that "Size was zero, but I expected 46086".

I'm so confused that I want to know why? When I click the button at the first time, I can do it successfully without any error.

My related codes are as follows:

internal void ThreadProc()
{
    try
    {
        ZipHelper.CreateZip(backupZipFile, Constants.HomeConstant, true);

        // do other things
    }

}

The CreateZip() function's realization is as follows:

public static void CreateZip(string zipFileName, string sourceDirectory, bool recurse)
{
 FastZip zip = new FastZip();

 if (File.Exists(zipFileName))
 {
         File.Delete(zipFileName);
 }

 zip.CreateZip(zipFileName, sourceDirectory, true, "");
}

Now, I will show you the recursive calling process:

  1. Call method "UpdateAppAsync" in "ActiveCheckManager" class
public void UpdateAppAsync(string masterConfig)
{
    this.masterConf = masterConfig;

    Thread actualThread = new Thread(new ThreadStart(UpdateApp));
    actualThread.IsBackground = true;
    actualThread.CurrentCulture = Thread.CurrentThread.CurrentCulture;
    actualThread.CurrentUICulture = Thread.CurrentThread.CurrentUICulture;
    actualThread.Start();
}
  1. Call the UpdateApp function asynchronously, in the UpdateApp method, it will only call the UpdateDetail function simply.
private void UpdateDetail(string masterConfig, string category)
{
    IUpdate worker = new HP.ActiveCheckLocalMode.UpdateEngine.UpdateManager();
    worker.UpdateApp(masterConf);
}
  1. The worker.UpdateApp will call UpdateDetail(string, UpdateCategory) only.
private void UpdateDetail(string masterConfig, UpdateCategory cat)
{
    UpdateThread updateThread = new UpdateThread(this, cat);
    updateThread.MasterConfig = masterConfig;
    updateThread.ThreadProc();
}

That is the calling process. When I click the update button second time, it will throw an exception, can you help me? Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙气飘飘 2024-09-20 07:37:21

在第二次开始之前第一个任务线程是否已完成?

我想象 File.Delete() 和 SharpZipLib 中的一些项目不能很好地响应多线程同时将同一文件夹压缩到同一文件。

将该“UpdateThread updateThread”提升为“ActiveCheckManager”类的私有成员,然后在创建新线程之前检查它是否已从先前的单击中运行。

Has the first task thread finished before you start the second time?

I would imagine that File.Delete() and some items in the SharpZipLib to not respond nicelly to multithreadingly zip the same folder simultaneously to the same file.

Promote that " UpdateThread updateThread " as a private member of the "ActiveCheckManager" class, then check if it is already running from a previous click before creating a new thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文