Code Optimization

Interesting things about software development and code optimization

Dropbox as a service (Windows Service for Windows Server)

Hello friends,


Have you searched for a Dropbox windows service so it would synchronize your files and folders on a specific time and without any logged in windows user? I guess yes and as me it seems that you have not found anything appropriate.

The question is not only about synchronization if we talk about windows server but about performance as well. As you know the Dropbox service use a lot of system resources like CPU, HDD/SSD, RAM while it is running and this is crucial for a server because of any server has to use as less resources as possible at any given time.


So that is why I implemented my own workflow to control dropbox. There are two main points:

- run non-GUI Dropbox instance on a specific time frame only

- if there is any dropbox instance outside of the specified timeframe then kill it immediately.


To run the dropbox without GUI and to start synchronizing files I use the following windows task scheduler:





And here is the windows service we will install and run indefinitely to monitor any dropbox process and kill it:

    public partial class Service1 : ServiceBase
    {
        System.Threading.Timer timer = null;
        System.Threading.Thread tt = null;
        volatile bool stop = false;
        var perod = new Setting();

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            perod.StartFrom = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 25, 0);
            perod.EndTo = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 25, 0).AddHours(1);
            
            tt = new System.Threading.Thread((Action) =>
            {
                if (timer == null)
                    timer = new System.Threading.Timer(new System.Threading.TimerCallback(KillDropbox), this, 5 * 1000, 60 * 1000);

                while (!stop)
                {
                    System.Threading.Thread.Sleep(3000);
                }
            });
            tt.Start();
        }

        void KillDropbox(object o)
        {
            try
            {
                if (DateTime.Now.TimeOfDay >= perod.StartFrom.TimeOfDay && DateTime.Now.TimeOfDay <= perod.EndTo.TimeOfDay)
                    return;

                int pc = 0;
                ReScan:
                var proccs = System.Diagnostics.Process.GetProcesses().Where(p => p.ProcessName.ToLowerInvariant().StartsWith("dropbox"));
                pc = proccs.Count();
                foreach (var proc in proccs)
                {
                    try
                    {
                        proc.Kill();
                        
                        System.Threading.Thread.Sleep(1000);
                        goto ReScan;
                    }
                    catch (Exception eex)
                    {
                    }
                }
            }
            catch(Exception ex)
            {
            }
            
        }

        protected override void OnStop()
        {
            stop = true;

            if (timer != null)
            {
                timer.Dispose();
                timer = null;
            }
            if (tt != null)
            {
                tt.Join(5000);
                tt = null;
            }
        }
    }


So after all we have a scheduler that runs non-GUI dropbox, dropbox synchronizes all configured files and folders and our windows service kills it outside of time frame.


Note: You should select your own time frame that will give dropbox enough time to synchronize everything.


It is also worth to mention that you will have to login to your server from time to time and update and sign into your Dropbox application in other way it will stop synchronization (something special that relates to dropbox itself).


Thank you, hope that was helpful!


1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y



Loading