2

Im trying to simply get all of the recent questions using Windows Phone 7.

But I keep getting an ObjectDisposedException

My Code is not complicated:

public void BeginGetAllRecentQuestions()
    {
        _context.Options.LazyLoadingEnabled(true);
        _context.Initialized += ContextInitialised;
        _context.Initialize(true);
    }

private void ContextInitialised(object sender, EventArgs e)
    {
        QuestionsQuery questionsQuery = _context.Official.StackOverflow.Questions.PageSize(20);
        if (GotAllRecentQuestions != null)
            GotAllRecentQuestions(questionsQuery.Sort(QuestionSort.Creation).Order(SortOrder.Desc).ToList());
    }

private void QuestionsReceived(List<Question> questions)
    {
        Debug.WriteLine(questions.Count);
        this.RecentQuestions = questions;
    }

And thats it, but I keep getting the exception, with the stack trace:

at System.Threading.EventWaitHandle.Set() at Soapi.ApiContext.<>c_DisplayClass12.b_e(IAsyncResult a) at System.Net.Browser.ClientHttpWebRequest.<>c_DisplayClassd.b_b(Object state2) at System.Threading.ThreadPool.WorkItem.doWork(Object o) at System.Threading.Timer.ring()

which means nothing to me...

any ideas?

4
  • Mark - support for Soapi.CS is @ bitbucket.org/bitpusher/soapi.cs/issues. This post is likely to garner negative attention from mods and others. Go ahead and post it there with some more details and I will help you figure it out. Commented Sep 16, 2010 at 7:05
  • 2
    ok, sorry about that, but really a down vote?
    – Mark
    Commented Sep 16, 2010 at 9:22
  • ok - try the code below with the 1.0.1.3 binaries bitbucket.org/bitpusher/soapi.cs/downloads - thanks for the bug Commented Sep 16, 2010 at 11:20
  • expect things like that, mark, but don't let it bother you. apparently the friendly suggestion of the first comment was not enough for them and they wanted to spank you. ;-) Commented Sep 16, 2010 at 13:50

1 Answer 1

1

As I said in comments, you will want to direct support issues to the bitbucket site, but my initial observation is:

You need only 1 ApiContext, typically a class field, and it should be initialized in the Loaded event of your page.

From that point, simply use the same ApiContext for all of your calls.

Also, Query methods are synchronous and must be called on another thread than that of the UI.

Update: You have indeed uncovered a bug introduced into the ApiContext.Initialize(async) code.

I have fixed this and will post new binaries within the hour.

Here is a simple example that will work with the Soapi.CS-1.0.1.3-binaries.zip when they are uploaded.

using System;
using System.Linq;
using System.Threading;
using System.Windows;
using Microsoft.Phone.Controls;
using Soapi;
using Soapi.Parameters;

namespace MarksIssue
{
    public partial class MainPage : PhoneApplicationPage
    {
        #region Fields

        private ApiContext _context;

        #endregion

        #region Constructors

        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        #endregion

        #region Private Methods

        private void _context_Initialized(object sender, EventArgs e)
        {
            // get off the ui thread
            ThreadPool.QueueUserWorkItem(delegate
                {
                    var questions = _context
                        .Official
                        .StackOverflow
                        .Questions
                        .PageSize(20)
                        .Sort(QuestionSort.Creation)
                        .Order(SortOrder.Desc)
                        .ToList();

                    // marshal results back onto the UI thread
                    Dispatcher.BeginInvoke(() => { listBox1.ItemsSource = questions.Select(q => new { q.Title }); });
                });
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            _context = new ApiContext("qgAq_KfDu0KYzlNG-qaTuw")
                .Options
                .LazyLoadingEnabled(true)
                .Context;

            _context.Initialized += _context_Initialized;
            _context.Initialize();
        }

        #endregion

    }
}
2
  • @mark ok - try the code below with the 1.0.1.3 binaries bitbucket.org/bitpusher/soapi.cs/downloads Commented Sep 16, 2010 at 11:28
  • Would this sample work if you didn't select the Title property and therefore create a list of String objects but instead set the ItemsSource to a list of Question objects and databind to the Title property? Commented Nov 1, 2010 at 21:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .