方法尚未完成,但再次调用

时间:2015-09-02 15:46:23

标签: android multithreading xamarin

我在Xamarin上创建Android应用程序,使用socket获取。我的问题是我通过套接字获取数据并检查收到的数据以显示推送通知,但有时数据经常出现并且检查方法没有再次检查数据和调用,因此应用程序崩溃。我该怎么用来解决这个问题?

这里我从scoket获取新数据:

SocketClass.OnMessageReceived += (sender, list) =>
            {
                var th = new Thread(() => SocketUpdate(list));
                th.Start();
            };

private void SocketUpdate(List<QuoteData> list)
        {
            _viewActivity.RunOnUiThread(() => UpdateRealTimeQuotesInfo(list));
        }

此刻我尝试使用这种方式:

private static void UpdateRealTimeQuotesInfo(List<QuoteData> list)
        {
            //Some operations with data in adapter 
            _adapter.UpdateAdapter();

            ThreadPool.QueueUserWorkItem(o => CheckAlertToPerfomance.GetAlerts(_viewActivity));
        }

检查方法:

public static void GetAlerts(Context context)
        {
            try
            {

                var value = SharedPreferencesDevice.GetPreferencesByName(SharedPreferencesDevice.PreferenceName.FiledToSaveAlarm);

                if (!value.Any()) return;

                var quotesNewList = MainDataClass.MainDataList; //QuotesEntities.ListQuotesEntities;

                foreach (var elem in value)
                {
                    NotifyInfoType obj = NotifyProcessFor.JsonDeserialize((string)elem.Value);  

                    foreach (var item in quotesNewList.Where(x => x.Symbol == obj.Symbol).ToList())
                    {
                        if (obj.TNotify == TypeNotifyEnum.Price)
                        {
                            if ((obj.TDirection==TypeDirectionEnum.Up && (double.Parse(item.Bid) + double.Parse(item.Ask)) / 2 > double.Parse(obj.Value))||
                                (obj.TDirection==TypeDirectionEnum.Down && (double.Parse(item.Bid) + double.Parse(item.Ask)) / 2 < double.Parse(obj.Value)))                                                        
                                ProcessForNotification(elem,obj.TNotifyStr,obj.TDirectionStr, context);                            
                        }
                        if (obj.TNotify == TypeNotifyEnum.Change)
                        {
                            if ((obj.TDirection==TypeDirectionEnum.Up&&double.Parse(item.Change)>double.Parse(obj.Value))||
                                (obj.TDirection==TypeDirectionEnum.Down&&double.Parse(item.Change)<double.Parse(obj.Value)))                           
                                ProcessForNotification(elem,obj.TNotifyStr,obj.TDirectionStr, context);                              
                        }
                        if (obj.TNotify == TypeNotifyEnum.Percent)
                        {
                            if ((obj.TDirection == TypeDirectionEnum.Up && (double)item.Percent > double.Parse(obj.Value)) ||
                                (obj.TDirection == TypeDirectionEnum.Down && (double)item.Percent < double.Parse(obj.Value)))
                                ProcessForNotification(elem, obj.TNotifyStr, obj.TDirectionStr, context);
                        }
                    }
                }
                foreach (var elem in _keyToDelete)
                {
                    SharedPreferencesDevice.RemovePreferanceByName(SharedPreferencesDevice.PreferenceName.FiledToSaveAlarm, elem);
                }

            }
            catch (Exception ex)
            {
                Console.Write (ex.Message);
            }
        }

调用显示推送的方法:

private static void ProcessForNotification(KeyValuePair<string, object> key, string type, string direction, Context context)
        {
            var notif = NotificationForAndroid.GetInstance(Application.Context);
            notif.DisplayNotification(key, type, direction, context);
            _keyToDelete.Add(key.Key);
        }

DisplayNotification方法只是一种创建和显示推送通知的简单方法。 并且应用程序在GetAlerts方法上崩溃,但它没有显示任何异常。

感谢。

1 个答案:

答案 0 :(得分:0)

我解决了,这是socket的一个问题,它工作不正确。