Saturday, 25 February 2017

.NET Threading in action part-2

as we discussed threading earlier and gave a very brief introductory post regarding threading in. NET , I managed to record a video about major simple tasks which can be implemented via threading and posted on my youtube channel. anyway, i have recently created this channel and perhaps I would post more videos there from now on.
this video describes sleep(), join() and interrupt() methods as well as threadpriority.can check it out on youtube at the following URL, hope it will be useful enough to start and spark something in your mind if you are not still familiar with threading, particularly in .NET framework.
https://www.youtube.com/watch?v=aNWyHRkhUaE

Wednesday, 22 February 2017

Perl GUI Programming

hi, today I want to explain how can you write a GUI app in Perl language. maybe after some months no writing, come back with some nice notes about perl.i’m sure that you somewhat tired of boring console apps.first of all you have to know that if ya wanna write something in Perl that is requires some extra modules, you have two choices to find & install them. one of them is PPM ( Perl package manager) that let you install packages in an easy way. another way is Cpan.it also exists in windows & Unix . with this command you can install packages: install ( package name ) .we use TK module for creating GUI forms .so lets go to the heart of matter.the main thing that you got to pay attention to it is that, its totally look like other modules in languages such as ( PHP – python ) that contain some steps like creating a new main window then place some widgets & starting off the event loop
use TK;
$mh = mainWindow->new();
$mh->title(“salam”);
$mh->label(-text=>”just for testing”)->pack; mainloop;

let's run this program & see your first GUI application in Perl.Yes! that was so much easy to accomplish the first mission.one note is there.pack at the end of the label code is for giving a default location on the main form to it.for placing some buttons on the form we use the following example:

$mh->button(-text=>”test”)->pack;

but you might think why it doesn’t return anything to me ? yes you didn’t define any event for it.
$mh->button(-text=>”test”)->pack;
mainloop; sub m2h {
}
print “button clicked”;

full source code to help you out and ease the process of learning

#!/usr/bin/perl
use Tk;
my $window = MainWindow->new;
$window->title("Welcome to mH-diary.ir");
$window->Label(-text => "welcome to my website")->pack;
$window->Label(-text => "lets see how can we write something like that")->pack;
#$window->Button(-text => "Print a value", -command => \&but )->pack;
#$window->Button(-text => "Double the value", -command => sub {$val*=2} )->pack;
#$window->Button(-text => "This is a quit button ", -command => \&finito )->pack; $window->Label(-text => "")->pack;
$window->Button(-text => "::Exit::",-command => \&m2h )->pack;
$window->Button(-text => "About me ",-command => \&m2h )->pack; $window->Label(-text => "")->pack; MainLoop; ############################################### sub but {
print "value is $val\n"; } sub m2h { print "test button clicked\n"; } sub finito{ print "going away ...\n"; print "final value is $val\n"; exit; } sub BEGIN { $val = 2;
}


there is a screenshot which i found it on my previous website ( in 2008 ).


Monday, 20 February 2017

.NET Threading in action part-1

I want to use C# as my default language for some consecutive posts but later on, I will try to post different languages threading tips and tricks. C# has an extensive Task Parallel Library which has been improving and enhancing through the time. I want to explain how to spawn a new worker thread and also some of its issues in console application programming which differs a bit from GUI based application.
Firstly, let's focus on Thread Class which represents the main Class of our post. This class lets us do everything that we have discussed earlier like suspending, aborting, starting, prioritising and so on. I will demonstrate the rest of material in a video clip. some complementary information about the lifecycle of Threads in C#:

  1. Aborted - The thread is in the stopped state
  2. AbortRequested - The Abort() method has been called but the thread has not yet received the System.Threading.ThreadAbortexception that will try to terminate it - the thread is not stopped but soon will be.
  3. Background - The thread is being executed in the background
  4. Running - The thread has started and is not blocked
  5. Stopped - The thread has completed all its instructions and stopped
  6. Suspended - The thread has been suspended
  7. WaitSleepJoin - The thread has been blocked by a call to Wait(), Sleep(), or Join() 
  8. Unstarted - The Start() method has not yet been called on the thread
