多线程,Task.Run 错误 'The call is ambiguous between the following methods or properties'
Multi Threading, Task.Run Error 'The call is ambiguous between the following methods or properties'
当我尝试构建项目时,显示以下错误消息。
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));
当我尝试构建项目时,显示以下错误消息。
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));