Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Referenced class won't let go of file [modified]

Referenced class won't let go of file [modified]

Scheduled Pinned Locked Moved Visual Basic
questioncsharpcomhelpworkspace
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KreativeKai
    wrote on last edited by
    #1

    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

    C T 2 Replies Last reply
    0
    • K KreativeKai

      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

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      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

      K 1 Reply Last reply
      0
      • C Colin Angus Mackay

        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

        K Offline
        K Offline
        KreativeKai
        wrote on last edited by
        #3

        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

        C 1 Reply Last reply
        0
        • K KreativeKai

          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

          T Offline
          T Offline
          TheFarsider
          wrote on last edited by
          #4

          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

          K 1 Reply Last reply
          0
          • K KreativeKai

            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

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            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 the Dispose() 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

            K 1 Reply Last reply
            0
            • T TheFarsider

              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

              K Offline
              K Offline
              KreativeKai
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • C Colin Angus Mackay

                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 the Dispose() 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

                K Offline
                K Offline
                KreativeKai
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups