Jump to content

Recommended Posts

Hey, I'm having some issues handling a form closing event in C#.  I've added this to my designer.cs file:

 

this.Closing += new System.ComponentModel.CancelEventHandler(this.Form_Closing);

 

And this is my Form_Closing() method:

 

public void Form_Closing(object sender, CancelEventArgs cArgs)
{
    if (Directory.Exists("./thumbs"))
    {
        Directory.Delete("./thumbs", true);
    }
}

 

The problem is, I'm using files inside the ./thumbs/ directory within the program, so when I try to delete that folder on form close, it gives me an error that a file within the folder is within use.  I've tried also using this.Closed and it doesn't do anything either, same issue.  The files within the folder are images used in a DataGridViewImageCell.  I've tried clearing the datagridview before I delete and that doesn't work either, all the files in there are locked to the process.

 

Is there any way I can delete these files when the program closes?  There has to be SOME way.

Link to comment
https://forums.phpfreaks.com/topic/182479-c-form-closing-event/
Share on other sites

this.Closed += System.eventhandler(this.Form_Closed);

 

Yah, I tried that one, same problem.  And by clear I meant that I did DataGridView1.Rows.Clear();, should I try dispose?  What would I dispose of?  The Datagridview?  I'll try it and see what I get.

Link to comment
https://forums.phpfreaks.com/topic/182479-c-form-closing-event/#findComment-963731
Share on other sites

Ok, I've tried DataGridView1.Dispose(); and it's still giving me a issue with file associations.

 

Maybe it'd help if I explained what I'm doing:

 

I have a method that fetches an XML file based on a GET variable in the URL (an Xbox Live gamertag), and parses it for some information.  There are 30 <item> tags within the XML and inside each of those I need 5 pieces of information: the title, pubdate, description, url to a thumbnail image, and url for the full-sized image.  I also have a method to download the thumbnail image I pulled out of the XML for each item.

 

So what I'm doing is parsing each of those <item>'s out of the XML using XmlNodeList, then using a foreach loop to iterate them.  Inside that foreach loop I use another XmlNodeList with the value of item.ChildNodes to get all 5 pieces of information and I save them to string variables.  Then I start a new thread:

 

Thread t = new Thread(downloadThumb);

t.start(thumbUrl);

 

To download the thumbnail images to a folder called ./thumbs/.  I use t.Join(); to pause the code until the thread is complete, and when it is I create a new row for my datagridview and add 2 new cells to that new row, one a checkbox and the other an imageCell.  I use the thumbnail I just downloaded to fill in the imageCell.

 

So, knowing this, how can I dispose of this properly so that I can delete the folder?  When I try closing the program in debug mode (within Visual Studio) the program just won't close at all, if I run it from the /bin/debug folder of my project and try to close it it returns an error:

 

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.IO.IOException: The process cannot access the file '94358334-Thumbnail.jpg' because it is being used by another process.
   at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive)
   at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive)
   at WindowsFormsApplication1.Form1.Form_Closing(Object sender, CancelEventArgs cArgs) in E:\Misc\CS Programming\H3ScreenshotDownloader\H3ScreenshotDownloader\Form1.cs:line 138
   at System.Windows.Forms.Form.OnClosing(CancelEventArgs e)
   at System.Windows.Forms.Form.WmClose(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v2.0.50727/mscorlib.dll
----------------------------------------
H3ScreenshotDownloader
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///E:/Misc/CS%20Programming/H3ScreenshotDownloader/H3ScreenshotDownloader/bin/Debug/H3ScreenshotDownloader.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Xml
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Configuration
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
    CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

 

It makes me feel like the program is tied to the file, not the directory, since the error returned says that the process cannot access the file.  Anyone have any advice?

Link to comment
https://forums.phpfreaks.com/topic/182479-c-form-closing-event/#findComment-964523
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.