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. Can someone explain this...

Can someone explain this...

Scheduled Pinned Locked Moved C#
question
11 Posts 6 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
    DanielSheets
    wrote on last edited by
    #1

    private void ReadDaqThreadProc()
    {
    dThreadCallback tcb = new dThreadCallback(ReadDaq);
    while (flags.daqEnabled)
    {
    try
    {
    this.Invoke(tcb, new Object[] { }); }
    catch (Exception e)
    {
    }
    Thread.Sleep(50);
    }
    }

    This snippet is part of a multithreaded app. "tcb" is a delegate, ReadDaq is a function in the MainForm code, and ReadDaqThreadProc is in its own thread. I know what the code does but I dont know how. I'd like to understand how this works. The line in question is the invoke line. Is 'new Object[] {}' an argument list for ReadDaq?

    L V 2 Replies Last reply
    0
    • D DanielSheets

      private void ReadDaqThreadProc()
      {
      dThreadCallback tcb = new dThreadCallback(ReadDaq);
      while (flags.daqEnabled)
      {
      try
      {
      this.Invoke(tcb, new Object[] { }); }
      catch (Exception e)
      {
      }
      Thread.Sleep(50);
      }
      }

      This snippet is part of a multithreaded app. "tcb" is a delegate, ReadDaq is a function in the MainForm code, and ReadDaqThreadProc is in its own thread. I know what the code does but I dont know how. I'd like to understand how this works. The line in question is the invoke line. Is 'new Object[] {}' an argument list for ReadDaq?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Yes, it's passing an empty object array (the default container for invoke-params) and calling "tcb", the ReadDaq method - a method without parameters. And it seems to swallow any exceptions, which isn't such a great thing.

      Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

      D 1 Reply Last reply
      0
      • L Lost User

        Yes, it's passing an empty object array (the default container for invoke-params) and calling "tcb", the ReadDaq method - a method without parameters. And it seems to swallow any exceptions, which isn't such a great thing.

        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

        D Offline
        D Offline
        DanielSheets
        wrote on last edited by
        #3

        It should be in a try block?

        L B 2 Replies Last reply
        0
        • D DanielSheets

          It should be in a try block?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Member 8461599 wrote:

          It should be in a try block?

          Why? Which exceptions does the Invoke method throw? It's only there to ensure that any exceptions from the implementation of the method do not kill the application; and those exceptions should be handled there - locally; not here. Also, it's wise to "log" all exceptions, as a "swallow all and be silent" is a known point of failure. Sometimes an exception is thrown away that you did not expect, causing weird program-behaviour.

          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

          D 1 Reply Last reply
          0
          • L Lost User

            Member 8461599 wrote:

            It should be in a try block?

            Why? Which exceptions does the Invoke method throw? It's only there to ensure that any exceptions from the implementation of the method do not kill the application; and those exceptions should be handled there - locally; not here. Also, it's wise to "log" all exceptions, as a "swallow all and be silent" is a known point of failure. Sometimes an exception is thrown away that you did not expect, causing weird program-behaviour.

            Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

            D Offline
            D Offline
            DanielSheets
            wrote on last edited by
            #5

            If you'll forgive my ignorance... I guess I dont know what you mean by "swallow any exceptions, which isn't such a great thing". My knowledge of C# isnt very deep, as you can see.

            OriginalGriffO 1 Reply Last reply
            0
            • D DanielSheets

              It should be in a try block?

              B Offline
              B Offline
              Bernhard Hiller
              wrote on last edited by
              #6

              The exceptions which could arise in the ReadDaq function will arise in a different thread - the thread of the GUI. You are not able to catch them here, hence you can omit the try-catch-block here. But ReadDaq ought to have its own try-catch.

              A 1 Reply Last reply
              0
              • D DanielSheets

                private void ReadDaqThreadProc()
                {
                dThreadCallback tcb = new dThreadCallback(ReadDaq);
                while (flags.daqEnabled)
                {
                try
                {
                this.Invoke(tcb, new Object[] { }); }
                catch (Exception e)
                {
                }
                Thread.Sleep(50);
                }
                }

                This snippet is part of a multithreaded app. "tcb" is a delegate, ReadDaq is a function in the MainForm code, and ReadDaqThreadProc is in its own thread. I know what the code does but I dont know how. I'd like to understand how this works. The line in question is the invoke line. Is 'new Object[] {}' an argument list for ReadDaq?

                V Offline
                V Offline
                V 0
                wrote on last edited by
                #7

                not 100% sure, but you could pass null instead of new Object[]{}. The signature of the Invoke method requires an object array. This is actually used to box/unbox parameters for the threading function. If null is allowed (I think so) this is for me preferable, because it indicates we're passing it to fulfill the method requirement. passing a new Object[]{} not only initializes memory, but also might indicate we "forgot" to do something. Hope this helps.

                V.
                (MQOTD Rules )

                1 Reply Last reply
                0
                • D DanielSheets

                  If you'll forgive my ignorance... I guess I dont know what you mean by "swallow any exceptions, which isn't such a great thing". My knowledge of C# isnt very deep, as you can see.

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  This code:

                  try
                  {
                  ...
                  }
                  catch (Exception e)
                  {
                  }

                  is called "swallowing an exception", because whatever error occurs in the try block will be caught by the catch block and discarded without any attempt to handle it, log it, or in any other way acknowledge that there is a problem that should be fixed, or at least investigated. This is considered a poor programming practice, as it just masks problems that may become serious later. There can (occasionally) be a good reasons for ignoring specific exceptions, but these should be restricted to specific exception classes (such as ArgumentNullException, or FileLoadException) rather than a blanket "Exception" and commented thoroughly.

                  Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  D 1 Reply Last reply
                  0
                  • B Bernhard Hiller

                    The exceptions which could arise in the ReadDaq function will arise in a different thread - the thread of the GUI. You are not able to catch them here, hence you can omit the try-catch-block here. But ReadDaq ought to have its own try-catch.

                    A Offline
                    A Offline
                    Alan N
                    wrote on last edited by
                    #9

                    Bernhard Hiller wrote:

                    You are not able to catch them here,

                    No, not quite true (at all). The Invoke mechanism catches an unhandled exception within the target method and rethrows it in the calling thread. In doing this the stack trace containing the site of the original exception is lost. Whether or not something bad happens when the exception is rethrown depends on the caller. For example I've noticed that the thread used by a System.Timers.Timer to call the Elapsed event handler carries on obliviously with no hint of discomfort! See the remarks section in Control.Invoke[^] Alan.

                    1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      This code:

                      try
                      {
                      ...
                      }
                      catch (Exception e)
                      {
                      }

                      is called "swallowing an exception", because whatever error occurs in the try block will be caught by the catch block and discarded without any attempt to handle it, log it, or in any other way acknowledge that there is a problem that should be fixed, or at least investigated. This is considered a poor programming practice, as it just masks problems that may become serious later. There can (occasionally) be a good reasons for ignoring specific exceptions, but these should be restricted to specific exception classes (such as ArgumentNullException, or FileLoadException) rather than a blanket "Exception" and commented thoroughly.

                      Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                      D Offline
                      D Offline
                      DanielSheets
                      wrote on last edited by
                      #10

                      Ahh... Gotcha. There actually is code there. I took it out when I posted it here.

                      OriginalGriffO 1 Reply Last reply
                      0
                      • D DanielSheets

                        Ahh... Gotcha. There actually is code there. I took it out when I posted it here.

                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #11

                        :thumbsup:

                        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        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