browse by category or date

GUI Programming used to be one of my nightmare in programming. I had a hard time understanding the concept and implemented it in my Windows Application. The good news is, I now know a little bit about it (^_^)/. Now by writing it into a blog post, I actually helping myself to understand the concept better, and hopefully, it might help others in one way or another. 🙂

In this project, we will create a stopwatch (might be useful to track-down how long you need to create a blog post (^_^)/ ). OK, now please open your Visual Studio, and create a Windows Application. Name the project ‘ThreadingGUI_02’. After that, design the form to look something like this:
threading_02_form.png

So the idea is to create a process that will have a counter, increase the counter value and update the GUI every 0.1 seconds. In order to keep the GUI responsive to user’s actions, the actual processing should be done in different Thread. We also need to create a delegate, this way the the worker thread will be able to access the GUI thread and updates the corresponding labels.

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ThreadingGUI_02
{
   public delegate void RefreshGUI(int i);

   public partial class Form1 : Form
   {
      Worker w;
      RefreshGUI myUpdateCounter;
      Thread t;
      public void UpdateCounter(int c)
      {
         if (this.InvokeRequired)
         {
            this.Invoke(myUpdateCounter, c);
         }
         else
         {
            int ms = c % 10;
            int second = (c / 10) % 60;
            int minute = (c / 600) % 60;
            int hour = (c / 36000) % 24;
            this.lblHour.Text = hour.ToString("00");
            this.lblMinute.Text = minute.ToString("00");
            this.lblSecond.Text = second.ToString("00");
            this.lblMiliSecond.Text = ms.ToString("0");
         }
      }
      public Form1()
      {
         InitializeComponent();
         myUpdateCounter = new RefreshGUI(UpdateCounter);
         w = new Worker(myUpdateCounter);
         t = new Thread(new ThreadStart(w.Counting));
         t.Start();
      }

      private void button1_Click(object sender, EventArgs e)
      {
         Button b = (Button)sender;
         b.Text = b.Text == "Start" ? "Stop" : "Start";
         w.isCounting = b.Text == "Stop";
         this.button2.Enabled = b.Text == "Start";
      }

      private void button2_Click(object sender, EventArgs e)
      {
         w.count = 0;
         int c = 0;
         int ms = c % 10;
         int second = (c / 10) % 60;
         int minute = (c / 600) % 60;
         int hour = (c / 36000) % 24;
         this.lblHour.Text = hour.ToString("00");
         this.lblMinute.Text = minute.ToString("00");
         this.lblSecond.Text = second.ToString("00");
         this.lblMiliSecond.Text = ms.ToString("0");
      }

      private void Form1_FormClosed(
         object sender, 
         FormClosedEventArgs e)
      {
         t.Abort();
         t = null;
      }

      private void label5_Click(
         object sender, 
         EventArgs e)
      {

      }
   }
}

Add a new class, and name it Worker.cs:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ThreadingGUI_02
{
   class Worker
   {
      RefreshGUI update;
      public bool isCounting;
      public int count = 0;
      public Worker(RefreshGUI d)
      {
         update = d;
         isCounting = false;
      }
      public void Counting()
      {
         while (true)
         {
            if (isCounting)
            {
               ++count;
               if (update != null)
                  update(count);
            }
            Thread.Sleep(100);
         }

      }
   }
}

So as you see in the codes above, the logic of program would be:

  1. the main thread (a.k.a the GUI thread) instantiates the delegate
  2. GUI thread instantiates the worker thread, and pass the delegate object to worker thread object
  3. GUI thread starts the worker thread object
  4. the worker thread updates the GUI thread object using delegate
  5. the worker thread sleeps for a 0.1 seconds (If you do not need the 0.1 seconds delay, you could comment the Thread.Sleep(100); line)

This is example is not really a good example in threading since we never protect the ‘Critical Section‘ of the program with Mutex/Semaphore. Maybe in the future we will discuss how to prevent deadlock using Mutex/Semaphore.

For those who are lazy to copy and paste (^_^) into their Visual Studio, you can download the project solution: Threading GUI – Stopwatch

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant:


I watched the movie last Friday two weeks ago (25/05/2007) together with Artha. We watched it at Golden Village, Jurong Point. The show started at 1 AM. There were around 20 people that watched the movie. I guess not many people in Jurong West area were willing to watch the movie after midnight. (T_T)

Generally, I think the movie is mediocre great. Captain Jack Sparrow is still partially drunk. Although now he starts to hallucinate. Elizabeth Swann might won Oscar for ‘Heroine with the biggest shotgun hidden in her pants‘. As for William Turner, a surprising end is waiting for him. Not going to be a spoiler post here (^_^)/

Favorite scenes:

  1. Elizabeth Swann disarmed herself. That scene is hilarious
  2. Jack Sparrow talked to Capt. Teageu ( Jack’s father, a cameo by Keith Richards).
  3. Jack Sparrow talked to Giselle and Scarlett. Jack’s words basically sum up all the lies that most men ever committed to their partner. Classic!!!
  4. The battle scene Between Black Pearl v.s. Flying Dutchman. I think this battle is the pinnacle of the movie.

A few days later, Artha mentioned that after the credits there is an easter-egg scene. And we missed it. That night I was somehow quite disappointed with the movie and rushed out of the theatre.

And last Wednesday (30/05/2007), Yoga asked me whether I have watched Pirates of Caribbean 3. I answered ‘No’ because I was thinking to repay him back his treat (He bought me the ticket for Pirates of Caribbean 2). Also, I just recently receive my first pay check from NTUC Income. So a small treat to a friend shouldn’t be a harm, right?

We watched the movie at The Cathay. To my surprise, I enjoy the movie better than the first time. No more conundrums after watching the movie. So if you think Pirates of Caribbean 3 is confusing, watch it again 🙂 And make sure you are not watching the after midnight show time.

So what’s with the title of this post? Back at Square Zero? Well, only those who have watched the movie know what it means =P So go get your movie ticket! (^_^)

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant:

I was tagged by Pieter Marbun yesterday.

The Rules:

* Players start with 8 random facts about themselves.
* Those who are tagged should post these rules and their 8 random facts.
* Players should tag eight other people and notify them that they have been tagged.

Eight random facts about me:

  1. Born in a village called Mojowarno. Mojowarno is a combination of two words, Mojo = is a kind of unedible fruit. Warno=Colour. The naming might be related to Mojopahit, a historical kingdom which was located relatively near to my village.
  2. Frequently mistaken as Filipino (^_^) In reality, I am a pure breed Indonesian Javanese (Well I can’t really say I’m pure breed since I never test my DNSA. Although I know exactly my lineage up to my great grandparents. All of them are Java programmer nese. (^_^)/ )
  3. Loves spicy foods
  4. Wanted to become a pirate. But turned off by Pirates of Malacca Straits. These pirates are neither cool nor savvy. They are just purely violent criminals.
  5. Lamb Chop with Black pepper sauce is probably the most frequent food I ordered in any food court
  6. The eldest of 4 brothers. That movie very unlikely to be inspired by me and my brothers 😀
  7. Believes in afterlife. Although I am in constant fight between good and evil (T_T)
  8. Procrastinates frequently =P

I shall continue to pass the tags to:

  1. Made Adi
  2. Gede Artha
  3. Adhi Wibowo
  4. Haqi
  5. Brett Mckay
  6. Culture Shiok!
  7. Lisa
  8. Karen

Keep it going …..

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant: