0

Im Grunde, was passiert, wenn versucht wird, eine Variable einer verwalteten Klasse (UWP) zu ändern, stürzt es ab. Darüber hinaus scheint es nur zu Abstürzen, wenn ich versuche, eine Variable zu ändern, die mit dem App-Namespace erstellt wird. Mit anderen Worten, wenn ich einen neuen Namespace und eine verwaltete Klasse erstelle, kann ich Variablen einfach ändern.verwaltete C++ Klassen Absturz in create_task

crash

void ASynTaskCategories(void) 
    { 
     concurrency::task_completion_event<Platform::String^> taskDone; 
     MainPage^ rootPage = this; 
     UberSnip::HELPER::ASYNC_RESPONSE^ responseItem = ref new UberSnip::HELPER::ASYNC_RESPONSE(); 
     create_task([taskDone, responseItem, rootPage]() -> Platform::String^ 
     { 

      //UBERSNIP API 0.4 
      UberSnip::UBERSNIP_CLIENT* UberSnipAPI = new UberSnip::UBERSNIP_CLIENT(); 

      UberSnipAPI->Http->RequestURL = "http://api.ubersnip.com/categories.php"; 
      UberSnipAPI->Http->request(); 
      taskDone.set(UberSnipAPI->Client->BodyResponse); 
      responseItem->Body = UberSnipAPI->Client->BodyResponse; 
      return "(done)"; 

     }).then([taskDone, responseItem, rootPage](Platform::String^ BodyResponse) { 
      Platform::String^ BR = responseItem->Body; 


      cJSON* cats = cJSON_Parse(_string(BR)); 
      cats = cats->child; 
      int *cat_count = new int(cJSON_GetArraySize(cats)); 
      rootPage->Categories->Clear(); 

      GENERIC_ITEM^ all_item = ref new GENERIC_ITEM(); 
      all_item->Title = "All"; 

      rootPage->Categories->Append(all_item); 
      for (int i = 0; i < *cat_count; i++) { 
       cJSON* cat = new cJSON(); 
       cat = cJSON_GetArrayItem(cats, i); 

       string *track_title = new string(cJSON_GetObjectItem(cat, "name")->valuestring); 

       GENERIC_ITEM gitem; 
       gitem.Title = "Hi"; 

       GENERIC_ITEM^ ubersnipCategory = ref new GENERIC_ITEM(); 
       ubersnipCategory->Title = _String(*track_title); 

       rootPage->Categories->Append(ubersnipCategory); 
      } 
     }); 

Das ist nicht

nicht abstürzen
UberSnipAPI->Http->RequestURL = "http://api.ubersnip.com/categories.php"; 

aber dieses tut

all_item->Title = "All"; 

ich fast sicher bin es etwas zu tun hat, mit ihm in der apps Verzuges Namespace und es wird außerhalb des Hauptthreads zugegriffen ... Zumindest ist es das, was es ist ems wie das ist wirklich der einzige Unterschied neben der eigentlichen Klasse.

So sieht GENERIC_ITEM aus.

[Windows::UI::Xaml::Data::Bindable] 
public ref class GENERIC_ITEM sealed { 
private: 
    Platform::String^ _title = ""; 
    Platform::String^ _description; 
    Windows::UI::Xaml::Media::ImageSource^ _Image; 

    event PropertyChangedEventHandler^ _PropertyChanged; 

    void OnPropertyChanged(Platform::String^ propertyName) 
    { 
     PropertyChangedEventArgs^ pcea = ref new PropertyChangedEventArgs(propertyName); 
     _PropertyChanged(this, pcea); 
    } 
public: 
    GENERIC_ITEM() { 

    }; 

    property Platform::String^ Title { 
     Platform::String^ get() { 
      return this->_title; 
     } 

     void set(Platform::String^ val) { 
      this->_title = val; 
      OnPropertyChanged("Title"); 
     } 
    } 

    property Platform::String^ Description { 
     Platform::String^ get() { 
      return this->_description; 
     } 

     void set(Platform::String^ val) { 
      this->_description = val; 
      OnPropertyChanged("Description"); 
     } 
    } 

    void SetImage(Platform::String^ path) 
    { 
     Windows::Foundation::Uri^ uri = ref new Windows::Foundation::Uri(path); 
     _Image = ref new Windows::UI::Xaml::Media::Imaging::BitmapImage(uri); 
    } 

}; 

Ich weiß, dass es kein Problem mit der Klasse ist, weil es perfekt funktioniert gut, wenn ich diesen exakt gleichen Code auf dem ursprünglichen Thread ausgeführt.

Irgendwelche Vorschläge? Vielen Dank! : D

Antwort

0

Nach mehreren Stunden herausgefunden! Falls dieses Problem bei einem anderen Benutzer auftritt, müssen Sie den Dispatcher nur verwenden, um Code auf der Benutzeroberfläche auszuführen, der außerhalb des Benutzeroberflächenthreads nicht verfügbar ist.

void loadCats(UberSnip::HELPER::ASYNC_RESPONSE^ responseItem, MainPage^ rootPage) { 
    Platform::String^ BR = responseItem->Body; 


    cJSON* cats = cJSON_Parse(_string(BR)); 
    cats = cats->child; 
    int *cat_count = new int(cJSON_GetArraySize(cats)); 
    rootPage->Categories->Clear(); 

    GENERIC_ITEM^ all_item = ref new GENERIC_ITEM(); 
    all_item->Title = "All"; 

    rootPage->Categories->Append(all_item); 
    for (int i = 0; i < *cat_count; i++) { 
     cJSON* cat = new cJSON(); 
     cat = cJSON_GetArrayItem(cats, i); 

     string *track_title = new string(cJSON_GetObjectItem(cat, "name")->valuestring); 

     GENERIC_ITEM gitem; 
     gitem.Title = "Hi"; 

     GENERIC_ITEM^ ubersnipCategory = ref new GENERIC_ITEM(); 
     ubersnipCategory->Title = _String(*track_title); 

     rootPage->Categories->Append(ubersnipCategory); 
    } 
} 

void ASyncTaskCategories(void) 
{ 
    concurrency::task_completion_event<Platform::String^> taskDone; 
    MainPage^ rootPage = this; 
    UberSnip::HELPER::ASYNC_RESPONSE^ responseItem = ref new UberSnip::HELPER::ASYNC_RESPONSE(); 

    auto dispatch = CoreWindow::GetForCurrentThread()->Dispatcher; 
    auto op2 = create_async([taskDone, responseItem, rootPage, dispatch] { 
     return create_task([taskDone, responseItem, rootPage, dispatch]() -> Platform::String^ 
     { 

      //UBERSNIP API 0.4 
      UberSnip::UBERSNIP_CLIENT* UberSnipAPI = new UberSnip::UBERSNIP_CLIENT(); 

      UberSnipAPI->Http->RequestURL = "http://api.ubersnip.com/categories.php"; 

      try{ 
       UberSnipAPI->Http->request(); 
      } 
      catch (...) { 
       printf("Error"); 
      } 

      int err = UberSnip::UTILS::STRING::StringToAscIIChars(UberSnipAPI->Client->BodyResponse).find("__api_err"); 

      if (err < 0) { 
       if (UberSnip::UTILS::STRING::StringToAscIIChars(UberSnipAPI->Client->BodyResponse).length() < 3) { 
        return; 
       } 
      } 
      taskDone.set(UberSnipAPI->Client->BodyResponse); 
      responseItem->Body = UberSnipAPI->Client->BodyResponse; 
      dispatch->RunAsync(Windows::UI::Core::CoreDispatcherPriority::High, ref new Windows::UI::Core::DispatchedHandler([=]() 
      { 
       rootPage->loadCats(responseItem, rootPage); 
      })); 

      for (int i = 0; i < 100;) { 

      } 


      return "(done)"; 

     }).then([taskDone, responseItem, rootPage](Platform::String^ BodyResponse) { 
     }); 

    }); 

     //rootPage->loadCats(responseItem, rootPage); 
}