Multi Threading, Task.Run Error 'The call is ambiguous between the following methods or properties'

Emre BEGEN picture Emre BEGEN · Feb 25, 2015 · Viewed 7.4k times · Source

When I try to build project the following error message is displayed.

The call is ambiguous between the following methods or properties: 'System.Threading.Tasks.Task.Run(System.Action)' and 'System.Threading.Tasks.Task.Run(System.Func)'

How can i fix this?

public static class MaintananceManager
    {
        private static ThreadSafeSocialMediaList<Post> PostList = new ThreadSafeSocialMediaList<Post>();
        private static ThreadSafeSocialMediaList<SocialUser> SocialUserList = new ThreadSafeSocialMediaList<SocialUser>();

        private static List<IPersistentProvider> providers = new List<IPersistentProvider>();
        private const int CACHESIZE = 1000;

        static MaintananceManager()
        {
            providers.Add(new SqlServerProvider());
            Task.Run(startMaintaining);

        }
        public static void PersistPosts(IEnumerable<Post> posts) 
        {
            PostList.AddRange(posts);
        }
        public static void PersistSocialUsers(IEnumerable<SocialUser> users)
        {
            SocialUserList.AddRange(users);
        }

        public static SocialUser GetSocialUser(SocialMediaType mediaType,string socialMediaId)
        {
            foreach(var provider in providers)
            {
                try
                {
                    return provider.GetSocialUser(mediaType, socialMediaId);
                }
                catch
                {

                }

            }
            return null;
        }
        private static void persistPosts()
        {
            var liste = PostList.Flush();
            foreach(var provider in providers)
            {
                provider.SavePosts(liste);
            }
        }
        private static void persistUsers()
        {
            var liste = SocialUserList.Flush();
            foreach (var provider in providers)
            {
                provider.SaveSocialUsers(liste);
            }
        }
        private static void startMaintaining()
        {
            while(true)
            {
                if (PostList.Count > CACHESIZE)
                    persistPosts();
                if (SocialUserList.Count > CACHESIZE)
                    persistUsers();

                Thread.Sleep(60000);

            }

        }

    }

Answer

Grant Winney picture Grant Winney · Feb 25, 2015

There's a few ways you could pass this as a valid Action:

Task.Run(() => startMaintaining());

Task.Run((Action)startMaintaining);

Task.Run(new Action(startMaintaining));