托管的c ++类在create_task中崩溃

时间:2016-04-14 00:26:33

标签: c++-cli win-universal-app windows-10-universal windows-10-desktop

基本上,正在发生的事情是在尝试更改托管类(UWP)的变量时,它会崩溃。此外,如果我尝试修改使用app命名空间创建的变量,它似乎只会崩溃。换句话说,如果我创建一个新的namspace和托管类,我可以很好地修改变量。

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);
            }
        });

这不会崩溃

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

但这个确实

all_item->Title = "All";

我几乎是肯定的,它与应用程序默认命名空间有关,它可以在主线程之外访问...至少那个看起来像是真的除了实际课程之外唯一的区别。

这就是GENERIC_ITEM的样子。

[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);
    }

};

我知道这个类没有问题,因为如果我在初始线程上运行这个完全相同的代码,它的工作完全正常。

有什么建议吗?谢谢! :d

1 个答案:

答案 0 :(得分:0)

几小时后想出来!如果其他人遇到此问题,您只需使用Dispatcher在UI线程外部不可用的UI上运行代码。

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);
}