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. Managed C++/CLI
  4. Dispatching for events?

Dispatching for events?

Scheduled Pinned Locked Moved Managed C++/CLI
csharpc++designquestion
7 Posts 2 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.
  • D Offline
    D Offline
    Don Rolando
    wrote on last edited by
    #1

    Hi everyone, currently I am writing a wrapper for some native library which offers a hand full of callbacks. I wrote some .net events which are called when these callbacks are triggered. So far no problems, but I cannot work with the result strings etc. from the application that uses my assembly, because the events are still called from a thread different from the UI thread. :( Is there any chance to call the events through some dispatcher within my mixed assembly? I would like to avoid that the user has to write some code on his own to dispatch the events. Since that is the expected event behavior, is it possible that I am doing something wrong at all? Sincerly, Roland

    L 1 Reply Last reply
    0
    • D Don Rolando

      Hi everyone, currently I am writing a wrapper for some native library which offers a hand full of callbacks. I wrote some .net events which are called when these callbacks are triggered. So far no problems, but I cannot work with the result strings etc. from the application that uses my assembly, because the events are still called from a thread different from the UI thread. :( Is there any chance to call the events through some dispatcher within my mixed assembly? I would like to avoid that the user has to write some code on his own to dispatch the events. Since that is the expected event behavior, is it possible that I am doing something wrong at all? Sincerly, Roland

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Don Rolando wrote:

      from the application that uses my assembly, because the events are still called from a thread different from the UI thread.

      Not sure I understand you correctly but it sounds like you are saying that your application code is registering an event handler for an event published in  a class in the library. The library fires the event  from a  worker thread and therefore the application codes event handler is executing on the worker thread. If that is correct I would point out first that from a library design stand point, that doesn't seem like a good idea. The library should managed the thread communications such that the event fires on the main thread. However a simple solution is to have your application event handler call BeginInvoke to update the UI components. You can find much information[^] about this topic

      led mike

      D 1 Reply Last reply
      0
      • L led mike

        Don Rolando wrote:

        from the application that uses my assembly, because the events are still called from a thread different from the UI thread.

        Not sure I understand you correctly but it sounds like you are saying that your application code is registering an event handler for an event published in  a class in the library. The library fires the event  from a  worker thread and therefore the application codes event handler is executing on the worker thread. If that is correct I would point out first that from a library design stand point, that doesn't seem like a good idea. The library should managed the thread communications such that the event fires on the main thread. However a simple solution is to have your application event handler call BeginInvoke to update the UI components. You can find much information[^] about this topic

        led mike

        D Offline
        D Offline
        Don Rolando
        wrote on last edited by
        #3

        You understood it correctly; but the bad news is that I cannot modify the design of the library which is embedded in my assembly. I have these callbacks fired from the lib-file's worker thread. I have thought about BeginInvoke myself, but in this case I cannot hide the dispatching process from the user (or can I?), because it must be implemented in the app's event handler - so the user has to write that code himself. I would need that dispatching process in between the callback from the worker thread in the lib file and the event handler.

        D L 2 Replies Last reply
        0
        • D Don Rolando

          You understood it correctly; but the bad news is that I cannot modify the design of the library which is embedded in my assembly. I have these callbacks fired from the lib-file's worker thread. I have thought about BeginInvoke myself, but in this case I cannot hide the dispatching process from the user (or can I?), because it must be implemented in the app's event handler - so the user has to write that code himself. I would need that dispatching process in between the callback from the worker thread in the lib file and the event handler.

          D Offline
          D Offline
          Don Rolando
          wrote on last edited by
          #4

          Have a deeper insight on BeginInvoke I realized that I could also do this on the event's delegate. So more or less I could start the invocation within my assembly; anyhow, I can't get it work in C++/CLI, because either the event is not recognized as a data member (seems to be a C++/CLI specific behavior, as I read so far) or the AsyncCallback structure will not work, grrr. Does anyone have a short code snippet how to invoke some event in C++/CLI correctly?

          L 1 Reply Last reply
          0
          • D Don Rolando

            Have a deeper insight on BeginInvoke I realized that I could also do this on the event's delegate. So more or less I could start the invocation within my assembly; anyhow, I can't get it work in C++/CLI, because either the event is not recognized as a data member (seems to be a C++/CLI specific behavior, as I read so far) or the AsyncCallback structure will not work, grrr. Does anyone have a short code snippet how to invoke some event in C++/CLI correctly?

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            Don Rolando wrote:

            Does anyone have a short code snippet how to invoke some event in C++/CLI correctly?

            I never looked at the them but I bet you will find an example in the introductory CLI articles here on CodeProject. Look under the "Chapters" menu.

            led mike

            1 Reply Last reply
            0
            • D Don Rolando

              You understood it correctly; but the bad news is that I cannot modify the design of the library which is embedded in my assembly. I have these callbacks fired from the lib-file's worker thread. I have thought about BeginInvoke myself, but in this case I cannot hide the dispatching process from the user (or can I?), because it must be implemented in the app's event handler - so the user has to write that code himself. I would need that dispatching process in between the callback from the worker thread in the lib file and the event handler.

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              Don Rolando wrote:

              I cannot modify the design of the library which is embedded in my assembly. I have these callbacks fired from the lib-file's worker thread.

              Don Rolando wrote:

              I cannot hide the dispatching process from the user (or can I?), because it must be implemented in the app's event handler - so the user has to write that code himself.

              That information is very confusing. If it is your assembly, why can't you modify it?

              led mike

              D 1 Reply Last reply
              0
              • L led mike

                Don Rolando wrote:

                I cannot modify the design of the library which is embedded in my assembly. I have these callbacks fired from the lib-file's worker thread.

                Don Rolando wrote:

                I cannot hide the dispatching process from the user (or can I?), because it must be implemented in the app's event handler - so the user has to write that code himself.

                That information is very confusing. If it is your assembly, why can't you modify it?

                led mike

                D Offline
                D Offline
                Don Rolando
                wrote on last edited by
                #7

                The assembly is from me, right... but not the linked lib-file which generates it's own worker threads and calls the callbacks. That's kind of a black box which's architectural design I cannot modify in any way. But I can try to start the dispatching when firing the event out of these callbacks. That's still within the assembly (but would however not beautify the general architecture), Silly thing is that I could not find examples for BeginInvoke on delegates for C++/CLI so far... only on controls or in C#; and I read that C++/CLI is different in that case and cannot be handled the same way... but I will get it work soon, I am sure. :)

                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