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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Get rid of delegate declarations

Get rid of delegate declarations

Scheduled Pinned Locked Moved C#
sysadminhelptutorialquestion
5 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 Offline
    G Offline
    Genbox
    wrote on last edited by
    #1

    I have a network application that runs in a thread for it self, my main application subscribes to the events the network application fires (such as SignedIn, ExceptionOccured and so on). My problem is that when a event in the other thread if fired, I want to disable/enable and change a lot of stuff in my main application GUI. But for every change I want to make in my main application, I have to write a delegate and invoke it. This gives me A LOT of code that is only used once, and it just seems stupid. Is there a way of executing a method with invoke without declaring a delegate? I have an example to demonstrate my problem: public class MyClass { public MyClass() { // starting the network application thread here ... // subscribing to the events myThread.SignOut += SignOut; } public void SignOut() { // This i would have to create a delegate for, since the myForm object is in // another thread. This forces me to create a delegate for // every (different) method call in my application. myForm.ShowLogin(); } }

    E J 2 Replies Last reply
    0
    • G Genbox

      I have a network application that runs in a thread for it self, my main application subscribes to the events the network application fires (such as SignedIn, ExceptionOccured and so on). My problem is that when a event in the other thread if fired, I want to disable/enable and change a lot of stuff in my main application GUI. But for every change I want to make in my main application, I have to write a delegate and invoke it. This gives me A LOT of code that is only used once, and it just seems stupid. Is there a way of executing a method with invoke without declaring a delegate? I have an example to demonstrate my problem: public class MyClass { public MyClass() { // starting the network application thread here ... // subscribing to the events myThread.SignOut += SignOut; } public void SignOut() { // This i would have to create a delegate for, since the myForm object is in // another thread. This forces me to create a delegate for // every (different) method call in my application. myForm.ShowLogin(); } }

      E Offline
      E Offline
      Ed Poore
      wrote on last edited by
      #2

      Not sure if this is what you're trying to accomplish but: If using .NET 2 you can use an anoymous delegate which would perform the same function (internally the code is very similar) but the source code would look more readable.

      pubilc MyClass()
      {
          myThread.SignOut += new YourDelegateTypeHere(delegate() { myForm.ShowLogin(); });
      }

      G 1 Reply Last reply
      0
      • E Ed Poore

        Not sure if this is what you're trying to accomplish but: If using .NET 2 you can use an anoymous delegate which would perform the same function (internally the code is very similar) but the source code would look more readable.

        pubilc MyClass()
        {
            myThread.SignOut += new YourDelegateTypeHere(delegate() { myForm.ShowLogin(); });
        }

        G Offline
        G Offline
        Genbox
        wrote on last edited by
        #3

        Thats better than what I'm doing now. Thanks! If anyone else has some tips, you are welcome to post them.

        1 Reply Last reply
        0
        • G Genbox

          I have a network application that runs in a thread for it self, my main application subscribes to the events the network application fires (such as SignedIn, ExceptionOccured and so on). My problem is that when a event in the other thread if fired, I want to disable/enable and change a lot of stuff in my main application GUI. But for every change I want to make in my main application, I have to write a delegate and invoke it. This gives me A LOT of code that is only used once, and it just seems stupid. Is there a way of executing a method with invoke without declaring a delegate? I have an example to demonstrate my problem: public class MyClass { public MyClass() { // starting the network application thread here ... // subscribing to the events myThread.SignOut += SignOut; } public void SignOut() { // This i would have to create a delegate for, since the myForm object is in // another thread. This forces me to create a delegate for // every (different) method call in my application. myForm.ShowLogin(); } }

          J Offline
          J Offline
          Jimmanuel
          wrote on last edited by
          #4

          When I'm doing a lot of cross thread operations I try to make my functions similar so that I can reuse their delegates as much as possible. If I have 10 functions that all have to be Invoked, only one delegate is needed if they all have the same return value and parameter list . . .

          G 1 Reply Last reply
          0
          • J Jimmanuel

            When I'm doing a lot of cross thread operations I try to make my functions similar so that I can reuse their delegates as much as possible. If I have 10 functions that all have to be Invoked, only one delegate is needed if they all have the same return value and parameter list . . .

            G Offline
            G Offline
            Genbox
            wrote on last edited by
            #5

            Yeah, that's what I was doing, but I don't like to do this because of the delegate naming and overhead of creating a delegate. I found the perfect solution: // In a form: if (InvokeRequired) { MethodInvoker invoker = delegate { lblStatus.Text = message; }; Invoke(invoker); } else lblStatus.Text = message; With this I can execute cross-thread calls without creating a named delegate and a method for the delegate to call. Best of all; the code is readable.

            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