How to stop and re-excute methods in C#/XamarinForms? - c#

I have a Display alert that ask if the user wants to retry syncing the data. My problem is when the user chose "Yes" my method overlaps it causes my application to crash. I there a way to for example when the user chooses yes the method execution stops and re-execute the method?
Here is my full code:
public async void FirstTimeSyncTown(string host, string database, string contact, string ipaddress)
{
try
{
syncStatus.Text = "Checking internet connection";
string apifile = "first-time-sync-town-api.php";
if (CrossConnectivity.Current.IsConnected)
{
syncStatus.Text = "Initializing first-time town sync";
var db = DependencyService.Get<ISQLiteDB>();
var conn = db.GetConnection();
var getData = conn.QueryAsync<TownTable>("SELECT * FROM tblTown WHERE Deleted != '1'");
var resultCount = getData.Result.Count;
var current_datetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
int count = 1;
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
if (resultCount == 0)
{
syncStatus.Text = "Getting data from the server";
var link = "http://" + ipaddress + ":" + Constants.port + "/" + Constants.apifolder + "/api/" + apifile;
string contentType = "application/json";
JObject json = new JObject
{
{ "Host", host },
{ "Database", database }
};
HttpClient client = new HttpClient();
var response = await client.PostAsync(link, new StringContent(json.ToString(), Encoding.UTF8, contentType));
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
if (!string.IsNullOrEmpty(content))
{
try
{
var dataresult = JsonConvert.DeserializeObject<List<TownData>>(content, settings);
var datacount = dataresult.Count;
for (int i = 0; i < datacount; i++)
{
syncStatus.Text = "Syncing town " + count + " out of " + datacount;
var item = dataresult[i];
var townID = item.TownID;
var provinceID = item.ProvinceID;
var town = item.Town;
var lastsync = DateTime.Parse(current_datetime);
var lastupdated = item.LastUpdated;
var deleted = item.Deleted;
var insertdata = new TownTable
{
TownID = townID,
ProvinceID = provinceID,
Town = town,
LastSync = lastsync,
LastUpdated = lastupdated,
Deleted = deleted
};
await conn.InsertOrReplaceAsync(insertdata);
count++;
}
synccount += "Total synced town: " + count + "\n";
var logType = "App Log";
var log = "Initialized first-time sync (<b>Town</b>) <br/>" + "App Version: <b>" + Constants.appversion + "</b><br/> Device ID: <b>" + Constants.deviceID + "</b>";
int logdeleted = 0;
Save_Logs(contact, logType, log, database, logdeleted);
}
catch
{
var retry = await DisplayAlert("Application Error", "Syncing failed. Failed to send the data.\n\n Error:\n\n" + content + "\n\n Do you want to retry?", "Yes", "No");
if (retry.Equals(true))
{
FirstTimeSyncTown(host, database, contact, ipaddress);
}
else
{
First_Time_Sync_Failed();
}
}
}
Preferences.Set("townchangelastcheck", current_datetime, "private_prefs");
SyncUserLogsClientUpdate(host, database, contact, ipaddress);
}
else
{
var retry = await DisplayAlert("Application Error", "Syncing failed. Please connect to the internet to sync your data. Do you want to retry?", "Yes", "No");
if (retry.Equals(true))
{
FirstTimeSyncTown(host, database, contact, ipaddress);
}
else
{
First_Time_Sync_Failed();
}
}
}
else
{
SyncTownServerUpdate(host, database, contact, ipaddress);
}
}
else
{
var retry = await DisplayAlert("Application Error", "Syncing failed. Please connect to the internet to sync your data. Do you want to retry?", "Yes", "No");
if (retry.Equals(true))
{
FirstTimeSyncTown(host, database, contact, ipaddress);
}
else
{
First_Time_Sync_Failed();
}
}
}
catch (Exception ex)
{
Crashes.TrackError(ex);
var retry = await DisplayAlert("Application Error", "Syncing failed. Failed to send the data.\n\n Error:\n\n" + ex.Message.ToString() + "\n\n Do you want to retry?", "Yes", "No");
if (retry.Equals(true))
{
FirstTimeSyncTown(host, database, contact, ipaddress);
}
else
{
First_Time_Sync_Failed();
};
}
}

In my case, I've used Task.Factory for executing methods in background, and also CancellationToken for cancelling executing.
Firstly, you need to create a Token parameter like so:
public CancellationTokenSource Ts { get; set; } = new CancellationTokenSource();
(I did it as public global param of the class for accessing from outside).
And when I execute the background methods, I use this lines of code:
// Get Token for Task.Factory
var ct = Ts.Token;
try
{
Task.Factory.StartNew(() =>
{
// your code for background task
...
// This is for defining whether user cancelled
// and in that place your code stops
if (ct.IsCancellationRequested)
{
// do the staff and return
return;
}
}, ct);
}
catch (AggregateException ex)
{
Console.WriteLine(ex.Message);
}
When user click on "Cancel", I handle this event and call this:
// Call it to stop thread
yourCustomClass.Ts.Cancel();
After that you can re-execute your method. Hope it helps!

Related

how to get information from user on alexa skill launch

I've got an Alexa app that on first launch looks for the user's id in a dynamoDB. If it isn't there I'd like it to ask the user for their ip address.
I have an intent that can collect the IP but I was wondering if I could trigger the intent from the launch request?
private SkillResponse LaunchRequestHandler(SkillRequest input, ILambdaContext context)
{
// Initialise response
var skillResponse = new SkillResponse
{
Version = "1.0",
Response = new ResponseBody()
};
// Output speech
SsmlOutputSpeech ssmlResponse = new SsmlOutputSpeech();
try
{
try
{
var strUserId = input.Session.User.UserId;
var request = new GetItemRequest
{
TableName = tableName,
Key = new Dictionary<string, AttributeValue>() { { "strUserId", new AttributeValue { S = strUserId } } },
};
var response = client.GetItemAsync(request);
// Check the response.
var result = response.Result;
var attributeMap = result.Item;
if (result.Item.Count() < 1)
{
ssmlResponse.Ssml = "<speak></speak>";
// Trigger intent to get IP address and port number.
}
else
{
ssmlResponse.Ssml = "<speak>Hi there. I'm not Cortana.</speak>";
// Give command guidance prompt.
}
}
catch (AmazonDynamoDBException e) { ssmlResponse.Ssml = "<speak>" + e.InnerException.Message + "</speak>"; }
catch (AmazonServiceException e) { ssmlResponse.Ssml = "<speak>" + e.Message + "</speak>"; }
catch (Exception e) { ssmlResponse.Ssml = "<speak>" + e.Message + "</speak>"; }
skillResponse.Response.OutputSpeech = ssmlResponse;
skillResponse.Response.ShouldEndSession = true;
}
catch
{
//ssmlResponse.Ssml = "<speak><audio src='/samples/ImSorryDave'/></speak>";
ssmlResponse.Ssml = "<speak>I'm sorry Dave. I'm afraid I can't do that.</speak>";
skillResponse.Response.OutputSpeech = ssmlResponse;
}
skillResponse.Response.ShouldEndSession = true;
return skillResponse;
}
Two options I can think of:
Have you tried just asking for the IP address? <speak> Hi there. What is your IP address?</speak> If you make sure your IP address intent has examples of how you might expect a user to respond to that question, that intent should be the triggered and sent to your skill to process.
Also look into how Alexa can handle some of the dialog management for you with intent chaining. The example there sounds very similar to your use case.

Telegram bot in asp.net core 3.1 is giving no data in Update object

