Wednesday, December 13, 2017

Different between Multi Thread and Asynchronous

  • Synchronous: you cook the eggs, then you cook the toast.
  • Asynchronous, single threaded: you start the eggs cooking and set a timer. You start the toast cooking, and set a timer. While they are both cooking, you clean the kitchen. When the timers go off you take the eggs off the heat and the toast out of the toaster and serve them.
  • Asynchronous, multithreaded: you hire two more cooks, one to cook eggs and one to cook toast. Now you have the problem of coordinating the cooks so that they do not conflict with each other in the kitchen when sharing resources. And you have to pay them.
  • Threading is about workers; Asynchronous is about tasks
  • Tasks where some tasks depend on the results of others; as each task completes it invokes the code that schedules the next task that can run, given the results of the just-completed task. But you only need one worker to perform all the tasks, not one worker per task.

async and await

  • async to create asynchronous method.
  • await is wait until the process is complete
  • If windows form has Async, it will show "loading" label and you can move form. 
  • Without Async, the form will hang and unhang when the task is finish.
Task
public async void btnProcess_click()
{
    Task<int> task = new Task<int>(CountChacaracter); //Function
    task.Start();

    lblCount.text = "Loading. Please wait";
    int count = await task; //wait for result
    lblCount.text = count.ToString();
}

Thread
UI thread (main thread) is invoke a new thread, you need to thread join to join back the thread. But It will Block the UI cannot move to wait the result.

public async void btnProcess_click()
{
    int count = 0;
    Thread thread = new Thread(() => { count = CountChacaracter(); });
    thread.Start();

    lblCount.text = "Loading. Please wait";
    thread.Join();
    lblCount.text = count.ToString();

}

With this, you can move the UI, but is not correct way, because only UI thread can modify the control, (it may or may not work, depend on luck)

Thread thread = new Thread(() => 
    count = CountChacaracter(); 
    lblcountText = count.tostring(); 
});

This is correct way, and same result as Task. But code is complicated, so use TASK!!

public async void btnProcess_click()
{
    int count = 0;
    Thread thread = new Thread(() =>
    {
        count = CountChacaracter();
        Action action = new Action(SetLabel(count));
        this.BeginInvoke(action);
    });

    thread.Start(); 
    lblCount.text = "Loading. Please wait";
}

private void SetLabel(int count)
{
    lblCount.text = count.ToString();
}

Tuesday, November 28, 2017

AWS Alexa

Intent - Goal want to achieve (BookHotel), map in Skill Service (Lambda)
Invocation Name - Intent people Said
Sample utterance -  Invoke Intent (book)
Slot - parameter fulfill the intent {City} (New York)

From, Ask, About, Using > Keyword to trigger intent
Open, Talk, Play, Start > Session working with intent (exit, close to stop the session)

Skill Service - Utterances, Slot Types, Intent Schema (Amazon Developer) [Configuration]
Skill Interface - Lambda, HTTP Service

Sunday, November 19, 2017

Web.Config Transform Debug & Release

1) Tool and Extension , Download "Configuration Transform".
2) Your Web or App.config, right click >  "Add Config Transform"
3) Add your changes

<appSettings>

    <add key="WaitSec" value="10" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>

Thursday, October 12, 2017

Cron Job

  1. Create a cron.yaml
  2. Write Code
cron:
- description: Update Today Flight job
  url: /api/Flight/UpdateTodayFlights/KUL

  schedule: every day 02:00
  1. cd yamlfilepath
  2. gcloud app deploy cron.yaml

Firebase Realtime DB vs Firestore


  • Firestore has better querying and more structured data
    • document-model database 
      • store data in Documents, organized into Collection
      • Each document contain a set of key-value pairs.
      • Documents can contain sub collections and nested objects.
    • Search multiple field.
  • Realtime DB better latency
Pricing Model
  • Realtime DB = amount of downloaded and store on DB.
  • Firestore DB = number of reads or write.
  • Traditional App, Request large of data > Firestore
  • Many updates in second > Realtime

Firestore Data Model

  • Similar to JSON
  • Document is record, map value with name
  • name of document is unique (add() will automatically)

Monday, October 9, 2017

Firebase C#

private FirebaseClient Auth()
{
    var firebaseClient = new FirebaseClient(_URL,
        new FirebaseOptions
        {
            AuthTokenAsyncFactory = () => Task.FromResult(_FireBaseApiKey)
        });

    return firebaseClient;
}

public async Task<Dictionary<string, T>> ListLimitLast<T>(string table, string filterKey, string filterValue, int limitLast)
 {
     var firebase = Auth();

     var result = await firebase
         .Child(_Database)
         .Child(table)
         .OrderBy("DriverStaff/UserName")
         .EqualTo(filterValue)
         .LimitToLast(limitLast)
         .OnceSingleAsync<Dictionary<string, T>>();

     return result;

 }

use dictionary because ID is parent, object is child.