Deadlock in Xamarin.Forms - c#

I've got a problem with my Xamarin App. The App uses a custom API to my website. I have not much experience in async/await methods.
The following code shows the App.xaml.cs:
public partial class App : Application
{
public static bool IsUserLoggedIn { get; set; }
public static UserModel currentLoggedInUser { get; set; }
public static List<ActionTypeModel> listTypes;
public static List<ActionSubTypeModel> listSubtypes;
public static List<UserModel> listFriends;
public static List<List<ActionModel>> listActiveActions;
public static List<ActionModel> listLastAction;
public App()
{
this.InitializeComponent();
APIHelper.InitializeClient();
StartApp().Wait();
}
private async Task StartApp()
{
//// DEBUG
//MyAccountStorage.Logout();
string username = await MyAccountStorage.GetUsername().ConfigureAwait(false);
string password = await MyAccountStorage.GetPassword().ConfigureAwait(false);
string user_id = await MyAccountStorage.GetId().ConfigureAwait(false);
if (username != null && password != null)
{
currentLoggedInUser = new UserModel();
if (user_id != null)
currentLoggedInUser.user_id = Convert.ToInt32(user_id);
currentLoggedInUser.username = username;
currentLoggedInUser.password = password;
bool isValid = false;
isValid = await AreCredentialsCorrect(0, currentLoggedInUser.username, currentLoggedInUser.password).ConfigureAwait(false);
if (isValid)
{
IsUserLoggedIn = true;
await FillLists().ConfigureAwait(false);
MainPage = new NavigationPage(await MyPage.BuildMyPage().ConfigureAwait(false));
}
else
{
IsUserLoggedIn = false;
MainPage = new NavigationPage(await LoginPage.BuildLoginPage().ConfigureAwait(false));
}
}
else
{
IsUserLoggedIn = false;
MainPage = new NavigationPage(await LoginPage.BuildLoginPage().ConfigureAwait(false));
}
}
private async Task FillLists()
{
listFriends = await DataControl.GetFriends(App.currentLoggedInUser.user_id, App.currentLoggedInUser.username, App.currentLoggedInUser.password).ConfigureAwait(false);
if (listFriends == null)
listFriends = new List<UserModel>();
listTypes = await DataControl.GetTypes(App.currentLoggedInUser.username, App.currentLoggedInUser.password).ConfigureAwait(false);
if (listTypes == null)
listTypes = new List<ActionTypeModel>();
listActiveActions = new List<List<ActionModel>>();
for (int i = 0; i < listTypes.Count; i++)
listActiveActions.Add(await DataControl.GetActiveActions(listTypes[i].action_type_id, currentLoggedInUser.user_id, currentLoggedInUser.username, currentLoggedInUser.password).ConfigureAwait(false));
listSubtypes = await DataControl.GetSubtypes(App.currentLoggedInUser.username, App.currentLoggedInUser.password).ConfigureAwait(false);
if (listSubtypes == null)
listSubtypes = new List<ActionSubTypeModel>();
listLastAction = await DataControl.GetLastAction(App.currentLoggedInUser.user_id, App.currentLoggedInUser.username, App.currentLoggedInUser.password).ConfigureAwait(false);
if (listLastAction == null)
listLastAction = new List<ActionModel>();
}
public static async Task<bool> AreCredentialsCorrect(int type, string user, string pass, string nick = "", string email = "")
{
List<UserModel> listUsers;
if (type == 1)
listUsers = await DataControl.CheckCredentials(1, user, pass, nick, email).ConfigureAwait(false);
else
listUsers = await DataControl.CheckCredentials(0, user, pass).ConfigureAwait(false);
if (listUsers != null)
if (listUsers.Any())
{
currentLoggedInUser = listUsers.First();
currentLoggedInUser.password = pass;
return true;
}
return false;
}
}
I have the API in DataControl.cs:
public static async Task<List<UserModel>> CheckCredentials(int type, string username, string pass, string email = "", string nickname = "")
{
string password = APIHelper.GetHashSha256(pass);
string url = string.Empty;
if (type == 0)
url = APIHelper.ApiClient.BaseAddress + "/account/login.php?username=" + username + "&password=" + password;
if (type == 1)
{
string nick = string.Empty;
if (string.IsNullOrEmpty(nickname) == false)
nick = "&nickname=" + nickname;
url = APIHelper.ApiClient.BaseAddress + "/account/signup.php?username=" + username + "&password=" + password + "&email=" + email + nick;
}
if (string.IsNullOrEmpty(url))
return null;
using (HttpResponseMessage response = await APIHelper.ApiClient.GetAsync(url).ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
List<UserModel> listUsers = JsonConvert.DeserializeObject<List<UserModel>>(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
return listUsers;
}
else
return null;
}
}
That's one of the different async methods. When I leave out the ConfigureAwait(false), I run into a deadlock. When I add it to the code I run into an error.
Could you please help me.

As #GSerg already wrote, you have to restructure the code. The App constructor must set the MainPage to some page. That one can be empty saying something like "Loading data".
Then you can start a background Task which retrieves the data you need.
When the data has been loaded, then you can update your page with the newly loaded data. But UI updates always have to happen on the UI thread. This is the same on all platforms. So you have to switch back with Device.BeginInvokeOnMainThread(...).
public App()
{
InitializeComponent();
APIHelper.InitializeClient();
MainPage = new LoadingPage();
// start a background thread
Task.Run(async () =>
{
try
{
await StartApp(); // load the data
// go back to the main thread
Device.BeginInvokeOnMainThread(() =>
{
// replace the MainPage with one which shows the loaded data
MainPage = new DataPage();
});
}
catch (Exception ex)
{
// handle the exception
}
});
}

You can use Task.Run(
//call your async method here
);
So in your case:
Task.Run(Startup);

Related

getter setter can't change textBox.Text control value in Winform c#

Why textBox3.text do not shows value _TextBoxRequestMsg. MessageBox opens and shows _TextBoxRequestMsg value OK, console prints too.
public partial class F_Main : Form
{
private string _TextBoxRequestMsg;
public string TextBoxRequestMsg
{
get { return textBox3.Text; }
set
{
_TextBoxRequestMsg = value;
MessageBox.Show(_TextBoxRequestMsg);
Console.WriteLine(_TextBoxRequestMsg);
textBox3.Text = _TextBoxRequestMsg;
}
}
public F_Main()
{
InitializeComponent();
}
}
public class CdataController : ApiController
{
F_Main mainForm = new F_Main();
public async Task<HttpResponseMessage> PostPayloadEventsOp(string SN, string table, string OpStamp)
{
using (var contentStream = await this.Request.Content.ReadAsStreamAsync())
{
contentStream.Seek(0, SeekOrigin.Begin);
using (var sr = new StreamReader(contentStream))
{
string results = sr.ReadToEnd();
mainForm.TextBoxRequestMsg = results;
}
}
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent("OK", System.Text.Encoding.UTF8);
response.Headers.CacheControl = new CacheControlHeaderValue()
{
MaxAge = TimeSpan.FromMinutes(2)
};
return response;
}
}
Your question states that your goal is to change textBox.Text control value in Winform and your code indicates that you want to do this by processing an HttpResponseMessage. Consider that the Form that owns the textBox3 control could await the response so that it can meaningfully process its content and assign the value to the text box.
For a minimal example, mock the API request:
public class MockCdataController : ApiController
{
public async Task<HttpResponseMessage> MockPostPayloadEventsOp(string SN, string table, string OpStamp)
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync("https://stackoverflow.com/q/75310027/5438626");
response.Content = new StringContent("OK", System.Text.Encoding.UTF8);
response.Headers.CacheControl = new CacheControlHeaderValue()
{
MaxAge = TimeSpan.FromMinutes(2)
};
return response;
}
}
}
The Form that is in possession of textBox3 could invoke something like this:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
buttonPost.Click += onPost;
}
private async void onPost(object? sender, EventArgs e)
{
try
{
UseWaitCursor = true;
buttonPost.BackColor = Color.LightGreen;
var response = await _controller.MockPostPayloadEventsOp("38D6FF5-F89C", "records", "Asgard");
if((response.Headers != null) && (response.Headers.CacheControl != null))
{
textBox3.Text = $"{response.Headers.CacheControl.MaxAge}";
}
}
finally
{
UseWaitCursor = false;
Cursor.Position = new Point(Cursor.Position.X + 1, Cursor.Position.Y);
}
}
MockCdataController _controller = new MockCdataController();
}

How I can start multiple download async in c#

my problem is the following: I just want to run x requests at the same time depending on the user.
Well, it seems to work fine when the MaxConcurrentDownloads variable is equal to 1, but when I increase it, say 10: I have to wait for the 10taches to finish for it to execute so that Console.WriteLine as to write, when it's supposed to run asynchronously, right?
Can you help me? Here is a minimalist version of my "problem" (Also I want to specify that I have no compiler or syntax errors)
main.c
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace test_client
{
class Program
{
private static client cli = new client();
private static readonly string PATH = #Directory.GetCurrentDirectory();
private static int concurrency = 100;
private static async Task<bool> MakeJOB(int pos)
{
return await cli.NewRequest<bool>((HttpClient client)=>
{
try
{
HttpClientHandler handler = null;
if (cli.handler != null)
handler = cli.GethandlerIndexed(pos);
client = new HttpClient(handler);
cli.AssignDefaultHeaders(client);
using (HttpResponseMessage response = client.GetAsync("https://api.my-ip.io/ip.txt").Result)
using (HttpContent content = response.Content)
Console.WriteLine(content.ReadAsStringAsync().Result + " / " + Task.CurrentId);
return true;
}
catch { /* exception .. */ return false; }
});
}
static void Main(string[] args)
{
ServicePointManager.DefaultConnectionLimit = 100;
MainAsync(args).GetAwaiter().GetResult();
Console.ReadLine();
}
static async Task MainAsync(string[] args)
{
cli.SetConcurrentDownloads(concurrency);
var t = new Task[concurrency];
int pos = 0;
for (int i = 0; i < t.Length; i++, pos++)
t[i] = MakeJOB(pos++);
await Task.WhenAll(t);
}
}
}
client.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.Collections.Concurrent;
using System.Threading;
namespace test_client
{
public class client
{
private readonly ConcurrentDictionary<string, HttpClient> Clients;
public SemaphoreSlim Locker;
private CancellationTokenSource TokenSource = new CancellationTokenSource();
public HttpClientHandler[] handler { get; set; }
public string[] address { get; set; }
public string[] port { get; set; }
public string[] username { get; set; }
public string[] password { get; set; }
public int MaxConcurrentDownloads { get; set; }
private void initializeHandler(string address = "", string port = "", string user = "", string pass = "")
{
initializeHandler(new string[] { string.Concat(address, ":", port, ":", user, ":", pass) });
}
private void initializeHandler(string[] proxies_client)
{
if (proxies_client == null || proxies_client.Length == 0)
return;
this.address = new string[proxies_client.Length];
this.port = new string[proxies_client.Length];
this.username = new string[proxies_client.Length];
this.password = new string[proxies_client.Length];
for (int i = 0; i < proxies_client.Length; i++)
{
var split = proxies_client[i].Split(new char[] { ':' });
this.address[i] = split[0] != "" ? split[0] : "";
this.port[i] = split[1] != "" ? split[1] : "";
this.username[i] = split[2] != "" ? split[2] : "";
this.password[i] = split[3] != "" ? split[3] : "";
}
var proxies = new WebProxy[proxies_client.Length];
NetworkCredential[] credential = new NetworkCredential[proxies_client.Length];
for (int i = 0; i < proxies_client.Length; i++)
{
if (this.username[i] != "")
credential[i] = new NetworkCredential(this.username[i], this.password[i]);
else
credential[i] = CredentialCache.DefaultNetworkCredentials;
}
const string protocol = "http://";
for (int i = 0; i < proxies.Length; i++)
{
if (this.address[i] != "")
{
var uri = proxies_client[i].Split(new char[] { ':' });
if (!uri[0].Contains(protocol))
uri[0] = string.Concat(protocol, uri[0]);
proxies[i] = new WebProxy()
{
Address = new Uri(string.Concat(uri[0], ":", uri[1])),
Credentials = credential[i],
};
}
};
this.handler = new HttpClientHandler[proxies.Length];
for (int i = 0; i < proxies.Length; i++)
{
if (proxies[i].Address.AbsoluteUri != "")
this.handler[i] = new HttpClientHandler() { Proxy = proxies[i] };
else
this.handler[i] = new HttpClientHandler();
}
}
public HttpClientHandler GethandlerIndexed(int index)
{
return (this.handler[index % this.handler.Length]);
}
public void SetConcurrentDownloads(int nb = 1)
{
Locker = new SemaphoreSlim(nb, nb);
}
public client(string[] proxies = null)
{
Clients = new ConcurrentDictionary<string, HttpClient>();
if (Locker is null)
Locker = new SemaphoreSlim(1, 1);
if (proxies != null)
initializeHandler(proxies);
}
private async Task<HttpClient> CreateClient(string Name, bool persistent, CancellationToken token)
{
if (Clients.ContainsKey(Name))
return Clients[Name];
HttpClient newClient = new HttpClient();
if (persistent)
{
while (Clients.TryAdd(Name, newClient) is false)
{
token.ThrowIfCancellationRequested();
await Task.Delay(1, token);
}
}
return newClient;
}
public async Task<T> NewRequest<T>(Func<HttpClient, T> Expression, int? MaxTimeout = 2000, string Id = null)
{
await Locker.WaitAsync(MaxTimeout ?? 2000, TokenSource.Token);
bool persistent = true;
if (Id is null)
{
persistent = false;
Id = string.Empty;
}
try
{
HttpClient client = await CreateClient(Id, persistent, TokenSource.Token);
T result = await Task.Run<T>(() => Expression(client), TokenSource.Token);
if (persistent is false)
client?.Dispose();
return result;
}
finally
{
Locker.Release();
}
}
public void AssignDefaultHeaders(HttpClient client)
{
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
//client.Timeout = TimeSpan.FromSeconds(3);
}
public async Task Cancel(string Name)
{
if (Clients.ContainsKey(Name))
{
CancellationToken token = TokenSource.Token;
HttpClient foundClient;
while (Clients.TryGetValue(Name, out foundClient) is false)
{
token.ThrowIfCancellationRequested();
await Task.Delay(1, token);
}
if (foundClient != null)
{
foundClient?.Dispose();
}
}
}
public void ForceCancelAll()
{
TokenSource?.Cancel();
TokenSource?.Dispose();
TokenSource = new CancellationTokenSource();
foreach (var item in Clients)
{
item.Value?.Dispose();
}
Clients.Clear();
}
}
}
One thing I spotted in a quick skim: Your line in main.cs::Program.MakeJOB:
using (HttpResponseMessage response = client.GetAsync("https://api.my-ip.io/ip.txt").Result)
should instead read
using (HttpResponseMessage response = await client.GetAsync("https://api.my-ip.io/ip.txt"))
This may not be your only issue, but by not awaiting the GetAsync method, you are effectively blocking on the request rather than yielding control back to the caller so that it can, for instance, context switch between tasks or queue up other tasks.
Same goes for
Console.WriteLine(content.ReadAsStringAsync().Result + " / " + Task.CurrentId);
which should be
Console.WriteLine((await content.ReadAsStringAsync()) + " / " + Task.CurrentId);
Although that one is not likely to block for a significant amount of time.

C# Unable to Get HTTP Return Codes via HttpResponseMessage

I’m getting an error in VS trying to check the Return Code of a method that builds and post data via an API.
The line that is generating the error is:
if (BuildApi(MyGlobals.data5, MyGlobals.data1, FQAN, MyGlobals.data4) == MyGlobals.ReturnCode)
The error is:
Operator '==' cannot be applied to operands of type 'Task<string'>' and 'string'
My goal is to pass those parameters (data1, data5, etc) to the BuildApi() method and then post that data via an API call.
When the data is successfully posted, I should get a Return Code of 200 or a Return Code of 400 if an error occurred (according to the API developer).
The BuildApi() method should return either a 200 or 400 back to the condition statement.
Is the BuildApi() method formatted correctly to return the Return Code and if so, what’s wrong with that “if” statement?
Thanks in advance for your help!
Full Code:
static class MyGlobals
{
public static XmlDocument XmlAccounts = new XmlDocument();
public static XmlNode XmlRoot;
public static string data1 { get; set; }
public static string data2 { get; set; }
public static string data3 { get; set; }
public static string data4 { get; set; }
public static string data5 { get; set; }
public static string ReturnCode { get; set; }
}
static HttpClient client = new HttpClient();
static void Main(string[] args)
{
SqlConnection ObjConn = new SqlConnection();
string ConnectMe = #"
Data Source =SERVER;
Database =DATABASE1;
User ID =USER;
Pwd =PASS;
Connection Timeout =700
";
// Open Connection
ObjConn = new SqlConnection(ConnectMe);
ObjConn.Open();
// Call methods based on the required tool
SR_Provisioning(ObjConn);
}
static public void SR_Provisioning(SqlConnection ObjConn)
{
Get = #"
SELECT
data1,
data2,
data3,
data4,
data5
FROM
table
";
ObjAdp = new SqlDataAdapter(Get, ObjConn);
ObjAdp.Fill(OutputTable);
foreach (DataRow OutputRow in OutputTable.Rows)
{
//Initalize FQAN
string FQAN = "";
// Convert query output to variables
MyGlobals.data1 = OutputRow[0].ToString();
MyGlobals.data2 = OutputRow[1].ToString();
MyGlobals.data3 = OutputRow[2].ToString();
MyGlobals.data4 = OutputRow[3].ToString();
MyGlobals.data5 = OutputRow[4].ToString();
// Instantiate new objects
strFunctions MyStr = new strFunctions();
wshWin32API win32api = new wshWin32API();
// Convert server to FQDN for accessibility ease
string FQDN = getFQDN(MyGlobals.data1, ObjConn);
// Perform action based on Tranaction_Type
switch (MyGlobals.data5)
{
case "Add":
if (MyGlobals.data2 == "LOCAL")
{
// Create local ID first
try
{
FQAN = MyGlobals.data1 + "\\" + MyGlobals.data3;
// Check the return code to determine how to log the results
if (BuildApi(MyGlobals.data5, MyGlobals.data1, FQAN, MyGlobals.data4) == MyGlobals.ReturnCode)
{
switch (MyGlobals.ReturnCode)
/*
Return Codes
200 (Created)
400(Expectation Failed)
*/
{
case "200":
// Do something
AllIsGood();
break;
case "400":
// Do something else
AllIsBad();
break;
}
}
}
catch (Exception err)
{
// Handle error and update transaction record
Update_Row();
}
}
}
static async Task<string> BuildApi(string data5, string data1, string FQAN, string data4)
{
try
{
UriBuilder baseUri = new UriBuilder("https://pwmfunction001.azurewebsites.net/api/VMGroupMemberModify01?code=T753ljF4jwXZXzmotCnnrBdV7Mrbqvcd3ibazRb92ZoBfJADuCpq5w==-Headers#{Metadata=true}-Body#{");
// Create the query string
string queryToAppend = "DATA5=" + data5 + ";DATA1=" + data1 + ";FQAN=" + FQAN + ";data4=" + data4 + "}";
if (baseUri.Query != null && baseUri.Query.Length > 1)
{
baseUri.Query = baseUri.Query.Substring(1) + ";" + queryToAppend;
}
else
{
// Check this
baseUri.Query = queryToAppend;
}
string httpResponseBody = "";
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var content = new StringContent(client.ToString());
HttpResponseMessage response = await client.PostAsync(baseUri.ToString(), content);
if (response.IsSuccessStatusCode)
{
httpResponseBody = "200";
return httpResponseBody;
}
else
{
httpResponseBody = "400";
return httpResponseBody;
}
}
catch(HttpRequestException err)
{
throw err;
}
}
}
}
Your BuildApi function is async so you need to await it in your code:
if (await BuildApi(MyGlobals.data5, MyGlobals.data1, FQAN, MyGlobals.data4) == MyGlobals.ReturnCode)
UPDATE:
If you can't run it async then you need the result:
if (BuildApi(MyGlobals.data5, MyGlobals.data1, FQAN, MyGlobals.data4).Result == MyGlobals.ReturnCode)
However, I would try in the first instance to make your calling method async

