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. About EndInvoke

About EndInvoke

Scheduled Pinned Locked Moved C#
helpquestion
13 Posts 4 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.
  • B Offline
    B Offline
    Bob_Sun
    wrote on last edited by
    #1

    I happened to find that in the provided libs of my program, many thread are invoked just by BeginInvoke without callback to do EndInvoke. Waht will happen to a thread in ThreadPool if EndInvoke is not all at all? Asyncs threads invoked by BeingInvoke are used in my program to send event, many I often enfront the problem that some events seem to heave disappeared in the middle of that thread. I will add EndInvoke code to all these threads. But I hope that I can get some confirmation befrore doing that. Thank you !

    R T 2 Replies Last reply
    0
    • B Bob_Sun

      I happened to find that in the provided libs of my program, many thread are invoked just by BeginInvoke without callback to do EndInvoke. Waht will happen to a thread in ThreadPool if EndInvoke is not all at all? Asyncs threads invoked by BeingInvoke are used in my program to send event, many I often enfront the problem that some events seem to heave disappeared in the middle of that thread. I will add EndInvoke code to all these threads. But I hope that I can get some confirmation befrore doing that. Thank you !

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      As I understand it, EndInvoke() is used to obtain the return value of your delegate method. If your delegate method is of type void, a call to EndInvoke() may not be required. if you haven't already, see this[^] article. /ravi My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

      B 1 Reply Last reply
      0
      • R Ravi Bhavnani

        As I understand it, EndInvoke() is used to obtain the return value of your delegate method. If your delegate method is of type void, a call to EndInvoke() may not be required. if you haven't already, see this[^] article. /ravi My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

        B Offline
        B Offline
        Bob_Sun
        wrote on last edited by
        #3

        Thank you very much. But I was told that BeginInvoke and EndInvoke should be called in pair, otherwise memory leak may happen in program. I have checked topics on ThreadPool the other day, and I found that threads invoked by BeginInvoke are not so simple.

        R 1 Reply Last reply
        0
        • B Bob_Sun

          Thank you very much. But I was told that BeginInvoke and EndInvoke should be called in pair, otherwise memory leak may happen in program. I have checked topics on ThreadPool the other day, and I found that threads invoked by BeginInvoke are not so simple.

          R Offline
          R Offline
          Ravi Bhavnani
          wrote on last edited by
          #4

          Bob_Sun wrote:

          BeginInvoke and EndInvoke should be called in pair,

          Hmm, I don't believe that is correct (but could be wrong). See this[^] MSDN article. /ravi My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

          B 1 Reply Last reply
          0
          • R Ravi Bhavnani

            Bob_Sun wrote:

            BeginInvoke and EndInvoke should be called in pair,

            Hmm, I don't believe that is correct (but could be wrong). See this[^] MSDN article. /ravi My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

            B Offline
            B Offline
            Bob_Sun
            wrote on last edited by
            #5

            Of cause, I haven't found any such discription in MSDN articles, just on some forum. But the fact is that my program do stop at somethere in the invoked thread (maybe for some other reasons). As the detail of ThreadPool is not opened completely, now I am considering of writing a ThreadManager of my own, just start a thread by Thread.Start(), if necessary cancel it by Thread.Abort().

            B 1 Reply Last reply
            0
            • B Bob_Sun

              Of cause, I haven't found any such discription in MSDN articles, just on some forum. But the fact is that my program do stop at somethere in the invoked thread (maybe for some other reasons). As the detail of ThreadPool is not opened completely, now I am considering of writing a ThreadManager of my own, just start a thread by Thread.Start(), if necessary cancel it by Thread.Abort().

              B Offline
              B Offline
              Bob_Sun
              wrote on last edited by
              #6

              In the following artical, it says CAUTION Always call EndInvoke after your asynchronous call completes. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpovrasynchronousprogrammingoverview.asp?frame=true[^] http://www.interact-sw.co.uk/iangblog/2005/05/16/endinvokerequired[^] what will happen if EndInvoke is not called? Thank you!

              T 1 Reply Last reply
              0
              • B Bob_Sun

                In the following artical, it says CAUTION Always call EndInvoke after your asynchronous call completes. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpovrasynchronousprogrammingoverview.asp?frame=true[^] http://www.interact-sw.co.uk/iangblog/2005/05/16/endinvokerequired[^] what will happen if EndInvoke is not called? Thank you!

                T Offline
                T Offline
                Tehnoon
                wrote on last edited by
                #7

                Rest assured. EndInvoke is ONLYused if your asyncronous method returns something. You call EndInvoke to retrieve that return value, since you can't get the return value in any other way. EndInvoke does not do anything else apart from giving you the return value. If you are not interested in the return value, you do not need to call EndInvoke. I have used BeginInvoke so many times in so many performance critical applications without using EndInvoke, it doesn't create a problem

                B D 2 Replies Last reply
                0
                • T Tehnoon

                  Rest assured. EndInvoke is ONLYused if your asyncronous method returns something. You call EndInvoke to retrieve that return value, since you can't get the return value in any other way. EndInvoke does not do anything else apart from giving you the return value. If you are not interested in the return value, you do not need to call EndInvoke. I have used BeginInvoke so many times in so many performance critical applications without using EndInvoke, it doesn't create a problem

                  B Offline
                  B Offline
                  Bob_Sun
                  wrote on last edited by
                  #8

                  Tehnoon wrote:

                  I have used BeginInvoke so many times in so many performance critical applications without using EndInvoke, it doesn't create a problem

                  Thank you for quick reply. In my case, this is the opposite, it seems to me that the thread invoked by BeginInvoke disappeared half way(only part of the log left). I am not sure whether a background thread will disappear half way if GC.Collect() happens during its process? I am investigating for the reason... Thank you

                  1 Reply Last reply
                  0
                  • T Tehnoon

                    Rest assured. EndInvoke is ONLYused if your asyncronous method returns something. You call EndInvoke to retrieve that return value, since you can't get the return value in any other way. EndInvoke does not do anything else apart from giving you the return value. If you are not interested in the return value, you do not need to call EndInvoke. I have used BeginInvoke so many times in so many performance critical applications without using EndInvoke, it doesn't create a problem

                    D Offline
                    D Offline
                    Daniel Grunwald
                    wrote on last edited by
                    #9

                    delegate.BeginInvoke (creates new thread [from thread-pool]) without delegate.EndInvoke will cause a memory leak. Control.BeginInvoke ('sends' call to main thread) without Control.EndInvoke is no problem.

                    B 1 Reply Last reply
                    0
                    • D Daniel Grunwald

                      delegate.BeginInvoke (creates new thread [from thread-pool]) without delegate.EndInvoke will cause a memory leak. Control.BeginInvoke ('sends' call to main thread) without Control.EndInvoke is no problem.

                      B Offline
                      B Offline
                      Bob_Sun
                      wrote on last edited by
                      #10

                      Daniel Grunwald wrote:

                      delegate.BeginInvoke (creates new thread [from thread-pool]) without delegate.EndInvoke will cause a memory leak.

                      I am troubled by threads invoked from ThreadPool. Can you give me some hint about what the memory leak will bring about? Thank you.

                      D 1 Reply Last reply
                      0
                      • B Bob_Sun

                        Daniel Grunwald wrote:

                        delegate.BeginInvoke (creates new thread [from thread-pool]) without delegate.EndInvoke will cause a memory leak.

                        I am troubled by threads invoked from ThreadPool. Can you give me some hint about what the memory leak will bring about? Thank you.

                        D Offline
                        D Offline
                        Daniel Grunwald
                        wrote on last edited by
                        #11

                        I think you will leak one WaitHandle for each delegate.BeginInvoke call, plus any objects waiting to be returned (e.g. exceptions). When you want to use the thread-pool in "fire-and-forget" mode, use hreadPool.QueueUserWorkItem or provide a callback-method to delegate.BeginInvoke that in turn calls delegate.EndInvoke.

                        1 Reply Last reply
                        0
                        • B Bob_Sun

                          I happened to find that in the provided libs of my program, many thread are invoked just by BeginInvoke without callback to do EndInvoke. Waht will happen to a thread in ThreadPool if EndInvoke is not all at all? Asyncs threads invoked by BeingInvoke are used in my program to send event, many I often enfront the problem that some events seem to heave disappeared in the middle of that thread. I will add EndInvoke code to all these threads. But I hope that I can get some confirmation befrore doing that. Thank you !

                          T Offline
                          T Offline
                          Tehnoon
                          wrote on last edited by
                          #12

                          Can you show the code in which you are invoking the thread which is creating the problem? Garbage collection will not be done unless the thread is finished with the execution. The only possibility that I am seeing here is that the system is not being able to provide all the requested resources by the program and hence the results are not coming out as expected. A look at the source code will give me a better idea of this.

                          B 1 Reply Last reply
                          0
                          • T Tehnoon

                            Can you show the code in which you are invoking the thread which is creating the problem? Garbage collection will not be done unless the thread is finished with the execution. The only possibility that I am seeing here is that the system is not being able to provide all the requested resources by the program and hence the results are not coming out as expected. A look at the source code will give me a better idea of this.

                            B Offline
                            B Offline
                            Bob_Sun
                            wrote on last edited by
                            #13

                            Tehnoon wrote:

                            Can you show the code in which you are invoking the thread which is creating the problem? Garbage collection will not be done unless the thread is finished with the execution. The only possibility that I am seeing here is that the system is not being able to provide all the requested resources by the program and hence the results are not coming out as expected. A look at the source code will give me a better idea of this.

                            Sorry for a late response. A statemachine exists in my program, and all the events are dispatched to it by seperate threads.The code is something like the following.

                            public class Control
                            {
                            private StateMachine myStateMachine = new StateMachine();

                            public void EventProc1()
                            {
                                ....
                                EventHandler eventHandler = new EventHandler(this.Thread1);
                                eventHandler.BeginInvoke(this,null,null,null);
                            }
                            
                            public void EventProc2()
                            {
                                ....
                                EventHandler eventHandler = new EventHandler(this.Thread2);
                                eventHandler.BeginInvoke(this,null,null,null);
                            }
                            
                            protected void Thread1(object sender,EventArgs e)
                            {
                                LogMessage("Start of thread1");
                                this.myStateMachine.Dispatch(1);
                            }
                            
                            protected void Thread2(object sender,EventArgs e)
                            {
                                LogMessage("Start of thread2");
                                this.myStateMachine.Dispatch(2);
                            }
                            

                            }

                            public class StateMachine
                            {
                            public void Dispatch(event id)
                            {
                            switch(event id)
                            {
                            case 1:
                            ....
                            break;
                            case 2:
                            ....
                            break;
                            ...
                            default:
                            break;
                            }
                            }
                            }

                            Up to now, many phenomena happened, such as although the LogMessage were logged in log file, the thread just stopped somewhere afterward. Or sometimes, one thread should be waiting for an AutoResetEvent which must be set in another thread as designed, but the previous thread moves on before the AutoResetEvent is Set()... Another important supplement is that GC.Collect() is executed in another thread, ie. in EventProc1(). I have written many test programs so as to find the real reason, and still don know where a memory leak brought by no-EndInvoke will lead to the above phenomena. Looking forword to your help. Thank you!

                            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