How to Gently Kill a Thread

Creating your own thread is not something that I do lightly. There are so many alternatives these days where framework does “dirty job” for you.

However, sometime making your own thread is way to go. I found that mostly I use same code in order to cancel it.

Idea is creating ManualResetEvent that we can check fairly quick from thread loop. Once that event switches it’s value to true, our thread should terminate.

private ManualResetEvent _cancelEvent;
private Thread _thread;

public void Start() {
    _cancelEvent = new ManualResetEvent(false);
    _thread = new Thread(Run);
    _thread.IsBackground = true;
    _thread.Name = "EntranceBarrier";
    _thread.Priority = ThreadPriority.AboveNormal;
    _thread.Start();
}

public void Stop() {
    _cancelEvent.Set();
    while (_thread.IsAlive) { Thread.Sleep(10); } //wait until it is really stopped
}

bool IsCanceled {
    get { return _cancelEvent.WaitOne(0, false); }
}

void Run() {
    while (!IsCanceled) {
        //some code
        if (this.IsCanceled) { return; }
        //some code
    }
}

P.S. This will terminate thread gently and it only works if event is checked often. If you have code that is waiting for system event, this is not solution for you.

MagiWOL 2.20

After very long time (more than a year!) here is update to my Wake-On-LAN program.

This update consists mostly of bug-fixes with single new feature. One can define custom wait period between sending packets. This change will not be used much but for situations where you need to wake hundred of computers without burning fusebox it is definitely crucial.

Additional small tweak that may stop some frustration is change of console tool to wol.exe. This way one working in command-line can write only three letters instead of eight (it was magiwolc.exe).

Download is ready.

GPS Should Stop

One error that creeps quite often in Android applications is not handling being in background gracefully. It is error that is really easy to make since your application works as it normally would. However, it does use more resources than it is needed and resources usually map to battery life.

One example of bad behavior that comes to mind is updating location non-stop. Most often application needs to update their location only when user is looking at it. Once application is in background GPS should be turned off (although there are some application that have good reason to leave it going).

Easiest way to do it is just handling all GPS “subscriptions” in onResume and onPause methods. Those methods get called each time application comes to foreground (onResume) or leaves it (onPause).

Generic solution would look something like this:

private LocationManager thisLocationManager;

@Override
public void onCreate(Bundle savedInstanceState) {
   ...
   thisLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
}

@Override
protected void onResume() {
    thisLocationManager.requestLocationUpdates("gps", 0, 0, gpsLocationListener);
    super.onResume();
}

@Override
protected void onPause() {
    thisLocationManager.removeUpdates(gpsLocationListener);
    super.onPause();
}

private LocationListener gpsLocationListener = new LocationListener() {
   ...
};

P.S. Do note that this code is not optimal in any way (e.g. hard-coding “gps” as provider name). Intention is just to illustrate concept.

Getting GUID Value After SQL Insert

Whoever used identity columns in SQL Server probably used SCOPE_IDENTITY in order to retrieve newest identity value after insert.

For example, if we have two columns, one named Id and declared as identity column and other named Test with text inside, we would use something like this in order to insert new row:

INSERT INTO Example(Test) VALUES('test'); SELECT SCOPE_IDENTITY();

Once we use ExecuteScalar, we would get newly inserted identity value (in column Id).

Once we enter world of Guids, we cannot do that. Guid is not considered identity value and same rules do not apply to it. In order to have similar example we shall have also two columns here. However, this time Id column will be of uniqueidentifier type and it will have it’s default set to NEWID(). This way client behavior is same. Once we insert new row (without specifying Id value) we will get Id assigned to us.

In order to retrieve value for Id column, we need to change SQL a little:

INSERT INTO Example(Test) OUTPUT INSERTED.Id VALUES('test');

Antena

Illustration

Another issue of scratching-an-itch with new application is my newest Android application. I had to mount my terrestrial TV antenna and I figured that there should be easier way to align it with DVB-T signal towers.

Result of that “one night stand” is Antena. Application that will show you all towers in 100 km radius and signal you when alignment is made. For that purpose it needs something to give it location (GPS?) and compass.

Unfortunately it is usable only in Croatia. There are no plans to make it work in others countries.

P.S. If someone is really interested in making it work somewhere else, all that is needed are GPS coordinates of towers and a beer. With those two items we can make it work anywhere.