Click here to Skip to main content
15,359,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried a simple demo at an article on here, and got into an issue, which I think it is simple:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace thread02
{
    public delegate bool MyDelegate(int i, string str);

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("testing threading concept:");
            MyDelegate delInstance = new MyDelegate(MyFunction);
            

            IAsyncResult ref = delInstance.BeginInvoke(100,"I am in Delegate Thread", null, null); //<== can not compile at this statement

            for (int i = 0; i < 100; i++)
                Console.WriteLine("I am in Main Thread {0}", i);
            bool status = delInstance.EndInvoke(ref);

            Console.ReadKey();
        }
        public static bool MyFunction(int count, string str)
        {
            for (int i = 0; i < count; i++)
                Console.WriteLine(str + " {0}", i);

            return true;
        }

    }
}


the error message is:
Error	CS0119	'IAsyncResult' is a type, which is not valid in the given context


What I have tried:

tried microsoft site and google. can not find the reason why.
Posted
Updated 4-Mar-22 10:54am

1 solution

ref is a keyword: you can't use it as a variable name:
C#
IAsyncResult ref = delInstance.BeginInvoke(100,"I am in Delegate Thread", null, null); 
Change the name, it should compile.
   
Comments
Southmountain 4-Mar-22 17:42pm
   
thank you! it works.

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