for the first try, it is better to start off with a very simple code to see how two threads can work simultaneously. in this code sample, we have a Class called "work" that contains two methods. 
the first thread works on the first method and the second one goes for the second method.
the difference between these two methods is, one meant for being used as a static method and another one is meant for using by an object of the class. after running both threads, their state becomes running till they finish. so the following code is all about the main function of our console application.

     static void Main()

        {

          

           ThreadStart x = new ThreadStart(Work.DoWork);
            Thread newThread = new Thread(x);
            Work w = new Work();
            w.Data = 42;
            ThreadStart x2 = new ThreadStart(w.DoMoreWork);
            Thread newThread2 = new Thread(x2);
            newThread.Start();
            newThread2.Start();
            Console.ReadLine();

        }
now it is the time for implementing our desired class ( word ).

 class Work 
    {
        public static void DoWork() 
        {
            Console.WriteLine("Static thread procedure has run"); 
        }
        public int Data;
        public void DoMoreWork() 
        {
            Console.WriteLine("Instance thread procedure. data=> {0}", Data); 
        }
    }

if you run this program for several times, you may get several outputs as threads are not executing in the same order. hope this short tutorial would be beneficial to you guys.

references: C# threading handbook 

Sunday, 19 February 2017

Threading Basics

generally speaking , A thread is a special object that is part of the general multitasking abilities of an OS and allows a part of the application to run independently from the execution of other objects. this commonly happens in every millisecond of our daily life. at first, we can to  take a quick glance at what threading is , some comparisons related to threading models, how they are managed and controlled and so on but not maybe all of the topics will be included in this post lol. 
as you can guess , multitasking refers to an ability of an OS which can run more than one application at a time. for instance , you can surf my website while listening to music and also running photoshop behind the browser or split your monitor and work simultaneously. if we want to talk about processes also , process means that when an application  is launched, memory and any other resource for that application are allocated. The physical separation of this memory and resources is called a process. one important note is , application and process are not the same. as you can see in the windows task manager , your system processes go under processes tab . 
Threads
You will also notice that the Task Manager has summary information about process CPU utilisation. This is because the process also has an execution sequence that is used by the computer's processor. This execution sequence is known as a thread. This thread is defined by the registers in use on the CPU, the stack used by the thread, and a container that keeps track of the thread's current state. The container mentioned in the last sentence is known as Thread Local Storage. The concepts of registers and stacks should be familiar to any of you used to dealing with low-level issues like memory allocation.  each process has at least one thread within itself. the main thread is responsible at the run time of the process . for example in most programming languages, the primary thread is started in the Static main () method. threads can access the isolated data within a process and run on the processor as required.
Time Slices
the operating system grants each application a period to execute before interrupting that application and allowing another one to execute. This is not entirely accurate. The processor actually grants time to the process. The period that the process can execute is known as a time slice or a quantum. The period of this time slice is unknown to the programmer and unpredictable to anything besides the operating system. Each operating system and each processor may have a different time allocated. You may spawn some threads in order to do some background work, such as accessing a network or querying a database or something which if you don't, your application ( main thread ) will freeze . for this sake, they are commonly known as worker threads. These threads share the process's memory space that is isolated from all the other processes on the system. The concept of spawning new threads within the same process is known as free threading.  there is something which is called apartment threading model. this model used in VB 6.0 . with this model each process was granted its own copy of the global data needed to execute. Each thread spawned was spawned within its own process, so that threads could not share data in the process's memory.this model is vastly and totally different from free-threading. each thread is taking its turn to execute, we might be reminded of that frustrating wait in line at the bank teller. However, remember that these threads are interrupted after a brief period. At that point, another thread, perhaps one in the same process, or perhaps a thread in another process, is granted execution.  you can find some columns related to threads in task manager shown in the below figure.

