Referenced class won't let go of file [modified]
-
I have a class setup that sends an e-mail using SMTP. This class is setup to be used by other programs using it as a reference. I have a program that creates a file and then calls this class to send the e-mail with the attachment. After the class sends the file and returns a boolean of True to tell the calling program it succeeded, the original program is supposed to delete the original file. Unfortunately the calling program seems to still be holding onto the file. Below is snippits of the code from the e-mail class. How can I get this class to let go of any attachments that it works with? Everything is working fine, it sends the e-mail, etc, but won't let go. ... Dim myAttachment As System.Net.Mail.Attachment = New System.Net.Mail.Attachment("c:\test.txt") Message.Attachments.Add(myAttachment) ... Dim SmtpMail As New SmtpClient SmtpMail.Host = "SMTP.Mydomain.com" SmtpMail.Credentials = New System.Net.NetworkCredential("myUsername", "myPassword") SmtpMail.Send(Message) Return True I have a try catch that will send false if the e-mail fails. I tried this code but it didn't seem to help: SmtpMail = Nothing Message = Nothing Any help / suggestions are appreciated. :confused:
Lost in the vast sea of .NET
modified on Thursday, June 19, 2008 9:44 AM
-
I have a class setup that sends an e-mail using SMTP. This class is setup to be used by other programs using it as a reference. I have a program that creates a file and then calls this class to send the e-mail with the attachment. After the class sends the file and returns a boolean of True to tell the calling program it succeeded, the original program is supposed to delete the original file. Unfortunately the calling program seems to still be holding onto the file. Below is snippits of the code from the e-mail class. How can I get this class to let go of any attachments that it works with? Everything is working fine, it sends the e-mail, etc, but won't let go. ... Dim myAttachment As System.Net.Mail.Attachment = New System.Net.Mail.Attachment("c:\test.txt") Message.Attachments.Add(myAttachment) ... Dim SmtpMail As New SmtpClient SmtpMail.Host = "SMTP.Mydomain.com" SmtpMail.Credentials = New System.Net.NetworkCredential("myUsername", "myPassword") SmtpMail.Send(Message) Return True I have a try catch that will send false if the e-mail fails. I tried this code but it didn't seem to help: SmtpMail = Nothing Message = Nothing Any help / suggestions are appreciated. :confused:
Lost in the vast sea of .NET
modified on Thursday, June 19, 2008 9:44 AM
Does it implement the IDisposable interface? If yes, then you have to call Disposr on it.
Recent blog posts: * Introduction to LINQ to XML (Part 1) - (Part 2) - (part 3) My website | Blog
-
Does it implement the IDisposable interface? If yes, then you have to call Disposr on it.
Recent blog posts: * Introduction to LINQ to XML (Part 1) - (Part 2) - (part 3) My website | Blog
No I'm not. I had a similar class doing the same type work in VS 2003 and had no problems with the class hanging onto the attachment, but it seems like the same type of class written in VS 2005 2.0 is not letting go. Is the IDisposable interface a 2005 feature and since I don't have it implemented in this project do you feel I should? If so, any advise. On a google search it seems like there is a wrong way people implement in and a right way. Any suggestions on a good place to research this interface? Thanks :)
Lost in the vast sea of .NET
-
I have a class setup that sends an e-mail using SMTP. This class is setup to be used by other programs using it as a reference. I have a program that creates a file and then calls this class to send the e-mail with the attachment. After the class sends the file and returns a boolean of True to tell the calling program it succeeded, the original program is supposed to delete the original file. Unfortunately the calling program seems to still be holding onto the file. Below is snippits of the code from the e-mail class. How can I get this class to let go of any attachments that it works with? Everything is working fine, it sends the e-mail, etc, but won't let go. ... Dim myAttachment As System.Net.Mail.Attachment = New System.Net.Mail.Attachment("c:\test.txt") Message.Attachments.Add(myAttachment) ... Dim SmtpMail As New SmtpClient SmtpMail.Host = "SMTP.Mydomain.com" SmtpMail.Credentials = New System.Net.NetworkCredential("myUsername", "myPassword") SmtpMail.Send(Message) Return True I have a try catch that will send false if the e-mail fails. I tried this code but it didn't seem to help: SmtpMail = Nothing Message = Nothing Any help / suggestions are appreciated. :confused:
Lost in the vast sea of .NET
modified on Thursday, June 19, 2008 9:44 AM
Its seesm to me that you are waiting on the Garbage collector to dispose of myAttachment If you need to release it sooner then you should explicity release the resourse either with myAttachment = nothing or if it is disposable myAttachment.dispose Remember that "return True" will cease all execution in the code block so anycode after "return" is not called so dispose of resources before you return. The other option is use the Try 'Code to Try Catch ' Error code Finally ' Clean up code ' You cant call return in here ' Also be carefull with dispose object that may not have been created as can end up will null reference errors End try
-
No I'm not. I had a similar class doing the same type work in VS 2003 and had no problems with the class hanging onto the attachment, but it seems like the same type of class written in VS 2005 2.0 is not letting go. Is the IDisposable interface a 2005 feature and since I don't have it implemented in this project do you feel I should? If so, any advise. On a google search it seems like there is a wrong way people implement in and a right way. Any suggestions on a good place to research this interface? Thanks :)
Lost in the vast sea of .NET
KreativeKai wrote:
Is the IDisposable interface a 2005 feature
No, it has been around since .NET 1.0
KreativeKai wrote:
since I don't have it implemented in this project do you feel I should?
I don't know, I am merely suggesting paths of investigation. I am saying that if the class that is holding on to the file (a class in the .NET framework I would presume) AND it implements
IDisposable
then you should call theDispose()
method it implements.KreativeKai wrote:
On a google search it seems like there is a wrong way people implement in and a right way. Any suggestions on a good place to research this interface?
I never asked you to implement the interface. I said if it was already implemented by the framework classes that you are using then you should call
Dispose()
.Recent blog posts: * Introduction to LINQ to XML (Part 1) - (Part 2) - (part 3) My website | Blog
-
Its seesm to me that you are waiting on the Garbage collector to dispose of myAttachment If you need to release it sooner then you should explicity release the resourse either with myAttachment = nothing or if it is disposable myAttachment.dispose Remember that "return True" will cease all execution in the code block so anycode after "return" is not called so dispose of resources before you return. The other option is use the Try 'Code to Try Catch ' Error code Finally ' Clean up code ' You cant call return in here ' Also be carefull with dispose object that may not have been created as can end up will null reference errors End try
I tried both ways to release the hold on the attachment with no luck. What is wierd is I have a class doing the exact same process in the 1.1 framework using System.Web.Mail and when it returns true to the calling program there is no problem with the attachment not being released. When I use my framework 2.0 class using System.Net.Mail it doesn't let go of the file once it returns true to the calling program. Any suggestions are appreciated :confused:
Lost in the vast sea of .NET
-
KreativeKai wrote:
Is the IDisposable interface a 2005 feature
No, it has been around since .NET 1.0
KreativeKai wrote:
since I don't have it implemented in this project do you feel I should?
I don't know, I am merely suggesting paths of investigation. I am saying that if the class that is holding on to the file (a class in the .NET framework I would presume) AND it implements
IDisposable
then you should call theDispose()
method it implements.KreativeKai wrote:
On a google search it seems like there is a wrong way people implement in and a right way. Any suggestions on a good place to research this interface?
I never asked you to implement the interface. I said if it was already implemented by the framework classes that you are using then you should call
Dispose()
.Recent blog posts: * Introduction to LINQ to XML (Part 1) - (Part 2) - (part 3) My website | Blog
Disposing didn't help? I used an article from MSDN that showed how to use it. I'm not sure if this was the best way to handle the idisposable interface or not? http://msdn.microsoft.com/en-us/library/s9bwddyx(VS.80).aspx[^] If you have any other suggestions I would be interested in hearing? :confused:
Lost in the vast sea of .NET