草庐IT

c# - 多线程,Task.Run 错误 'The call is ambiguous between the following methods or properties'

coder 2024-05-24 原文

当我尝试构建项目时,显示以下错误消息。

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)'

我该如何解决这个问题?

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);

            }

        }

    }

最佳答案

有几种方法可以将其作为有效的 Action 传递:

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

Task.Run((Action)startMaintaining);

Task.Run(new Action(startMaintaining));

关于c# - 多线程,Task.Run 错误 'The call is ambiguous between the following methods or properties',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28724030/

有关c# - 多线程,Task.Run 错误 'The call is ambiguous between the following methods or properties'的更多相关文章

随机推荐