HaLo2FrEeEk Posted November 22, 2009 Share Posted November 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/182479-c-form-closing-event/ Share on other sites More sharing options...
cags Posted November 22, 2009 Share Posted November 22, 2009 When you say you tried clearing datagridview what exactly do you mean? Did you Dispose() of it? Also from what I remember theres a FormClosed event, perhaps you'll have more luck with that. Quote Link to comment https://forums.phpfreaks.com/topic/182479-c-form-closing-event/#findComment-963137 Share on other sites More sharing options...
HaLo2FrEeEk Posted November 23, 2009 Author Share Posted November 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/182479-c-form-closing-event/#findComment-963731 Share on other sites More sharing options...
cags Posted November 23, 2009 Share Posted November 23, 2009 Calling Dispose on any object which uses resources stored in the folder should enable you to then delete the folder because it should clear up all attachments from you application to the file. Quote Link to comment https://forums.phpfreaks.com/topic/182479-c-form-closing-event/#findComment-963872 Share on other sites More sharing options...
HaLo2FrEeEk Posted November 24, 2009 Author Share Posted November 24, 2009 Would that be something like datagridview1.dispose()? I've never used dispose before, so I'm not sure how to do it or what to search for. Quote Link to comment https://forums.phpfreaks.com/topic/182479-c-form-closing-event/#findComment-964501 Share on other sites More sharing options...
HaLo2FrEeEk Posted November 24, 2009 Author Share Posted November 24, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/182479-c-form-closing-event/#findComment-964523 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.