Friday, 18 December 2015

Pineng PN-920 20000mAh Review

Couple of weeks back I have bought the Pineng PN-920 from 11Street for a discounted price, but too bad it doesn't comes with a free pouch. It is original and can be verified at pineng official website http://pineng.com.my/Product-Verification. It is huge, bigger and heavier than most of the phones.

pineng 920 input output light pineng 920 Description pineng 920 output led light pineng 920 input led light pineng 920 torch light pineng 920 brand pineng 920 button
PN-920

The light is blue for the LCD screen on the PN-920 and it can be quite irritating and uncomfortable to the eyes due to its brightness. The LCD screen will display some information whenever you perform the following:
  • Hold the button for a couple of seconds (Display the remaining percentage with the word "OUT")
  • Double click the button (Display the remaining percentage with the word "OUT" and turn on the flashlight)
  • Charging the PN-920 (Display the remaining percentage with the word "IN")
  • Charging the phone with 5V 1A port and/or 5V 2.1A port (Display the remaining percentage with the word "OUT" and shows 5V 1A and/or 5V 2.1A)
The LCD display can be turned off by holding the button for a couple of seconds and turning off the flashlight can be done by clicking the button once. For more details about PN-920, you can check it out at http://pineng.com.my/PINENG-PN-920-20000mAh-Power-Bank-Black.

** NOTE: The following result may not be accurate due to the measurement are done through software and not dedicated hardware tools. The result is just to give you a general idea on how well it performs.

** NOTE: Using different micro usb cable may affect the charging speed. Other than that, the phone charging speed is slower when charging above 80%.

Measuring on how many times it can fully charged a phone may not be that accurate, this is due to different phones have different battery capacity and some phones consume more battery in the background. So I hope that the following test will give you a general idea on how many times it can fully charge a phone and how fast it can charge a phone with 5V 1A. To test how well PN-920 perform, the following are used:
  • Samsung Galaxy Note 5
  • Pineng PN-920
  • Micro usb cable (Pineng)
  • Battery Monitor Widget
  • Stopwatch
This test will be done in every 1 hour time frame to charge the Samsung Galaxy Note 5 while plugged on 5V 1A port on the PN-920. The phone screen, wifi, data is turned off and only the default applications and Battery Monitor Widget is running. Same micro usb cable is used throughout the test.

No. Phone Battery (%) Phone Battery (°C) PN-920 Battery (%) State
1 21 30.3 90 Charging
66 32.8 83 Unplugged
2 20 31.6 83 Charging
66 33.0 76 Unplugged
3 21 32.4 76 Charging
67 35 70 Unplugged
4 20 29.8 70 Charging
66 32.3 60 Unplugged
5 21 30.4 60 Charging
65 33 50 Unplugged
6 21 30.8 50 Charging
67 33.3 36 Unplugged
7 22 31 36 Charging
67 34.5 26 Unplugged
8 22 31.5 26 Charging
67 34.5 19 Unplugged
9 20 30.4 19 Charging
66 33.8 11 Unplugged

Based on the table above, it seems that the battery percentage on the PN-920 drop a lot when it is below 70%. In 1 hour time frame, the battery percentage increased between 44-46% for the Samsung Galaxy Note 5 with 5V 1A port. It will need more than 2 hours to fully charge the phone from 0% to 100%, which is very slow. So it is advisable to plugged to the 5V 2.1A port on the PN-920 to charge faster. But do take note that charging faster would lead to your PN-920 battery deplete faster and some of the very old phones can only accept up to 5V 1A.




Friday, 20 November 2015

Creating Windows Task Scheduler with Task Scheduler Managed Wrapper in C# & VB

While discussion with my mentor Serena Yeoh about creating Task Scheduler in code and we stumble upon Task Scheduler Managed Wrapper. This is a wrapper to the Windows Task Scheduler. For more detail, you can check it out from the following link. https://taskscheduler.codeplex.com/. There are other ways to create and manage the task scheduler, including PowerShell. But in this post, I'm going to just focus on Task Scheduler Managed Wrapper.

Creating task scheduler in code is pretty much straight forward. There are a few mandatory information you need to provide when creating a task scheduler, which is the name of the task scheduler, trigger (one time, daily, weekly, monthly) and the action (EG: execute an application). The rest of it are optional.

To use the Task Scheduler Managed Wrapper, you need to get the package from NuGet called "Task Scheduler Managed Wrapper". Once done, instantiate a new instance of TaskService.

