Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why in the commented area EndInvoke return null while that returns "Step is:number"

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication28
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            ParameterizedThreadStart start = new ParameterizedThreadStart(startthread);
            System.Threading.Thread u;
            int i = 0;
            while (i < 100)
            {
                u = new System.Threading.Thread(start);
                u.Start(i);

                Thread.Sleep(1000);


                lock (this)
                {
                    try
                    {
                        EndInvoke(g);   // return null??????why?
                    }
                    catch { }
                }

                i++;
            }
        }

        delegate void j(object h);
        j b;

        object h = new object();
        object y = new object();

        void startthread(object m)
        {
            lock (this)
            {
                b = new j(p1);

                if (label1.InvokeRequired)
                {
                    IAsyncResult g = label1.BeginInvoke(b, m);
                }
                else
                {
                    label1.Text = string.Format("Step is :{0}", h.ToString());
                }
            }
        }

        void p1(object h)
        {

            label1.Text = string.Format("Step is :{0}", h.ToString());

        }

        IAsyncResult g;
    }
}
Posted
Updated 6-Feb-11 3:55am
v2
Comments
#realJSOP 6-Feb-11 9:45am    
Your variable names absolutely suck donkey balls.

1 solution

The delegate has a void return type, so what exactly did you expect it to return?

If you want it to return the string you set the label to, change your delegate to:

C#
delegate string j(object h);


And then do this:

C#
string p1(object h)
{
    return(label1.Text = string.Format("Step is :{0}", h.ToString()));
}


Oh, and please choose better variable names. It's not 1987 any more :-)
 
Share this answer
 
v4
Comments
Espen Harlinn 6-Feb-11 10:36am    
Nicely spotted, my 5
Nish Nishant 6-Feb-11 10:40am    
Thank you, Espen!
Sergey Alexandrovich Kryukov 6-Feb-11 12:45pm    
That is correct, my 5.
Did you see OP previous question. For the very first time I refrained to answer, because I don't see understanding of previous answers on the topic. Do you have an idea what could help OP?
--SA
Nish Nishant 6-Feb-11 13:35pm    
Thanks Kryukov, and no, I have not see his other thread.
Sergey Alexandrovich Kryukov 6-Feb-11 17:37pm    
You good click at OP's profile -> Question and see this funny pattern in most recent group of question. Similar wrong lines re-appear again and again, mixed up with more and more "hi-tech" techniques; then general questions go, and again, almost the same wrong approach... Kind of lack of connection between theory and practice or I don't know what. That's why I quit answering, just don't know how to proceed...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900