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. C#
  4. Timer CallBack function

Timer CallBack function

Scheduled Pinned Locked Moved C#
helptutorial
13 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.
  • G Guffa

    There is no guarantee that the finalizer (~) will be run when the application ends. The finalizer is called after that the garbage collector has recognised that the object isn't used any more. An object with a finalizer is placed in a queue where a background thread will be calling the finalizer, but as your object is still used when the application ends, it will never make the queue. As you have a resource that needs to be freed, you should implement the IDisposable interface so that you can call the Dispose method on the object to free the resources. Just as you are calling the Dispose method of the Timer object to make it free it's resources.

    --- single minded; short sighted; long gone;

    A Offline
    A Offline
    aruna_koride
    wrote on last edited by
    #4

    Thanks a lot for prompt replies,I will try to dispose the timer first to stop app from running..

    1 Reply Last reply
    0
    • G Guffa

      There is no guarantee that the finalizer (~) will be run when the application ends. The finalizer is called after that the garbage collector has recognised that the object isn't used any more. An object with a finalizer is placed in a queue where a background thread will be calling the finalizer, but as your object is still used when the application ends, it will never make the queue. As you have a resource that needs to be freed, you should implement the IDisposable interface so that you can call the Dispose method on the object to free the resources. Just as you are calling the Dispose method of the Timer object to make it free it's resources.

      --- single minded; short sighted; long gone;

      A Offline
      A Offline
      aruna_koride
      wrote on last edited by
      #5

      I implemented IDisposable interface , but still not able to release resources. I am trying to dispose the timer in Dispose Method and calling the Dispose Method in the Main Class event where I want to end the application. But my app is crashed at the Dispose Method with StackOverflowException at timer.Dispose(). Please help me...

      G 1 Reply Last reply
      0
      • A aruna_koride

        I implemented IDisposable interface , but still not able to release resources. I am trying to dispose the timer in Dispose Method and calling the Dispose Method in the Main Class event where I want to end the application. But my app is crashed at the Dispose Method with StackOverflowException at timer.Dispose(). Please help me...

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #6

        A stack overflow usually means that you have "accomplished" an infinite recursive call. What does your code look like?

        --- single minded; short sighted; long gone;

        A 1 Reply Last reply
        0
        • G Guffa

          A stack overflow usually means that you have "accomplished" an infinite recursive call. What does your code look like?

          --- single minded; short sighted; long gone;

          A Offline
          A Offline
          aruna_koride
          wrote on last edited by
          #7

          I am just giving part of code ,main function of this class is when the user request a file to download, file is broken into chunks,the chunks are added to PendingChunks,the PendingChunks are updated in timer ,the callBack function gets the ChunkAddress of the requested pendingchunks from the server . public Class:IDisposable { Class{ ArrayList PendingChunks=new ArrayList(); timer2 = new System.Threading.Timer(new TimerCallback(getAddressfromServer), null, Timeout.Infinite, Timeout.Infinite); StartTimer(); } public void getAddressfromServer(object state) { getChunkAddress(PendingChunks) } const long TIMER_INTERVAL = 10000L; private void StartTimer() { timer.Change(0, TIMER_INTERVAL); } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { timer.dispose(); } disposed = true; } } public void getChunkAddress(ArrayList chnkList) { ArrayList chnkAddrList=null; Chunk c=null; ChunkAddress chnkAdd; chnkAddrList = HTTPClass.httpGetChunks(chnkList); if (chnkAddrList != null) { for (int j = 0; j < chnkList.Count; j++) { c = (Chunk)chnkList[j]; for (int i = 0; i < chnkAddrList.Count; i++) { c.chnkAddress = (ChunkAddress)chnkAddrList[i]; } } }

          G 1 Reply Last reply
          0
          • A aruna_koride

            I am just giving part of code ,main function of this class is when the user request a file to download, file is broken into chunks,the chunks are added to PendingChunks,the PendingChunks are updated in timer ,the callBack function gets the ChunkAddress of the requested pendingchunks from the server . public Class:IDisposable { Class{ ArrayList PendingChunks=new ArrayList(); timer2 = new System.Threading.Timer(new TimerCallback(getAddressfromServer), null, Timeout.Infinite, Timeout.Infinite); StartTimer(); } public void getAddressfromServer(object state) { getChunkAddress(PendingChunks) } const long TIMER_INTERVAL = 10000L; private void StartTimer() { timer.Change(0, TIMER_INTERVAL); } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { timer.dispose(); } disposed = true; } } public void getChunkAddress(ArrayList chnkList) { ArrayList chnkAddrList=null; Chunk c=null; ChunkAddress chnkAdd; chnkAddrList = HTTPClass.httpGetChunks(chnkList); if (chnkAddrList != null) { for (int j = 0; j < chnkList.Count; j++) { c = (Chunk)chnkList[j]; for (int i = 0; i < chnkAddrList.Count; i++) { c.chnkAddress = (ChunkAddress)chnkAddrList[i]; } } }

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #8

            The code that you show looks basically correct. I can't say anything about the actual code that you use, as you won't show it. Where do you dispose the object?

            --- single minded; short sighted; long gone;

            A 2 Replies Last reply
            0
            • G Guffa

              The code that you show looks basically correct. I can't say anything about the actual code that you use, as you won't show it. Where do you dispose the object?

              --- single minded; short sighted; long gone;

              A Offline
              A Offline
              aruna_koride
              wrote on last edited by
              #9

              I am disposing the timer in public void virtual Dispose Method

              1 Reply Last reply
              0
              • G Guffa

                The code that you show looks basically correct. I can't say anything about the actual code that you use, as you won't show it. Where do you dispose the object?

                --- single minded; short sighted; long gone;

                A Offline
                A Offline
                aruna_koride
                wrote on last edited by
                #10

                Sorry I have no idea how to dispose the object, can u please tell me?

                G 1 Reply Last reply
                0
                • A aruna_koride

                  Sorry I have no idea how to dispose the object, can u please tell me?

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #11

                  aruna_koride wrote:

                  Sorry I have no idea how to dispose the object, can u please tell me?

                  You just call the Dispose method. This should usually be done by the object that contains the reference to the object to dispose. If you for instance have put the object in a form, that form would call Dispose on the object when the form is closing.

                  --- single minded; short sighted; long gone;

                  A 1 Reply Last reply
                  0
                  • G Guffa

                    aruna_koride wrote:

                    Sorry I have no idea how to dispose the object, can u please tell me?

                    You just call the Dispose method. This should usually be done by the object that contains the reference to the object to dispose. If you for instance have put the object in a form, that form would call Dispose on the object when the form is closing.

                    --- single minded; short sighted; long gone;

                    A Offline
                    A Offline
                    aruna_koride
                    wrote on last edited by
                    #12

                    As you said I have a recursive loop because I am not stopping to get Chunk Address untill I cleared the PendingChunks ArrayList,may be that is Cause? Yes I have Called Dispose method in an another class ,triggered by Application Shutdown event of Form, like this: using(Class1 c=new Class1) { c.Dispose(); } Is it correct? Do I need to use using method?or just call Dispose Method?

                    G 1 Reply Last reply
                    0
                    • A aruna_koride

                      As you said I have a recursive loop because I am not stopping to get Chunk Address untill I cleared the PendingChunks ArrayList,may be that is Cause? Yes I have Called Dispose method in an another class ,triggered by Application Shutdown event of Form, like this: using(Class1 c=new Class1) { c.Dispose(); } Is it correct? Do I need to use using method?or just call Dispose Method?

                      G Offline
                      G Offline
                      Guffa
                      wrote on last edited by
                      #13

                      aruna_koride wrote:

                      Yes I have Called Dispose method in an another class ,triggered by Application Shutdown event of Form, like this: using(Class1 c=new Class1) { c.Dispose(); }

                      Then you are creating a new object and dispose that, that doesn't dispose the object that you use in the rest of the code.

                      --- single minded; short sighted; long gone;

                      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