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. What's wrong with this code?

What's wrong with this code?

Scheduled Pinned Locked Moved C#
helpquestion
12 Posts 7 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.
  • J Juvil John
        private delegate void \_Post\_Result(object\[\] data);
    
        private void Post\_Result(object\[\] data)
        {
            if (lvThreads.InvokeRequired)
            {
                //This means that calling was accessed in separate thread
                \_Post\_Result upd = new \_Post\_Result(Post\_Result);
                //this.lvThreads.Invoke(upd, data);
                **this.lvThreads.Invoke(upd, new object\[\] { data });**
            }
            else
            {
                //This means that calling of lvThreads is in main thread
                ListViewItem lvitem = (ListViewItem)data\[0\];
                this.lvThreads.Items\[lvitem.Text\].SubItems\[1\].Text = Convert.ToString(data\[1\]);
            }
        }
    

    I am getting this error "Parameter count mismatch" in the bold line...

    K Offline
    K Offline
    Kristian Sixhoj
    wrote on last edited by
    #2

    don234564 wrote:

    Parameter count mismatch

    The error message always tells you the problem. In this case, the number of parameters passed to the method is incorrect. I don't know that method you're using, though, so I can't help you with that.

    Kristian Sixhoej "You can't undo the past... but you can certainly not repeat it." - Bruce Willis

    1 Reply Last reply
    0
    • J Juvil John
          private delegate void \_Post\_Result(object\[\] data);
      
          private void Post\_Result(object\[\] data)
          {
              if (lvThreads.InvokeRequired)
              {
                  //This means that calling was accessed in separate thread
                  \_Post\_Result upd = new \_Post\_Result(Post\_Result);
                  //this.lvThreads.Invoke(upd, data);
                  **this.lvThreads.Invoke(upd, new object\[\] { data });**
              }
              else
              {
                  //This means that calling of lvThreads is in main thread
                  ListViewItem lvitem = (ListViewItem)data\[0\];
                  this.lvThreads.Items\[lvitem.Text\].SubItems\[1\].Text = Convert.ToString(data\[1\]);
              }
          }
      

      I am getting this error "Parameter count mismatch" in the bold line...

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #3

      Looks alright to me, I have to say. What is lvThreads?

      Regards, Rob Philpott.

      1 Reply Last reply
      0
      • J Juvil John
            private delegate void \_Post\_Result(object\[\] data);
        
            private void Post\_Result(object\[\] data)
            {
                if (lvThreads.InvokeRequired)
                {
                    //This means that calling was accessed in separate thread
                    \_Post\_Result upd = new \_Post\_Result(Post\_Result);
                    //this.lvThreads.Invoke(upd, data);
                    **this.lvThreads.Invoke(upd, new object\[\] { data });**
                }
                else
                {
                    //This means that calling of lvThreads is in main thread
                    ListViewItem lvitem = (ListViewItem)data\[0\];
                    this.lvThreads.Items\[lvitem.Text\].SubItems\[1\].Text = Convert.ToString(data\[1\]);
                }
            }
        

        I am getting this error "Parameter count mismatch" in the bold line...

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #4

        don234564 wrote:

        new object[] { data }

        data already is an object array, why create a new array holding this one array as its sole element? BTW: there is no need to create the delegate over and over again, you can move the statement _Post_Result upd = new _Post_Result(Post_Result); elsewhere (e.g. as a class member) and execute it only once. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


        modified on Friday, June 10, 2011 11:38 AM

        R J 2 Replies Last reply
        0
        • L Luc Pattyn

          don234564 wrote:

          new object[] { data }

          data already is an object array, why create a new array holding this one array as its sole element? BTW: there is no need to create the delegate over and over again, you can move the statement _Post_Result upd = new _Post_Result(Post_Result); elsewhere (e.g. as a class member) and execute it only once. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


          modified on Friday, June 10, 2011 11:38 AM

          R Offline
          R Offline
          Rob Philpott
          wrote on last edited by
          #5

          I think I must be missing something here - I can't see what's wrong with the code. You create a new array holding the array as it's only element because that's the signature Control.Invoke expects. The array represents individual parameters, the fact there's only one and that it is itself an array is irrelevant. But then, I think it should work so don't listen to me... :doh:

          Regards, Rob Philpott.

          J 1 Reply Last reply
          0
          • R Rob Philpott

            I think I must be missing something here - I can't see what's wrong with the code. You create a new array holding the array as it's only element because that's the signature Control.Invoke expects. The array represents individual parameters, the fact there's only one and that it is itself an array is irrelevant. But then, I think it should work so don't listen to me... :doh:

            Regards, Rob Philpott.

            J Offline
            J Offline
            Juvil John
            wrote on last edited by
            #6

            im confused either where im getting this wrong... anyone can try the code?

            1 Reply Last reply
            0
            • L Luc Pattyn

              don234564 wrote:

              new object[] { data }

              data already is an object array, why create a new array holding this one array as its sole element? BTW: there is no need to create the delegate over and over again, you can move the statement _Post_Result upd = new _Post_Result(Post_Result); elsewhere (e.g. as a class member) and execute it only once. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


              modified on Friday, June 10, 2011 11:38 AM

              J Offline
              J Offline
              Juvil John
              wrote on last edited by
              #7

              yes data is already an array. parameter in control.invoke refers to the number of parameters you pass this through in that case "upd" which is refered to Post_Results which contains only 1 param so thats the reason why you have to place the data into another array.

              1 Reply Last reply
              0
              • J Juvil John
                    private delegate void \_Post\_Result(object\[\] data);
                
                    private void Post\_Result(object\[\] data)
                    {
                        if (lvThreads.InvokeRequired)
                        {
                            //This means that calling was accessed in separate thread
                            \_Post\_Result upd = new \_Post\_Result(Post\_Result);
                            //this.lvThreads.Invoke(upd, data);
                            **this.lvThreads.Invoke(upd, new object\[\] { data });**
                        }
                        else
                        {
                            //This means that calling of lvThreads is in main thread
                            ListViewItem lvitem = (ListViewItem)data\[0\];
                            this.lvThreads.Items\[lvitem.Text\].SubItems\[1\].Text = Convert.ToString(data\[1\]);
                        }
                    }
                

                I am getting this error "Parameter count mismatch" in the bold line...

                W Offline
                W Offline
                Wendelius
                wrote on last edited by
                #8

                Never run into this situation but could you try what happens if you define the delegate as public. Also as others pointed out, I don't see a reason to create a new array.

                The need to optimize rises from a bad design.My articles[^]

                J 1 Reply Last reply
                0
                • J Juvil John
                      private delegate void \_Post\_Result(object\[\] data);
                  
                      private void Post\_Result(object\[\] data)
                      {
                          if (lvThreads.InvokeRequired)
                          {
                              //This means that calling was accessed in separate thread
                              \_Post\_Result upd = new \_Post\_Result(Post\_Result);
                              //this.lvThreads.Invoke(upd, data);
                              **this.lvThreads.Invoke(upd, new object\[\] { data });**
                          }
                          else
                          {
                              //This means that calling of lvThreads is in main thread
                              ListViewItem lvitem = (ListViewItem)data\[0\];
                              this.lvThreads.Items\[lvitem.Text\].SubItems\[1\].Text = Convert.ToString(data\[1\]);
                          }
                      }
                  

                  I am getting this error "Parameter count mismatch" in the bold line...

                  M Offline
                  M Offline
                  martin_hughes
                  wrote on last edited by
                  #9

                  I suspect that the exception is not being thrown here:

                  don234564 wrote:

                  this.lvThreads.Invoke(upd, new object[] { data });

                  but somewhere here:

                  don234564 wrote:

                  ListViewItem lvitem = (ListViewItem)data[0]; this.lvThreads.Items[lvitem.Text].SubItems[1].Text = Convert.ToString(data[1]);

                  And what you're actually experiencing is one of the whole panoply of wonders associated with multi-threading.

                  My Bookmarks I clicked the link. In an instant I was transported 15 years back in time.

                  J 1 Reply Last reply
                  0
                  • M martin_hughes

                    I suspect that the exception is not being thrown here:

                    don234564 wrote:

                    this.lvThreads.Invoke(upd, new object[] { data });

                    but somewhere here:

                    don234564 wrote:

                    ListViewItem lvitem = (ListViewItem)data[0]; this.lvThreads.Items[lvitem.Text].SubItems[1].Text = Convert.ToString(data[1]);

                    And what you're actually experiencing is one of the whole panoply of wonders associated with multi-threading.

                    My Bookmarks I clicked the link. In an instant I was transported 15 years back in time.

                    J Offline
                    J Offline
                    Juvil John
                    wrote on last edited by
                    #10

                    I have not actually found out whats really causing this but I re design my code and somehow got this to work. here's what i'm using now.

                    private delegate void _Post_Result(object data);

                        private void Post\_Result(object data)
                        {
                            if (lvThreads.InvokeRequired)
                            {
                                //This means that calling was accessed in separate thread
                                upd = new \_Post\_Result(Post\_Result);
                                this.lvThreads.Invoke(upd, new object\[\] { data });
                            }
                            else
                            {
                                //This means that calling of lvThreads is in main thread
                                lvitem = (ListViewItem)data;
                                this.lvThreads.Items\[lvitem.Index\].SubItems\[1\].Text = 
                                    Convert.ToString(Convert.ToInt32(lvitem.SubItems\[1\].Text) + 1);
                            }
                        }
                    
                    1 Reply Last reply
                    0
                    • W Wendelius

                      Never run into this situation but could you try what happens if you define the delegate as public. Also as others pointed out, I don't see a reason to create a new array.

                      The need to optimize rises from a bad design.My articles[^]

                      J Offline
                      J Offline
                      Jon Rista
                      wrote on last edited by
                      #11

                      You have to create a new array because the outer array is the array of parameters TO PASS TO the called function, and the inner array IS the first parameter OF the called function. Since the called function takes an array as its first parameter...

                      W 1 Reply Last reply
                      0
                      • J Jon Rista

                        You have to create a new array because the outer array is the array of parameters TO PASS TO the called function, and the inner array IS the first parameter OF the called function. Since the called function takes an array as its first parameter...

                        W Offline
                        W Offline
                        Wendelius
                        wrote on last edited by
                        #12

                        Wouldn't casting work in this situation? this.lvThreads.Invoke(upd, (object[])data);

                        The need to optimize rises from a bad design.My articles[^]

                        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