I am developing telegram notifications in my existing asp.net core 3.1 project. I have written below code in controller.
#region Telegram
TelegramBotClient _botService;
private const string token = "332435:45345345345dflskdfjksdjskdjflkdd";
[HttpPost("Update")]
[AllowAnonymous]
public async Task<IActionResult> Update([FromBody] Update update)
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
if (_botService == null)
_botService = new TelegramBotClient(token);
if (update.Type != UpdateType.Message)
return Ok(new Response
{
code = (int)HttpStatusCode.OK,
status = "Ok",
message = "Success"
});
var message = update.Message;
try
{
_logger.LogInformation("Received Message from {0}", message.Chat.Id);
switch (message.Type)
{
case MessageType.Text:
if (message.Text.Contains("/Reset"))
{
//Delete(string chatid)
var response = _UserRepository.DeleteTeleBotChatID(message.Chat.Id);
if (response)
await _botService.SendTextMessageAsync(message.Chat.Id, "You have successfully unsubscribed.");
else
await _botService.SendTextMessageAsync(message.Chat.Id, "You are not registered yet.");
}
else
if (message.Text.Contains("/") && !message.Text.ToLower().Contains("/start"))
{
var user = Crypto.decrypt(Encoding.UTF8.GetString(Convert.FromBase64String(message.Text.Split('/').Last())));
var response = _UserRepository.UpdateTeleBotChatIDByUser(new TeleBotModel() { ChatId = message.Chat.Id, Username = user });
if (response)
await _botService.SendTextMessageAsync(message.Chat.Id, $"You have successfully subscribe notifications for {user}.");
else
await _botService.SendTextMessageAsync(message.Chat.Id, "Username is not valid");
// var chat=modifyus(string username,chatid)
}
else
{
await _botService.SendTextMessageAsync(message.Chat.Id, "Enter your encrypted username.\n Type /Reset to unsubscribe.");
}
break;
case MessageType.Photo:
// Download Photo
var fileId = message.Photo.LastOrDefault()?.FileId;
var file = await _botService.GetFileAsync(fileId);
var filename = file.FileId + "." + file.FilePath.Split('.').Last();
using (var saveImageStream = System.IO.File.Open(filename, FileMode.Create))
{
await _botService.DownloadFileAsync(file.FilePath, saveImageStream);
}
await _botService.SendTextMessageAsync(message.Chat.Id, "Thx for the Pics");
break;
}
}
catch (Exception exp)
{
//LoggerSimple.Error(exp);
await _botService.SendTextMessageAsync(message.Chat.Id, "Wrong Bot command");
}
return Ok(new Response
{
code = (int)HttpStatusCode.OK,
status = "Ok",
message = "Success"
});
}
[HttpPost]
public async Task<IActionResult> sendTeleMsg(TelegramMessgae Data)
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
if (_botService == null)
_botService = new TelegramBotClient(token);
//check username exist
long ChatId = _UserRepository.GetChatIdByUsername(Data.Username);
if (ChatId == -1)
{
return Ok(new Response
{
error = "true",
code = HttpStatusCode.BadRequest,
status = HttpStatus.OK,
message = "Not registered with telegram bot"
});
}
try
{
await _botService.SendTextMessageAsync(ChatId, string.Format("*{0}*\n{1}", parseMText(Data.Subject), parseMText(Data.Message)), ParseMode.Markdown);
return Ok(new Response
{
code = HttpStatusCode.OK,
status = HttpStatus.OK,
message = "Message Sent"
});
}
catch (Exception exp)
{
//if wrong chatid
_UserRepository.DeleteTeleBotChatID(ChatId);
return Ok(new Response
{
error = "true",
code = HttpStatusCode.BadRequest,
status = HttpStatus.OK,
message = exp.Message
});
}
}
private string parseMText(string txt)
{
var vs = new string[] { "*", "_", "`", "[", "]" };
foreach (var item in vs)
{
txt = txt.Replace(item, "\\" + item);
}
return txt;
}
#endregion
then used ngrok for tunnelling and exposed localhost so that I can connect with telegram bot. After creating and subscribing the bot, I am able to receive a breakpoint in above Update method but data was nothing. I sent messages on bot but always there is no data in update object. See below screenshot.
I am unable to figure-out the issue in the code. Can anyone pls help?
Calling AddNewtonsoftJson() function in starup.cs file fixed the issue.
services.AddControllers().AddNewtonsoftJson();

How can I increment the result keyword in this method? in selenium c#

