创建异步任务队列 - 使用BlockingCollection C#

问题描述 投票:0回答:1

我有一个WPF C#应用程序,它使用API​​向Facebook和Twitter发送消息。为此,我有一个主静态类,我可以发送字符串消息和一些参数。当用户间歇性地发出通知时,代码工作正常。但是,当用户立即发出多个通知时,它不起作用。我想要的是:

  • 按FIFO顺序一次一个地发布公告
  • 不要破坏GUI
  • 允许请求排队。
  • 允许在程序中的任何位置随时提出请求

我已经研究过使用BlockingCollection,但没有太多运气了解如何使它工作。

这是我目前的代码,我希望尽可能接近这个:

class PublishAnnouncement {
 //This function is callled upon in many parts of the program and acts as a general publisher
 public static void PostAnnoucment(string message, string TwAccountKey, string FbAccountKey, string[] JourneyRefID, double latness, MainWindow mainWindow) {
  //First it is published to facebook
  BackgroundWorker FacebookWorker = new BackgroundWorker();
  FacebookWorker.DoWork += (obj, e) => FacebookDoWork(message, FbAccountKey);
  FacebookWorker.RunWorkerAsync();

  //Then it is published to twitter - This is where it appears to fail
  BackgroundWorker TwitterWorker = new BackgroundWorker();
  TwitterWorker.DoWork += (obj, e) => TwitterDoWork(message, TwAccountKey, JourneyRefID, latness, mainWindow, 0);
  TwitterWorker.RunWorkerAsync();
 }

 private static void FacebookDoWork(string message, string FbAccountKey) {
  //STAGE 1 - Facebook
  //First the program will attempt to post a Facebook post.
  try {
   //If it is to be posted by one of the additional Facebook Pages and 
   //not by the default page.

   var client = new RestClient("https://graph.facebook.com/v3.0/");

   var request = new RestRequest("{pageId}/feed", Method.POST);
   request.AddParameter("message", message); // adds to POST or URL querystring based on Method
   request.AddParameter("access_token", Properties.Settings.Default.FBPageAccessToken);
   request.AddUrlSegment("pageId", Properties.Settings.Default.FBPageID); // replaces matching token in request.Resource
   IRestResponse response = client.Execute(request);

   if (response.IsSuccessful == false) {
    Console.WriteLine(response.Content);
    Console.WriteLine("");
   }

  } catch (Exception ex) {
   Console.WriteLine(ex.ToString());
  }
 }

 private static void TwitterDoWork(string message, string TwAccountKey, string[] JourneyRefID, double latness, MainWindow mainWindow, int Attempts) {
  //STAGE 2 - Twitter
  //Once a Facebook post has/has not been posted the program will attempt to send a tweet.
  try {

   Auth.SetUserCredentials(Properties.Settings.Default.TwConsumerKey, Properties.Settings.Default.TwConsumerSecret, Properties.Settings.Default.TwUserAccessToken, Properties.Settings.Default.TwUserAccessSecret);
   var tweet = Tweet.PublishTweet(message);
   foreach(var ID in JourneyRefID)
        AddTweetID(tweet.Id, ID, latness, mainWindow);


  } catch (Exception ex) {
   foreach(var ID in JourneyRefID)
        AddTweetID(0, ID, latness, mainWindow);

   Console.WriteLine(ex.Message);
  }
 }
}
c# .net wpf task tweetinvi
1个回答
1
投票

我建议在PostAnnouncement中使用Mutex。请参阅此处接受的答案 - 使用锁定示例:Usage of Mutex in c#

class PublishAnnouncement {
private static readonly object syncLock = new object();

public static void PostAnnoucment(string message, string TwAccountKey, string FbAccountKey, string[] JourneyRefID, double latness, MainWindow mainWindow) {
    lock(syncLock) {
        //First it is published to facebook
        BackgroundWorker FacebookWorker = new BackgroundWorker();
        FacebookWorker.DoWork += (obj, e) => FacebookDoWork(message, FbAccountKey);
        FacebookWorker.RunWorkerAsync();

        //Then it is published to twitter - This is where it appears to fail
        BackgroundWorker TwitterWorker = new BackgroundWorker();
        TwitterWorker.DoWork += (obj, e) => TwitterDoWork(message, TwAccountKey, JourneyRefID, latness, mainWindow, 0);
        TwitterWorker.RunWorkerAsync();

        //etc
    }
}

}

