public void StartClean(refreshProgress progress)
        {
            Stop();

            workerThread = new Thread(new ThreadStart(delegate()
            {
                try
                {
                    Game[] dbGames = DB.Instance.GetGames("", true, true);

                    if (stopWorker)
                    {
                        return;
                    }

                    int gamesCount   = dbGames.Length;
                    int gamesRemoved = 0;

                    GUIGraphicsContext.form.Invoke(progress, new object[] { string.Format("Cleaning: checking rom {0} / {1}", 0, gamesCount), 0, false });
                    for (int x = 0; x < gamesCount; x++)
                    {
                        if (stopWorker)
                        {
                            return;
                        }

                        int perc = (int)Math.Round(((double)x / (gamesCount + 1)) * 100);
                        GUIGraphicsContext.form.Invoke(progress, new object[] { string.Format("Cleaning: checking rom {0} / {1}", x, gamesCount), perc, false });

                        Game game = dbGames[x];

                        if (!File.Exists(game.Path) || game.ParentEmulator == null)     //rom doesn't exist or is orphan
                        {
                            GUIGraphicsContext.form.Invoke(new System.Windows.Forms.MethodInvoker(delegate()
                            {
                                Logger.LogDebug("Removing '{0}' from database", game.Title);
                                GUIGraphicsContext.form.Invoke(progress, new object[] { string.Format("Cleaning: removing rom {0} / {1}", x, gamesCount), perc, false });
                            }
                                                                                                  ));

                            game.Delete();     //remove rom from DB
                            gamesRemoved++;
                        }

                        else if (game.ParentEmulator.UID > -1)
                        {
                            bool remove = true;
                            if (game.Path.StartsWith(game.ParentEmulator.PathToRoms))     //is rom in rom directory?
                            {
                                foreach (string extension in game.ParentEmulator.Filter.Split(';'))
                                {
                                    if (extension.EndsWith(Path.GetExtension(game.Path)) || extension == "*.*")     //does rom end with valid extension?
                                    {
                                        remove = false;
                                        break;
                                    }
                                }
                            }
                            if (remove)
                            {
                                //rom isn't in rom directory or doesn't have a valid extension
                                GUIGraphicsContext.form.Invoke(new System.Windows.Forms.MethodInvoker(delegate()
                                {
                                    Logger.LogDebug("Removing '{0}' from database", game.Title);
                                }
                                                                                                      ));

                                game.Delete();     //remove rom from DB
                                gamesRemoved++;
                            }
                        }
                    }

                    GUIGraphicsContext.form.Invoke(progress, new object[] { string.Format("Clean complete: removed {0} roms", gamesRemoved), 100, true });
                }
                catch (ThreadAbortException)
                {
                    Thread.ResetAbort();
                    GUIGraphicsContext.form.BeginInvoke(progress, new object[] { "Clean aborted", 100, true });
                }
            }
                                                      ));

            workerThread.Start();
        }
        public void StartRefresh(refreshProgress progress)
        {
            Stop();

            workerThread = new Thread(new ThreadStart(delegate()
            {
                try
                {
                    List <string> dbPaths = DB.Instance.GetAllGamePaths();
                    Emulator[] emus       = DB.Instance.GetEmulators();

                    if (stopWorker)
                    {
                        return;
                    }

                    Dictionary <Emulator, string[]> allNewPaths = new Dictionary <Emulator, string[]>();
                    int filesFound = 0;
                    progress.Invoke(string.Format("Refreshing: found {0} new files", filesFound), 0, false);
                    //GUIGraphicsContext.form.Invoke(progress, new object[] { string.Format("Refreshing: found {0} new files", filesFound), 0, false });

                    foreach (Emulator emu in emus)
                    {
                        if (stopWorker)
                        {
                            return;
                        }

                        string romDir = emu.PathToRoms;
                        if (!Directory.Exists(romDir))
                        {
                            continue; //rom directory doesn't exist, skip
                        }

                        List <string> newPaths = new List <string>();

                        foreach (string filter in emu.Filter.Split(';'))
                        {
                            if (stopWorker)
                            {
                                return;
                            }

                            string[] gamePaths;
                            try
                            {
                                gamePaths = Directory.GetFiles(romDir, filter, SearchOption.AllDirectories); //get list of matches
                            }
                            catch
                            {
                                continue; //error with filter, skip
                            }


                            for (int x = 0; x < gamePaths.Length; x++)
                            {
                                if (stopWorker)
                                {
                                    return;
                                }

                                if (!dbPaths.Contains(gamePaths[x]) && !newPaths.Contains(gamePaths[x])) //check that path is not already in DB
                                {
                                    newPaths.Add(gamePaths[x]);
                                    filesFound++;
                                    progress.Invoke(string.Format("Refreshing: found {0} new files", filesFound), 0, false);
                                    //GUIGraphicsContext.form.Invoke(progress, new object[] { string.Format("Refreshing: found {0} new files", filesFound), 0, false });
                                }
                            }
                        }

                        if (newPaths.Count > 0)
                        {
                            allNewPaths.Add(emu, newPaths.ToArray());
                        }
                    }

                    int filesAdded = 0;
                    foreach (KeyValuePair <Emulator, string[]> val in allNewPaths)
                    {
                        foreach (string path in val.Value)
                        {
                            if (stopWorker)
                            {
                                return;
                            }

                            DB.Instance.AddGame(path, val.Key); //add new rom to DB
                            filesAdded++;
                            int perc = (int)Math.Round(((double)filesAdded / filesFound) * 100);
                            progress.Invoke(string.Format("Refreshing: adding {0} / {1} new files", filesAdded, filesFound), perc, false);
                            //GUIGraphicsContext.form.Invoke(progress, new object[] { string.Format("Refreshing: adding {0} / {1} new files", filesAdded, filesFound), perc, false });
                        }
                    }
                    progress.Invoke(string.Format("Refresh complete: added {0} new files", filesAdded), 100, true);
                    //GUIGraphicsContext.form.BeginInvoke(progress, new object[] { string.Format("Refresh complete: added {0} new files", filesAdded), 100, true });
                }
                catch (ThreadAbortException)
                {
                    Thread.ResetAbort();
                    progress.Invoke("Refresh aborted", 100, true);
                    //GUIGraphicsContext.form.BeginInvoke(progress, new object[] { "Refresh aborted", 100, true });
                }
            }
                                                      ));
            workerThread.Start();
        }