Async => avoiding deadlock and writing slow performing async code

I have this class with two public async methods, which also calls some private methods that are async.
Here's the class:
public class AggregatedDataService
{
public async Task<OrganizationAggregatedInfo> GetOrganizationAggregatedInfo(int organizationId)
{
var organization = await GetOrganization(organizationId);
var organizationContact = await GetOrganizationContact(organization.ID);
var associations = await GetOrganizationAssociations(organization.ID);
//await Task.WhenAll(organizationContact, associations);
return new OrganizationAggregatedInfo(organization.Name, organization.Address, organizationContact,
associations);
}
public async Task<SchoolAggregatedInfo> GetSchoolAggreagtedInfo(int schoolId)
{
if(schoolId < 1)
return null;
var school = await GetSchool(schoolId);
if (school == null)
{
return null;
}
var getSchoolAddressTask = GetSchoolAddress(schoolId);
var getMemberSchoolsTask = GetMemberSchools(schoolId);
var getSchoolCurriculumTask = GetSchoolCurriculum(schoolId);
var getSchoolFacilitiesTask = GetSchoolFacilities(schoolId);
var getSchoolAssociationsTask = GetSchoolAssociations(schoolId);
var getSchoolAcreditationsTask = GetSchoolAcreditations(schoolId);
var getGovernanceStructureTask = GetGovernanceStructure(schoolId);
await Task.WhenAll(getSchoolAddressTask, getMemberSchoolsTask, getSchoolCurriculumTask,
getSchoolFacilitiesTask, getSchoolAssociationsTask, getSchoolAcreditationsTask,
getGovernanceStructureTask);
var schoolAddress = getSchoolAddressTask.Result;
var memberSchools = getMemberSchoolsTask.Result;
var teacherInfo = await GetTeacherInformation(schoolAddress.SchoolID);
var curriculum = getSchoolCurriculumTask.Result;
var facilities = getSchoolFacilitiesTask.Result;
var associations = getSchoolAssociationsTask.Result;
var accreditations = getSchoolAcreditationsTask.Result;
var studentInfo = await GetStudentInformation(schoolAddress.SchoolID);
var governanceStructure = getGovernanceStructureTask.Result;
SchoolContactReadView contact = null;
if (schoolAddress != null)//TODO: consider using null propagation
{
if (schoolAddress.SchoolContact != null)
contact = SchoolContactReadView.ShowSchoolContactView(schoolAddress.SchoolContact);
}
var schoolAggregateInfo = new SchoolAggregatedInfo
{
Name = school.Name,
Address = school.Address,
MemberSchoolsCount = memberSchools.Count(),
GovernanceStructure = governanceStructure,
Accreditations = accreditations.ToList(),
Associations = associations.ToList(),
Contact = contact,
Curriculum = curriculum,
Facilities = facilities.ToList()
};
return schoolAggregateInfo;
}
private async Task<List<OrganizationAssociationReadView>> GetOrganizationAssociations(int organizationId)
{
HttpResponseMessage response = await client.GetAsync("api/organization/associations/" + organizationId);
if (response.StatusCode == HttpStatusCode.OK)
{
var contentResult = await response.Content.ReadAsAsync<List<OrganizationAssociation>>();
associations = contentResult.Select(OrganizationAssociationReadView.MapFrom).ToList();
}
else
{
_log.Error("API Error Reason: " + response.ReasonPhrase);
}
}
private async Task<OrganizationContactReadView> GetOrganizationContact(int organizationId)
{
HttpResponseMessage response = await client.GetAsync("api/organization/contact/" + organizationId);
if (response.StatusCode == HttpStatusCode.OK)
{
var contentResult = await response.Content.ReadAsAsync<OrganizationContact>();
var organizationContactReadView = OrganizationContactReadView.MapFrom(contentResult);
organizationContact = organizationContactReadView;
}
else
{
_log.Error("API Error Reason: " + response.ReasonPhrase);
}
}
private async Task<OrganizationReadView> GetOrganization(int organizationId)
{
same as the other other methods....
}
private async Task<GovernanceStructureReadView> GetGovernanceStructure(int schoolId)
{
....
}
private async Task<IEnumerable<AccreditationReadView>> GetSchoolAcreditations(int schoolId)
{
....
}
.... all other method ....
}
and I also consume this method e.g
var obj = await GetSchoolAggreagtedInfo(21).ConfigureAwait(false);
I feel like I have too much async methods and which I'm afraid may cause more harm than good.
Is there a better way of doing all this async stuff (maybe a pattern), or is the code OK?

