English 中文(简体)
What s wrong with my application ---- Size was 0, but I expected 46806 !
原标题:

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.

问题回答

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签