0

在 Monodroid 中异步调用 Web 服务时出现问题。调用似乎正常工作,但每次我处理响应时应用程序都会崩溃。当我在模拟器中运行程序和在手机上运行程序时(Samsung Galaxy S w 2.2.1 FROYO.XWJS8),程序的行为有所不同。我正在考虑自己创建线程,但不知道它是否能解决问题。在 Windows Phone 7 应用程序中使用时,相同的代码可以正常工作。

Async 方法调用的代码是(注意:实际上 ShowMessage 调用写入 Android.Util.Log.Debug)

private void callws(string _input)
{
     MessageBox.GetInstance().ShowMessage("Search async started, input: " + _input); 
     m_waitingrequest = new RequestStatus() { Waiting = true, NewestInput = _input, OriginalInput = _input };
     connectormobile.UserInformation ui = new connectormobile.UserInformation() 
        { UserName = m_appsettings.GetValue<string>(AppSettings.WS_USERNAME_NAME), Password = m_appsettings.GetValue<string>(AppSettings.WS_PASSWORD_NAME) };
     MessageBox.GetInstance().ShowMessage("Username: " + ui.UserName + " Password: " + ui.Password);
     m_client.SearchAsync(ui, _input);
     MessageBox.GetInstance().ShowMessage("After search async call, input: " + _input);
}

搜索异步结果函数以:

void m_client_SearchCompleted(object sender, connectormobile.SearchCompletedEventArgs e)
{
             MessageBox.GetInstance().ShowMessage("Search async completed");
            SearchCache.CacheElement element = new SearchCache.CacheElement();
            element.SearchCriteria = m_waitingrequest.OriginalInput;
            element.PartialResponse = e.Result.PartialResponse;

            if (e.Result.CompanyNameInfoArray == null)
                element.Rows = new List<connectormobile.CompanyNameInfo>();
            else
                element.Rows = e.Result.CompanyNameInfoArray.ToList();
            MessageBox.GetInstance().ShowMessage("Search async returned, partial response: " + e.Result.PartialResponse
                + " row count: " + element.Rows.Count + " return value: " + e.Result.ReturnValue.ErrorDescriptionFi);
}

这是程序行为不同的地方。在模拟器中,代码永远不会到达 SearchCompleted 的第一行。但在我的手机中,SearchCompleted 功能似乎通过了(至少我所有的调试行都在跟踪中),但之后用户界面冻结了。(一分钟后它说进程没有响应)

4

1 回答 1

0

您可能试图从后台线程而不是 UI 线程修改 UI。使用 RunOnUIThread () 在正确的线程上执行您的 UI 逻辑:

http://mono-android.net/Documentation/Guides/Writing_Responsive_Applications

于 2011-04-11T15:27:21.283 回答