WP8 ObservableCollection.CollectionChanged delegate crashing

bit of a noob when it comes to async operations but I am having some trouble with a ObservableCollection and not sure if the problem is because it is in an async method or not. When it tries to add the delegate it crashes with a System.AccessViolationException error... Here's the code:
public partial class ContactsList : PhoneApplicationPage
{
static ObservableCollection<Contact> dataSource { get; set; }
public ContactsList()
{
InitializeComponent();
}
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
dataSource.CollectionChanged += this.dataSource_CollectionChanged;
var tasks = new List<Task>();
for (int i = 1; i < 6; i++)
{
tasks.Add(GetContacts(i.ToString()));
}
await Task.WhenAll(tasks);
}
private void dataSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
List<AlphaKeyGroup<Contact>> DataSource = AlphaKeyGroup<Contact>.CreateGroups(dataSource, System.Threading.Thread.CurrentThread.CurrentUICulture, (Contact s) => { return s.Name; }, true);
ContactsLList.ItemsSource = DataSource;
}
public async Task GetContacts(string page)
{
try
{
string strCredidentials = Globals.APIKey;
string strAuthorization = Convert.ToBase64String(Encoding.UTF8.GetBytes(strCredidentials));
RestClient client = new RestClient(Globals.myURL);
RestRequest request = new RestRequest("/contacts.json?state=all&page=" + page);
request.RequestFormat = DataFormat.Json;
request.AddHeader("Authorization", "Basic " + strAuthorization);
request.Method = Method.GET;
var rslt = client.ExecuteAsync(request, (r) =>
{
if (r.ResponseStatus == ResponseStatus.Completed)
{
if (r.Content == "" || r.Content == " ")
{
MessageBox.Show("No Contacts Found");
}
else
{
dataSource = new ObservableCollection<Contact>();
var conts = JsonConvert.DeserializeObject<List<ContactWrapper>>(r.Content);
foreach (ContactWrapper cont in conts)
{
try
{
string name = cont.User.Name;
string email = cont.User.Email;
string mobile = cont.User.Mobile;
string phone = cont.User.Phone;
string jobtitle = cont.User.JobTitle;
dataSource.Add(new Contact("", "", "", "", "", email, "", jobtitle, mobile, name, phone, ""));
}
catch { }
}
}
}
});
} catch {}
}
}
}
In the GetContacts method the dataSource collection gets added to, so the idea is that GetContacts is called 6 times and each time the return data is added to the dataSource.
When that happens I want to call dataSource_CollectionChanged to update the bound longlistselector on the XAMl page.
Can someone tell me where I am going wrong?
Thanks
I've build a simple example basing on your code and it seems that your code is all right.
I think the problem may be in your Task GetContacts(string page) in // Do stuff here - check if you are not trying to change something within UI if so, do it by using Dispatcher:
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
//do UI stuff here;
});
EDIT - it turned out in the discussion that the Collection hadn't beed initialized on start.

Categories

Resources