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. how to send a delegate to a fuction with a parameter?

how to send a delegate to a fuction with a parameter?

Scheduled Pinned Locked Moved C#
helpquestiondatabasedata-structurestutorial
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.
  • H Offline
    H Offline
    Hussam Fattahi
    wrote on last edited by
    #1

    I don't know if the above question is correct, my english it's not very good. however, i'm trying to make a safe thread call to a form : I have declared a chatForms array with name (chatsAvailabel), so when i want to show one of them i call the fuction ShowForm with a parameter that specify the index of the form in the array.

     delegate void ShowFormCallback(int index);
    
    private void ShowForm(int index)
            {
                if (this.chatsAvailabe[index].InvokeRequired)
                {
                    ShowFormCallback tmp = new ShowFormCallback(ShowForm(index));  //error here
                    this.chatsAvailabe[index].Invoke(tmp);
                }
                else
                    this.chatsAvailabe[index].Show();
            }
    

    but the problem occures when the InvokeRequired is true in if bolck, i must declare an instance from the ShowFormCallback delegate to use with invoke, but how could i send parmaeter index again along with this delegate instance. thanks in advance

    L E 2 Replies Last reply
    0
    • H Hussam Fattahi

      I don't know if the above question is correct, my english it's not very good. however, i'm trying to make a safe thread call to a form : I have declared a chatForms array with name (chatsAvailabel), so when i want to show one of them i call the fuction ShowForm with a parameter that specify the index of the form in the array.

       delegate void ShowFormCallback(int index);
      
      private void ShowForm(int index)
              {
                  if (this.chatsAvailabe[index].InvokeRequired)
                  {
                      ShowFormCallback tmp = new ShowFormCallback(ShowForm(index));  //error here
                      this.chatsAvailabe[index].Invoke(tmp);
                  }
                  else
                      this.chatsAvailabe[index].Show();
              }
      

      but the problem occures when the InvokeRequired is true in if bolck, i must declare an instance from the ShowFormCallback delegate to use with invoke, but how could i send parmaeter index again along with this delegate instance. thanks in advance

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

      what about this style? : private delegate void ShowFormCallback (int index); private void ShowForm (int index) { if (this.chatsAvailabe[index].InvokeRequired) this.chatsAvailable[index].Invoke(new ShowFormCallback(ShowForm),new object[] { index }); else this.cahtsAvailable[index].Show(); } VirtualVoid.NET

      H 1 Reply Last reply
      0
      • L Lost User

        what about this style? : private delegate void ShowFormCallback (int index); private void ShowForm (int index) { if (this.chatsAvailabe[index].InvokeRequired) this.chatsAvailable[index].Invoke(new ShowFormCallback(ShowForm),new object[] { index }); else this.cahtsAvailable[index].Show(); } VirtualVoid.NET

        H Offline
        H Offline
        Hussam Fattahi
        wrote on last edited by
        #3

        it's do work without error or exception, but the new showed form freeze(not rsponding) on screen which mean that the operation it's not thread safe.

        L 1 Reply Last reply
        0
        • H Hussam Fattahi

          it's do work without error or exception, but the new showed form freeze(not rsponding) on screen which mean that the operation it's not thread safe.

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

          maybe ShowDialog() can help ? (not just Show())

          VirtualVoid**.NET**

          1 Reply Last reply
          0
          • H Hussam Fattahi

            I don't know if the above question is correct, my english it's not very good. however, i'm trying to make a safe thread call to a form : I have declared a chatForms array with name (chatsAvailabel), so when i want to show one of them i call the fuction ShowForm with a parameter that specify the index of the form in the array.

             delegate void ShowFormCallback(int index);
            
            private void ShowForm(int index)
                    {
                        if (this.chatsAvailabe[index].InvokeRequired)
                        {
                            ShowFormCallback tmp = new ShowFormCallback(ShowForm(index));  //error here
                            this.chatsAvailabe[index].Invoke(tmp);
                        }
                        else
                            this.chatsAvailabe[index].Show();
                    }
            

            but the problem occures when the InvokeRequired is true in if bolck, i must declare an instance from the ShowFormCallback delegate to use with invoke, but how could i send parmaeter index again along with this delegate instance. thanks in advance

            E Offline
            E Offline
            Ennis Ray Lynch Jr
            wrote on last edited by
            #5

            You are not correctly using delegates. Try this:

            private void InvokeShowForm(int index){
            if(InvokeRequired){
            Invoke(new ShowFormCallback(ShowForm), new object[]{index}));
            }
            else{
            ShowForm(index);
            }
            }

            public void ShowForm(int index){
            chatsAvailable[index].Show();
            }


            File Not Found

            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