如何批处理队列记录并在不同的线程中执行它们并等到它结束?

时间:2016-04-05 11:05:50

标签: c# multithreading pushsharp

我正在使用push sharp版PushSharp 4.0.4。

我在Windows应用程序中使用它。

我有三种主要方法

1- BroadCastToAll

2- BrodcatsToIOS

3- BrodcatsToAndriod

我有一个按钮calld发送。在按钮的单击事件上。我打电话给

BroadCastToAll功能。

 private void btnSend_Click(object sender, EventArgs e)
 {
    var url = "www.mohammad-jouhari.com"
    var promotion = new Promotion ();
    BroadCastToAll(promotion, url);
 }

这是BrodcastToAll函数

 public void BroadCastToAll(Promotion promotion, string url)
 {
    var deviceCatalogs = GetDeviceCatalog();
    BroadCastToIOS(promotion, url, deviceCatalogs.Where(d => d.OS == "IOS").ToList());
    BroadCastToAndriod(promotion, url, deviceCatalogs.Where(d => d.OS == "Android").ToList());
 }

这是BrodcastToIOS功能

 public void BroadCastToIOS(Promotion promotion, string url, List<DeviceCatalog> deviceCatalogs)
 {
    if (deviceCatalogs.Count == 0)
       return;
       lock (_lock)// Added this lock because there is a potential chance that PushSharp callback execute during registering devices
       {
          QueueAllAppleDevicesForNotification(promotion, url, deviceCatalogs, logsMessage);
       }
 }

这是BrodcastToAndriod功能

 public void BroadCastToAndriod(Promotion promotion, string url, List<DeviceCatalog> deviceCatalogs)
 {
    if (deviceCatalogs.Count == 0)
        return;
        lock (_lock)// Added this lock because there is a potential chance that PushSharp callback execute during registering devices
        {
           QueueAllGcmDevicesForNotification(promotion, url, deviceCatalogs, logsMessage);
        }
 }

这是QueueAllAppleDevicesForNotification函数

  private void QueueAllAppleDevicesForNotification(Promotion promotion, string url, List<DeviceCatalog> deviceCatalogs)
        {
            var apnsServerEnviroment = UseProductionCertificate ? ApnsConfiguration.ApnsServerEnvironment.Production : ApnsConfiguration.ApnsServerEnvironment.Sandbox;
            var fileService = new FileService();
            var filePath = Application.StartupPath+ "/Certifcates/" +  (UseProductionCertificate ? "prod.p12" : "dev.p12");
            var buffer = fileService.GetFileBytes(filePath);
            var config = new ApnsConfiguration(apnsServerEnviroment, buffer, APPLE_CERTIFICATE_PWD);
            apnsServiceBroker = new ApnsServiceBroker(config);
            apnsServiceBroker.OnNotificationFailed += (notification, aggregateEx) => {
                aggregateEx.Handle (ex => {
                    // Log the Resposne
                });

            };
            apnsServiceBroker.OnNotificationSucceeded += (notification) => {
                   // Log The Response 
            };
            apnsServiceBroker.Start();
            foreach (var deviceToken in deviceCatalogs) {
                var title = GetTitle(promotion, deviceToken);
                //title += DateTime.UtcNow.TimeOfDay.ToString();
                var NotificationPayLoadObject = new NotificationPayLoadObjectApple();
                NotificationPayLoadObject.aps.alert = title;
                NotificationPayLoadObject.aps.badge = 0;
                NotificationPayLoadObject.aps.sound = "default";
                NotificationPayLoadObject.url = url;
                var payLoad = JObject.Parse(JsonConvert.SerializeObject(NotificationPayLoadObject));
                apnsServiceBroker.QueueNotification(new ApnsNotification
                {
                    Tag = this,
                    DeviceToken = deviceToken.UniqueID,
                    Payload = payLoad
                });
            }
            var fbs = new FeedbackService(config);
            fbs.FeedbackReceived += (string deviceToken, DateTime timestamp) =>
            {
                // This Token is no longer avaialble in APNS
                new DeviceCatalogService().DeleteExpiredIosDevice(deviceToken);
            };
            fbs.Check();

            apnsServiceBroker.Stop();
        }

这是QueueAllGcmDevicesForNotification

private void QueueAllGcmDevicesForNotification(Promotion promotion, string url, List<DeviceCatalog> deviceCatalogs, )
    {
        var config = new GcmConfiguration(ANDROID_SENDER_ID, ANDROID_SENDER_AUTH_TOKEN, ANDROID_APPLICATION_ID_PACKAGE_NAME);
        gcmServiceBroker = new GcmServiceBroker(config);
        gcmServiceBroker.OnNotificationFailed += (notification, aggregateEx) => {
            aggregateEx.Handle (ex => {
                // Log Response
                return true;
            });
        };

        gcmServiceBroker.OnNotificationSucceeded += (notification) => {
           // Log Response
        };
        var title = GetTitle(shopexPromotion);
        gcmServiceBroker.Start ();
        foreach (var regId in deviceCatalogs) {
            var NotificationPayLoadObject = new NotificationPayLoadObjectAndriod(url, title, "7", promotion.ImageUrl);
            var payLoad = JObject.Parse(JsonConvert.SerializeObject(NotificationPayLoadObject));
            gcmServiceBroker.QueueNotification(new GcmNotification
            {
                RegistrationIds = new List<string> { 
                    regId.UniqueID
                },
                Data = payLoad
            });
        }
        gcmServiceBroker.Stop();

    }

现在当我点击发送按钮时。该事件将开始执行。

将调用BrodcastToAll函数。我先调用BrodcastToIOS设备然后再调用BrodcatsToAndriod。

有什么方法可以调用BrodcastToIOS并等到所有设备都已排队并且库已推送通知并且回调事件完全触发然后开始执行BrodcastToAndriod功能?

我需要添加哪些代码?

是否还有办法批处理要排队的设备数量?

例如。

我们说我有1000台设备

500 IOS

500 Andriod

我可以为IOS排队100,100,100,100,100以及何时完成

我为Andriod排队100,100,100,100,100。

感谢任何帮助。

感谢。

1 个答案:

答案 0 :(得分:0)

默认情况下,对broker.Stop ()的调用将一直阻止,直到队列中的所有通知都已处理完毕。