锁定一次只允许一个线程通过,堆叠所有线程。注意我已经锁定了静态。 (无论一次使用多少个类实例,所有调用线程都只生成并引用一个锁对象)。

如果要将锁移动到后台线程中:

class   PublishAnnouncement {
private static  readonly    object  syncLockForTwitter  =   new object();
private static  readonly    object  syncLockForFacebook =   new object();
//This  function    is  callled upon    in  many    parts   of  the program and acts    as  a   general publisher
public  static  void    PostAnnoucment(string   message,    string  TwAccountKey,   string  FbAccountKey,   string[]    JourneyRefID,   double  latness,    MainWindow  mainWindow) {
    //First it  is  published   to  facebook
    BackgroundWorker    FacebookWorker  =   new BackgroundWorker();
    FacebookWorker.DoWork   +=  (obj,   e)  =>  FacebookDoWork(message, FbAccountKey);
    FacebookWorker.RunWorkerAsync();

    //Then  it  is  published   to  twitter -   This    is  where   it  appears to  fail
    BackgroundWorker    TwitterWorker   =   new BackgroundWorker();
    TwitterWorker.DoWork    +=  (obj,   e)  =>  TwitterDoWork(message,  TwAccountKey,   JourneyRefID,   latness,    mainWindow, 0);
    TwitterWorker.RunWorkerAsync();
}

private static  void    FacebookDoWork(string   message,    string  FbAccountKey)   {
    lock(syncLockForFacebook)   {
    //STAGE 1   -   Facebook
    //First the program will    attempt to  post    a   Facebook    post.
        try {
            //If    it  is  to  be  posted  by  one of  the additional  Facebook    Pages   and 
            //not   by  the default page.

            var client  =   new RestClient("https://graph.facebook.com/v3.0/");

            var request =   new RestRequest("{pageId}/feed",    Method.POST);
            request.AddParameter("message", message);   //  adds    to  POST    or  URL querystring based   on  Method
            request.AddParameter("access_token",    Properties.Settings.Default.FBPageAccessToken);
            request.AddUrlSegment("pageId", Properties.Settings.Default.FBPageID);  //  replaces    matching    token   in  request.Resource
            IRestResponse   response    =   client.Execute(request);

            if  (response.IsSuccessful  ==  false)  {
                Console.WriteLine(response.Content);
                Console.WriteLine("");
            }

        }   catch   (Exception  ex) {
            Console.WriteLine(ex.ToString());
        }
    }
}

private static  void    TwitterDoWork(string    message,    string  TwAccountKey,   string[]    JourneyRefID,   double  latness,    MainWindow  mainWindow, int Attempts)   {
    lock(syncLockForTwitter)    {
    //STAGE 2   -   Twitter
    //Once  a   Facebook    post    has/has not been    posted  the program will    attempt to  send    a   tweet.
        try {

            Auth.SetUserCredentials(Properties.Settings.Default.TwConsumerKey,  Properties.Settings.Default.TwConsumerSecret,   Properties.Settings.Default.TwUserAccessToken,  Properties.Settings.Default.TwUserAccessSecret);
            var tweet   =   Tweet.PublishTweet(message);
            foreach(var ID  in  JourneyRefID)
                                AddTweetID(tweet.Id,    ID, latness,    mainWindow);


        }   catch   (Exception  ex) {
            foreach(var ID  in  JourneyRefID)
                                AddTweetID(0,   ID, latness,    mainWindow);

            Console.WriteLine(ex.Message);
        }
    }
}

}

© www.soinside.com 2019 - 2024. All rights reserved.