Interrupts and Thread Local Storage 
When one thread runs out of time in its allocated time slice, it doesn't just stop and wait its turn again. Each processor can only handle one task at a time, so the current thread has to get out of the way. However, before it jumps out of line again, it has to store the state information that will allow its execution to start again from the point it left earlier. this is a function of Thread Local Storage (TLS). The TLS for this thread,  contains the registers, stack pointers, scheduling information, address spaces in memory, and information about other resources in use. One of the registers stored in the TLS is a program counter that tells the thread which instruction to execute next. Windows knows when it needs to make a decision about thread scheduling by using interrupts. actually, an interrupt is a mechanism that causes the normally sequential execution of CPU instructions to branch elsewhere in the computer memory without the knowledge of the execution program. Windows determines how long a thread has to execute and places an instruction in the current thread's execution sequence. This period can differ from system to system and even from thread to thread on the same system. Since this interrupt is obviously placed in the instruction set, it is known as a software interrupt. This should not be confused with hardware interrupts, which occur outside the specific instructions being executed. Once the interrupt is placed, Windows then allows the thread to execute. When the thread comes to the interrupt, Windows uses a special function known as an interrupt handler to store the thread's state in the TLS. The current program counter for that thread, which was stored before the interrupt was received, is then stored in that TLS. As you may remember, this program counter is simply the address of the currently executing instruction.
Thread Sleep and Interrupt
a program may have yielded execution to another thread so it can wait on some outside resource. However, the resources may not be available the next time the thread is brought back to execute. In fact, it may not be available the next 20 times a thread is executed. The programmer may wish to take this thread out of the execution queue for a long period so that the processor doesn't waste time switching from one thread to another just to realise it has to yield execution again. When a thread voluntarily takes itself out of the execution queue for a period, it is said to sleep. When a thread is put to sleep, it is again packed up into TLS, but this time, the TLS is not placed at the end of the running queue; it is placed on a separate sleep queue. In order for threads on a sleep queue to run again, they are marked to do so with a different kind of interrupt called a clock interrupt. When a thread is put into the sleep queue, a clock interrupt is scheduled for the time when this thread should be awakened. When a clock interrupt occurs that matches the time for a thread on the sleep queue, it is moved back to the runnable queue where it will again be scheduled for execution. we will see later on our video illustration.
Thread Abort
a thread can be interrupted, and also goes to sleep. However, like all other good things in life, threads must die. Threads can be stopped explicitly as a request during the execution of another thread. When a thread is ended in this way, it is called an abort. Threads also stop when they come to the end of their execution sequence. In any case, when a thread is ended, the TLS for that thread is de-allocated. The data in the process used by that thread does not go away, however, unless the process also ends. Threads can be suspended or resumed in some cases.
Thread Priorities
as we have seen how a thread can be interrupted, sleep, aborted, there is something left in our explanation. The last thing we need to cover for the basic concept of threading is how threads prioritise themselves. Using the analogy of our own lives, we understand that some tasks we need to do take priority over other tasks.Windows prioritises threads on a scale of 0 to 31, with larger numbers meaning higher priorities. also as a programmer, you can set the priority of threads by yourself. the process also takes priority.  in  Windows, as long as threads of a higher priority exist, threads in lower priority are not scheduled for execution. The processor will schedule all threads at the highest priority first. Each thread of that same priority level will take turns executing in a round-robin fashion. After all threads in the highest priority have completed, then the threads in the next highest level will be scheduled for execution.

 References: C# Threading Handbook




Wednesday, 8 February 2017

Establishment of my blog after 8 years

hello dear readers,
in 2003, I established my first blog on 7p.com. later on, as my second attempt in 2008, I ran my website on MH-diary.ir ( still some signs of it exist on archive.org lol ) but I had no intention of running those virtual places for a long period.
at the moment, I'm totally determined to make this blog as part of my weekly or perhaps daily life...
mH