Write to preferences file in other thread

In order to avoid disk writes in the main thread, now it writes in a separated thread