private SendGridCompleteRegistrationEmail WaitForBlobEmail(AzureStorageBlobClient blobClient, string containerName, string fileName)
{
int i = 0;
SendGridCompleteRegistrationEmail result = null;
while ((i < 120) && (result == null))
{
System.Threading.Thread.Sleep(1000);
result = blobClient.GetJsonBlobContent<SendGridCompleteRegistrationEmail>(containerName, fileName).Result;
i++;
}
Assert.IsNotNull(result, "Failed to retrieve json message for " + fileName + " from " + containerName + " blob");
return result;
}
Everytime its used I want a new keyword result, result2 etc to be returned
[TestMethod]
[DataRow("Manager", "testadmin#markerstudy.com", new string[] { "Foody Fleet" } )]
[DataRow("Manager", "fleetmanager#visiontrack.com", new string[] { "Foody Fleet", "Fleetingly Employed" })]
[TestCategory("UserMgt")]
public void CompleteRegProcessPass_dom(string role, string emailID, string[] fleetNames)
{
//is emailID registered if not register
var blobClient = new AzureStorageBlobClient(AzureStorageBlobClient);
_regRep.btnAddUser.Click();
FilterUserTableByEmail(emailID);
if (FilterUserTableByEmail(emailID) == true)
{
//No need to register
}
else
{
//must register
objCommon.EnterText(_regRep.firstNameAdd, userName);
objCommon.EnterText(_regRep.lastNameAdd, "Smithy");
objCommon.EnterText(_regRep.userEmailAdd, emailID);
objCommon.EnterText(_regRep.userTelephoneAdd, "12345678901");
objCommon.Exists(_regRep.userRoleManager(role), 10);
objCommon.ScrollInToViewAndClick(_regRep.userRoleManager(role));
//objCommon.Exists(_regRep.chooseFleet, 5);
for (int i = 0; i < fleetNames.Count(); i++)
{
objCommon.ScrollInToViewAndClick(_regRep.chooseFleet(fleetNames[i]));
}
objCommon.Click(_regRep.btnSaveUser);
System.Threading.Thread.Sleep(1000);
FilterUserTableByEmail(emailID);
var result = WaitForBlobEmail(blobClient, "complete-registration", $"{emailID}.json");
Assert.IsNotNull(result, "Failed to retrieve json message from complete-registration blob");
objCommon.HoverAndClick(_regRep.UserIcon, _regRep.LogOutLink, driver);
System.Threading.Thread.Sleep(2000);
driver.Navigate().GoToUrl(result.TokenUrl);
System.Threading.Thread.Sleep(2000);
Actions builder1 = new Actions(driver);
builder1.MoveToElement(driver.FindElement(By.XPath("//div[#class='vt-login-page__footer']/span"))).Click().Build().Perform();
System.Threading.Thread.Sleep(2000);
driver.FindElement(By.Id("email")).SendKeys(emailID);
driver.FindElement(By.XPath("//input[#id='newPassword']")).SendKeys("cy!NbZtnzAs4T&");
driver.FindElement(By.XPath("//input[#id='confirmPassword']")).SendKeys("cy!NbZtnzAs4T&");
driver.FindElement(By.XPath("//button[text()='Set Password']")).Click();
if (driver.FindElement(By.XPath("//h1[text()='Complete Registration...']")).Displayed)
{
Assert.IsTrue(driver.FindElement(By.XPath("//h1[text()='Complete Registration...']")).Displayed == true);
}
else
{
Console.WriteLine("Registration completed successfully.");
}
}
objCommon.EnterText(_regRep.firstNameAdd, userName);
objCommon.EnterText(_regRep.lastNameAdd, "Smithy");
objCommon.EnterText(_regRep.userEmailAdd, emailID);
objCommon.EnterText(_regRep.userTelephoneAdd, "12345678901");
objCommon.Exists(_regRep.userRoleManager(role), 10);
objCommon.ScrollInToViewAndClick(_regRep.userRoleManager(role));
//objCommon.Exists(_regRep.chooseFleet, 5);
for (int i = 0; i < fleetNames.Count(); i++)
{
objCommon.ScrollInToViewAndClick(_regRep.chooseFleet(fleetNames[i]));
}
objCommon.Click(_regRep.btnSaveUser);
System.Threading.Thread.Sleep(1000);
FilterUserTableByEmail(emailID);
var result = WaitForBlobEmail(blobClient, "complete-registration", $"{emailID}.json");
Assert.IsNotNull(result, "Failed to retrieve json message from complete-registration blob");
objCommon.HoverAndClick(_regRep.UserIcon, _regRep.LogOutLink, driver);
System.Threading.Thread.Sleep(2000);
driver.Navigate().GoToUrl(result.TokenUrl);
System.Threading.Thread.Sleep(2000);
Actions builder = new Actions(driver);
builder.MoveToElement(driver.FindElement(By.XPath("//div[#class='vt-login-page__footer']/span"))).Click().Build().Perform();
System.Threading.Thread.Sleep(2000);
driver.FindElement(By.Id("email")).SendKeys(emailID);
driver.FindElement(By.XPath("//input[#id='newPassword']")).SendKeys("cy!NbZtnzAs4T&");
driver.FindElement(By.XPath("//input[#id='confirmPassword']")).SendKeys("cy!NbZtnzAs4T&");
driver.FindElement(By.XPath("//button[text()='Set Password']")).Click();
if (driver.FindElement(By.XPath("//h1[text()='Complete Registration...']")).Displayed)
{
Assert.IsTrue(driver.FindElement(By.XPath("//h1[text()='Complete Registration...']")).Displayed == true);
}
else
{
Console.WriteLine("Registration completed successfully.");
}
}
How about the other way around everytime the original method is called could we count it to make it unique in the main test method? The problem I have is I need to register the login credentials and they use the keyword "result". Please help me write the correct code?
You can create a list and add the result value returned from that function to that list. So that you will have the values returned by the function in each run.
List<SendGridCompleteRegistrationEmail> results = new List<SendGridCompleteRegistrationEmail>();
SendGridCompleteRegistrationEmail result = WaitForBlobEmail(blobClient, "complete-registration", $"{emailID}.json");
results.Add(result);