[C#]
TaskService taskService = new TaskService();

[VB]
Dim taskService As TaskService = New TaskService()


TaskDefinition is required to be used for defining your actions and triggers, which is the mandatory information for creating task scheduler. TaskDefinition can also be used to define other optional information, such as task scheduler's description, when the task scheduler being created, who created the task scheduler and many more.

[C#]
TaskDefinition taskDefinition = taskService.NewTask();

[VB]
Dim taskDefinition As TaskDefinition = taskService.NewTask()


Once the TaskDefinition has been instantiated, we can start defining the trigger. In task scheduler, there are 4 different types of trigger (One Time, Daily, Weekly, Monthly). Each of them is using different trigger class (instantiate TimeTrigger() for [One Time], instantiate DailyTrigger() for [Daily], instantiate WeeklyTrigger() for [Weekly], instantiate MonthlyTrigger() for [Monthly]).

While all the triggers needs the start time by supplying the value to StartBoundary property, some of the trigger requires additional information. Such as the recurring days for daily schedule, recurring weeks for weekly schedule, the scheduled days of the week, and so on.

The following are the trigger example for daily. The task scheduler will be triggered every 2 days and the task scheduler will be triggered after 3 minutes from the current date time.

[C#]
DailyTrigger dailyTrigger = new DailyTrigger();
dailyTrigger.DaysInterval = 2;
dailyTrigger.StartBoundary = DateTime.Now.AddMinutes(3);

[VB]
Dim dailyTrigger As DailyTrigger = New DailyTrigger()
dailyTrigger.DaysInterval = 2
dailyTrigger.StartBoundary = DateTime.Now.AddMinutes(3)


Once you have define your trigger, add them to your TaskDefinition.

[C#]
taskDefinition.Triggers.Add(trigger);

[VB]
taskDefinition.Triggers.Add(trigger)


Next we will define the action for the task scheduler. ExecAction class will be used to define the application's executable path and its argument if any. The following code define the action to launch command prompt.

[C#]
ExecAction execAction = new ExecAction(@"C:\Windows\System32\cmd.exe");

[VB]
Dim execAction As ExecAction = New ExecAction("C:\Windows\System32\cmd.exe")


After define your action, add them to your TaskDefinition.

[C#]
taskDefinition.Actions.Add(execAction);

[VB]
taskDefinition.Actions.Add(execAction)


With both trigger and action defined, you can now create the task scheduler. The following code will create a task scheduler with a name "SampleTask" and it is located in the root path of your Task Scheduler Window.

[C#]
taskService.RootFolder.RegisterTaskDefinition("SampleTask", taskDefinition);

[VB]
taskService.RootFolder.RegisterTaskDefinition("SampleTask", taskDefinition)


You can check out the sample code here https://onedrive.live.com/redir?resid=E6612168B803803D!348&authkey=!AG20kICe6IVyxiU&ithint=file%2czip . For more examples, you can check it out from the github https://github.com/dahall/taskscheduler.

Friday, 13 November 2015

Thread Synchronization with Semaphore and how to Implement it in C# & VB

In the previous couple of posts, I have posted about aquiring exclusive lock with SpinLock http://jaryl-lan.blogspot.com/2015/08/exclusive-lock-with-spinlock-c-vb.html or with Mutex http://jaryl-lan.blogspot.com/2015/08/thread-synchronization-with-mutex.html. So for today, I'm going to post about something different yet it can still provide exclusive right to a thread in a multi-threaded environment, which is Semaphore.

Semaphore is used to control the number of concurrent threads to access a particular section of the code / resources. With the ability to control the number of concurrent threads, It can do more than just acquiring exclusive lock by limiting to 1 thread. I will demonstrate on how to use Semaphore to control the concurrent threads with an example. The example below is by no means the best practice, it is just to show how Semaphore works.

Imagine that you need to process more than 1 file at the same time with FileSystemWatcher. By default, the event only process 1 file at a time. So to process multiple file at the same time and control the threads, we will use Task.Run to simulate fire-and-forget and Semaphore to control the threads.

As usual, you need an instance of Semaphore. The following constructor accept 2 parameters. The first parameter define the number of free / available requests can be accepted. The second parameter define the maximum number of concurrent threads. In this case, we have 3 available requests and can accept only up to 3 concurrent threads.

[C#]
Semaphore semaphore = new Semaphore(3, 3);

[VB]
Dim semaphore As Semaphore = New Semaphore(3, 3)


In your FileSystemWatcher's created event, call WaitOne() to occupy 1 available request. If the request is not available, it will wait until a new request is available.

[C#]
semaphore.WaitOne();

[VB]
semaphore.WaitOne()


We will use Task.Run to execute the method in another thread. In that method, call Release() to release the request. Allowing another thread to acquire the available request.

[C#]
semaphore.Release();

[VB]
semaphore.Release()



If you play around with the sample code, you will notice that the thread id is different from when it call WaitOne() and call Release(). This is due to the thread that execute Task.Run spawn a new thread and since it did not call await or .Wait() for the Task.Run, it end up leaving the Task.Run without waiting for its completion and exit the method. To re-explain this in steps, 
  1. Thread A call WaitOne() and execute Task.Run.
  2. Task.Run spawn a new thread B to further execute the method specified in Task.Run. 
  3. Thread A leaves without waiting thread B completes its execution and exit the method. (fire-and-forget)
  4. Thread B executing the method and call Release().

List of Additional Funtionality to Include During Self Install Windows Service in C# & VB

To continue on the previous post about self install a windows service. http://jaryl-lan.blogspot.com/2014/09/create-simple-windows-service-and-self.html. Have you ever been wondering what else can you do during installation of the windows service? Well, you can include methods or functionalities that only requires to execute once that are not suitable to be called during the windows service is running. So, let's look into the list of methods or functionalities to be included.


Create Event Log & Source

If you need to write logs into the event log with custom log and source, it is advisable to create them during windows service installation. This is because writing log into event log requires the log and source to be there. The good thing is that if it does not exist, the code will attempt to create the log and source. But if the Windows Service is launched with a user with low-privilege, then the service will throw an exception due to the user did not have the permission to creation of log and source. For more detail on dealing with Event Log, you can refer to the following link. http://jaryl-lan.blogspot.com/2015/09/create-configure-and-write-to-event-log.html


Automatically Start / Stop the Windows Service During Installation & Uninstall

To simplify the installation, you may want to consider starting the service for the user after installation. Otherwise the user need to manually start the windows service by launching the Services window to find the installed windows service. Also, there's a high chance that the user is not aware of the windows service name.

[C#]
using (var serviceController = new ServiceController(_serviceName))
{
    if (serviceController.Status != ServiceControllerStatus.Stopped) return;

    serviceController.Start();
    serviceController.WaitForStatus(ServiceControllerStatus.Running);
}

[VB]
Using ServiceController As ServiceController = New ServiceController(_serviceName)
    If Not ServiceController.Status = ServiceControllerStatus.Stopped Then
        Exit Sub
    End If

    ServiceController.Start()
    ServiceController.WaitForStatus(ServiceControllerStatus.Running)
End Using

Other than that, you may want to stop the service before uninstall the windows service. When the windows service is stopping, the OnStop method will be executed, so you can write the necessary code to do some cleanup in the method.

[C#]
using (var serviceController = new ServiceController(_serviceName))
{
    if (serviceController.Status != ServiceControllerStatus.Running) return;

    serviceController.Stop();
    serviceController.WaitForStatus(ServiceControllerStatus.Stopped);
}

[VB]
Using ServiceController As ServiceController = New ServiceController(_serviceName)
    If Not ServiceController.Status = ServiceControllerStatus.Running Then
        Exit Sub
    End If

    ServiceController.Stop()
    ServiceController.WaitForStatus(ServiceControllerStatus.Stopped)
End Using


Windows Service Recovery

For those who are unaware about Windows Service Recovery, you can actually configure them in the Recovery Tab by navigating to the Windows Service's properties and look for the Recovery Tab. But it can be tedious to configure each and every Windows Service that you have installed. To simplify this, recovery settings should be set during Windows Service installation. There are couple of ways to do it, but I will only demonstrate how to set it using command line with Process class.

The arguments specified in the code below will do the following:
  • The error count will be reset after 3600 seconds.
  • The Windows Service will restart itself when it gets terminated unexpectedly for the first and second failure (After 5 minutes).
  • The subsequent failure will attempt to execute the windows service with the argument "-email" (After 30 seconds).

[C#]
using (var process = new Process())
{
    var startInfo = process.StartInfo;
    startInfo.FileName = SC_COMMAND;
    startInfo.Arguments = string.Format("failure \"{0}\" reset= 3600 actions= restart/300000/restart/300000/run/30000 command= \"\\\"{1}\\\" -email\"", _serviceName, executableLocation);

    process.Start();
    process.WaitForExit();
}

[VB]
Using process As Process = New Process()
    Dim startInfo = process.StartInfo
    startInfo.FileName = SC_COMMAND
    startInfo.Arguments = String.Format("failure ""{0}"" reset= 3600 actions= restart/300000/restart/300000/run/30000 command= ""\""{1}\"" -email""", _serviceName, executableLocation)

    process.Start()
    process.WaitForExit()
End Using


Display Error Message for Failed Installation / Uninstall

It is best to show an error message whenever an installation or uninstall for the windows service is being performed, otherwise it will be hard and tedious to trace the problem (Check installation log and Event Log) & the end user that does the installation or uninstall may not aware on how to trace the problem. With a proper and meaningful error message being displayed, the user will be aware of the error and might be able to perform the necessary action or amendment to fix the problem and then retry the installation or uninstall of the windows service.

The sample code can be obtained here. https://onedrive.live.com/redir?resid=E6612168B803803D!345&authkey=!AOIm5LtxJbwXoQo&ithint=file%2czip