Async and await tasks are getting missed

I'm getting a "Because this call is not awaited..." on
SendPostAsync(CustomerName, email, Phone, maxImages, MainEventName, MainEventCode, CLemail, package_type, PlayerInfo, template_ID, favoritesArray);
Here's the button click:
private void btnCopyAllInvoices_Click(object sender, EventArgs e)
{
//sets up a list to store the incoming invoice numbers from the DB
List<string> InvoiceNums = new List<string>();
mySqlInterface.Connect();
InvoiceNums = mySqlInterface.GetNewInvoices();
//prep the visuals
lblStatus.Text = "";
InvoicePanel.Visible = true;
progressBarInvoice.Value = 0;
progressBarInvoice.Maximum = InvoiceNums.Count;
//for each invoice collected let's copy it
InvoiceNums.ForEach(delegate(string inv)
{
if (OrderDAL.CheckOrderExist(inv))
{
// the order already exist
Order myorder = new Order();
myorder = OrderDAL.GetOrder(inv);
CopyImages(myorder, true);
OrderDAL.UpdateFulfillment(string.Format("Images Copied"), inv);
}
});
//let the user know how we did
MessageBoxButtons buttons = MessageBoxButtons.OK;
string strError = string.Format("{0} Invoices copied.", InvoiceNums.Count);
MessageBox.Show(this, strError, "Copy New Invoices", buttons, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
InvoicePanel.Visible = false;
}
Here, CopyImages is called as part of the foreach loop above.
public void CopyImages(Order order, bool CopyAllInv)
{
string baseTarget = WorkSpace.Text;
string CLhotfolderTarget = string.Empty;
//check to see if the order has been photo released. If it has add "pr" to the end of the invoice number
string prInvoice = "";
if (order.Header.SignatureLine != "null" && order.Header.SignatureChecks != "null")
{
prInvoice = "pr";
}
string PackageName = null;
string CustomerName = null;
string Phone = null;
string email = null;
string PlayerInfo = null;
string PlayerName = null;
string PlayerNumber = null;
string MainEventName = null;
string MainEventCode = null;
string CLemail = null;
//go to the DB and get the info
mySqlInterface.Connect();
bool videoPackage = mySqlInterface.VideoInfo(order.Header.InvoiceNumber, out PackageName, out CustomerName, out Phone, out email, out PlayerName, out PlayerNumber, out MainEventName, out MainEventCode);
mySqlInterface.Close();
if (videoPackage)
{
if (PackageName.Contains("Video") || PackageName.Contains("Ultimate Ripken"))
{
CLemail = MainEventCode + "_" + email.Replace("#", "_").Replace(".", "_").Replace("+", "_");
PlayerInfo = PlayerName + " " + PlayerNumber;
int template_ID = 0;
if (txtCLtemplateID.Text != "")
{
template_ID = Convert.ToInt32(txtCLtemplateID.Text);
}
//we will always need a hotfolder. So let's set and create it now
CLhotfolderTarget = txtCLhotfolder.Text + "\\toUpload\\" + CLemail;
if (!System.IO.Directory.Exists(CLhotfolderTarget))
{
// create the directory
System.IO.Directory.CreateDirectory(CLhotfolderTarget);
}
int maxImages = 7;
int package_type = 2;
string[] favoritesArray = new string[maxImages];
//populate the array of images for the video
int count = 0;
foreach (Order.InvoiceImages image in order.ImageList)
{
favoritesArray[count] = image.ImageName;
count++;
}
//let's call the API and send info to CL
SendPostAsync(CustomerName, email, Phone, maxImages, MainEventName, MainEventCode, CLemail, package_type, PlayerInfo, template_ID, favoritesArray);
}
}
}
public async Task SendPostAsync(string name, string email, string phone, int photo_count, string event_name, string event_id, string dir_name, int package_type, string video_text, int template_id, string[] favoritesArray)
{
string postURL = null;
string token = null;
int delivery_method = 2;
//production
postURL = "https://search.apicall.com/photographer/customer";
token = "token xxxxxxxxxxxxxxxxxxxxx";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(postURL);
client.DefaultRequestHeaders.Add("Authorization", token);
string POSTcall = JsonConvert.SerializeObject(new { name, email, phone, photo_count, event_id, event_name, dir_name, package_type, video_text, delivery_method, template_id, favorites = favoritesArray });
//Send string to log file for debug
WriteLog(POSTcall);
StringContent stringContent = new StringContent(POSTcall, UnicodeEncoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(new Uri(postURL), stringContent);
string POSTresponse = await response.Content.ReadAsStringAsync();
WriteLog(POSTresponse);
//simplified output for debug
if (POSTresponse.Contains("error") && POSTresponse.Contains("false"))
{
lblStatus.Text = "Error Sending to CL";
}
else
{
lblStatus.Text = "Successfully added to CL";
}
}
I have an await on the HttpResponseMessage response = await client.PostAsync
If I run this one at a time, it works. But when I run this through a loop and there are a bunch back to back, I think the PostAsyncs are getting stepped on. I'm missing entires in the WriteLog.
It seems I need to do the async/awaits further upstream, right? This way I can run the whole method.
Referencing Async/Await - Best Practices in Asynchronous Programming, event handlers allow async void so refactor the code to be async all the way through.
refactor CopyImages to await the posting of the data
public async Task CopyImages(Order order, bool CopyAllInv) {
//...omitted for brevity
if (videoPackage) {
if (PackageName.Contains("Video") || PackageName.Contains("Ultimate Ripken")) {
//...omitted for brevity
await SendPostAsync(CustomerName, email, Phone, maxImages, MainEventName, MainEventCode, CLemail, package_type, PlayerInfo, template_ID, favoritesArray);
}
}
}
And update the event handler
private async void btnCopyAllInvoices_Click(object sender, EventArgs e) {
//sets up a list to store the incoming invoice numbers from the DB
List<string> InvoiceNums = new List<string>();
mySqlInterface.Connect();
InvoiceNums = mySqlInterface.GetNewInvoices();
//prep the visuals
lblStatus.Text = "";
InvoicePanel.Visible = true;
progressBarInvoice.Value = 0;
progressBarInvoice.Maximum = InvoiceNums.Count;
//for each invoice collected let's copy it
foreach(string inv in InvoiceNums) {
if (OrderDAL.CheckOrderExist(inv)) {
// the order already exist
Order myorder = OrderDAL.GetOrder(inv);
await CopyImages(myorder, true);
OrderDAL.UpdateFulfillment(string.Format("Images Copied"), inv);
}
}
//let the user know how we did
MessageBoxButtons buttons = MessageBoxButtons.OK;
string strError = string.Format("{0} Invoices copied.", InvoiceNums.Count);
MessageBox.Show(this, strError, "Copy New Invoices", buttons, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
InvoicePanel.Visible = false;
}
I would also advise against creating a HttpClient for each post request. Extract that out and use a single client.
static Lazy<HttpClient> httpClient = new Lazy<HttpClient>(() => {
var postURL = "https://search.apicall.com/photographer/customer";
var token = "token xxxxxxxxxxxxxxxxxxxxx";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(postURL);
client.DefaultRequestHeaders.Add("Authorization", token);
return client
});
public async Task SendPostAsync(string name, string email, string phone, int photo_count, string event_name, string event_id, string dir_name, int package_type, string video_text, int template_id, string[] favoritesArray)
{
var postURL = "https://search.apicall.com/photographer/customer";
int delivery_method = 2;
string POSTcall = JsonConvert.SerializeObject(new { name, email, phone, photo_count, event_id, event_name, dir_name, package_type, video_text, delivery_method, template_id, favorites = favoritesArray });
//Send string to log file for debug
WriteLog(POSTcall);
StringContent stringContent = new StringContent(POSTcall, UnicodeEncoding.UTF8, "application/json");
HttpResponseMessage response = await httpClient.Value.PostAsync(new Uri(postURL), stringContent);
string POSTresponse = await response.Content.ReadAsStringAsync();
WriteLog(POSTresponse);
//simplified output for debug
if (POSTresponse.Contains("error") && POSTresponse.Contains("false")) {
lblStatus.Text = "Error Sending to CL";
} else {
lblStatus.Text = "Successfully added to CL";
}
}

Access List<Object> in task async

I have this code to load and count data from API server;
class TestNetWork
{
private Task taskFillPicker;
private List<CityItemDB> itemsCity;
private CustomPicker cpCity;
public async Task FillPicker()
{
try {
JObject res = await SuperFUNC.GET_CITY_ACTIVE_SENDER();
if(res == null){
//null
}else{
string message = res["message"].ToString();
if(message.Equals("Success")){
itemsCity.Clear();
cpCity.Items.Clear();
JArray data = (JArray)res["data"];
int count = data.Count;
for (int i = 0; i < count; i++) {
CityItemDB node = new CityItemDB();
node.cityId = Int32.Parse(data[i]["cityId"].ToString());
node.cityName = data[i]["cityName"].ToString();
itemsCity.Add(node);
cpCity.Items.Add(node.ToString());
}
}else{
//null
}
}
} catch (Exception ex) {
Debug.WriteLine (TAG + " : " + ex.StackTrace);
}
}
public TestNetWork()
{
this.itemsCity = new List<CityItemDB> ();
this.cpCity = new CustomPicker {
HeightRequest = 40,
TextColor = Color.FromHex("#5a5a5a"),
Title = "City Choose",
};
taskFillPicker = FillPicker ();
Debug.WriteLine (COUNT + " : " + itemsCity.Count);
}
}
But console print me COUNT : 0, I'm sure code get and parse json from internet is correct, picker show full data but List<CityItemDB> itemsCity count 0.
Thank for read, sorry my english not good!
You need to await the task, otherwise execution might continue before FillPicker has completed:
taskFillPicker = await FillPicker ();
As this code is in a constructor where await is not possible, I suggest moving it to a separate async method:
public async Task Init()
{
taskFillPicker = await FillPicker ();
Debug.WriteLine (COUNT + " : " + itemsCity.Count);
}
You have to write a little bit more code to construct the object now:
var n = new TestNetWork();
await n.Init();

Categories

Resources