/// <summary> /// Actualize new parameters. /// </summary> /// <param name="parameters"></param> virtual public void Switch(Parameters parameters) { try { if (parameters.Synchronize) { if (string.IsNullOrWhiteSpace(parameters.SynchronizationFolder)) { throw new Exception("SynchronizationFolder is not set."); } //if (string.IsNullOrWhiteSpace(parameters.UploadFolderName)) // throw new Exception("UploadFolderName is not set."); //if (string.IsNullOrWhiteSpace(parameters.DownloadFolderName)) // throw new Exception("DownloadFolderName is not set."); this.parameters = parameters; //synchronizedSettingsFieldFullNames= synchronizedSettingsFieldFullNames.Distinct().ToList(); downloadFolder = FileSystemRoutines.CreateDirectory(parameters.SynchronizationFolder + Path.DirectorySeparatorChar + parameters.DownloadFolderName); uploadFolder = FileSystemRoutines.CreateDirectory(parameters.SynchronizationFolder + Path.DirectorySeparatorChar + parameters.UploadFolderName); //if (PathRoutines.IsDirWithinDir(downloadFolder, uploadFolder)) // throw new Exception("DownloadFolder cannot be within UploadFolder: \r\n" + downloadFolder + "\r\n" + uploadFolder); //if (PathRoutines.IsDirWithinDir(downloadFolder, uploadFolder)) // throw new Exception("UploadFolder cannot be within DownloadFolder: \r\n" + uploadFolder + "\r\n" + downloadFolder); appSetupFolder = FileSystemRoutines.CreateDirectory(parameters.SynchronizationFolder); if (pollingThread?.IsAlive != true) { pollingThread = ThreadRoutines.Start(polling); } } } catch (Exception e) { ErrorHandler(e); } }
static void setRootDir(bool create) { lock (lockObject) { if (rootDir != null) { if (!create) { return; } if (Directory.Exists(rootDir)) { return; } } List <string> baseDirs = new List <string> { CompanyUserDataDir, CompanyCommonDataDir, Log.AppDir, Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), Path.GetTempPath() + Path.DirectorySeparatorChar + CompanyName + Path.DirectorySeparatorChar, }; if (Log.baseDirs != null) { baseDirs.InsertRange(0, Log.baseDirs); } foreach (string baseDir in baseDirs) { BaseDir = baseDir; rootDir = BaseDir + Path.DirectorySeparatorChar + rootDirName + RootDirNameSuffix; if (create) { try { if (!Directory.Exists(rootDir)) { FileSystemRoutines.CreateDirectory(rootDir); } string testFile = rootDir + Path.DirectorySeparatorChar + "test"; File.WriteAllText(testFile, "test"); File.Delete(testFile); break; } catch //(Exception e) { rootDir = null; } } } if (rootDir == null) { throw new Exception("Could not access any log directory."); } rootDir = PathRoutines.GetNormalizedPath(rootDir, false); if (Directory.Exists(rootDir) && deleteLogsOlderThanDays >= 0 && deletingOldLogsThread?.IsAlive != true) { deletingOldLogsThread = ThreadRoutines.Start(() => { Log.DeleteOldLogs(deleteLogsOlderThanDays, DeleteOldLogsDialog); });//to avoid a concurrent loop while accessing the log file from the same thread } } }
//static readonly Dictionary<Control, Thread> controls2sliding_thread = new Dictionary<Control, Thread>(); public static Thread Condense(this Form f, double opacityPerMss, double opacity2, double step = 0.05, MethodInvoker finished = null) { lock (f) { Thread t = null; //if (controls2condensing_thread.TryGetValue(f, out t) && t.IsAlive) // return t; step = f.Opacity < opacity2 ? step : -step; double total_mss = Math.Abs(opacity2 - f.Opacity) / opacityPerMss; int sleep = (int)(total_mss / ((opacity2 - f.Opacity) / step)); t = ThreadRoutines.Start(() => { try { while (!(bool)ControlRoutines.Invoke(f, () => { f.Opacity = f.Opacity + step; return step > 0 ? f.Opacity >= opacity2 : f.Opacity <= opacity2; }) ) System.Threading.Thread.Sleep(sleep); ControlRoutines.Invoke(f, () => { finished?.Invoke(); }); } catch (Exception e)//control disposed { } }); //controls2condensing_thread[f] = t; return t; } }
public static Thread SlideVertically(this Control c, double pixelsPerMss, int position2, int step = 1, MethodInvoker finished = null) { lock (c) { Thread t = null; //if (controls2sliding_thread.TryGetValue(c, out t) && t.IsAlive) // return t; step = c.Top > position2 ? -step : step; double total_mss = Math.Abs(position2 - c.Top) / pixelsPerMss; int sleep = (int)(total_mss / ((position2 - c.Top) / step)); t = ThreadRoutines.Start(() => { try { while (c.Visible && !(bool)ControlRoutines.Invoke(c, () => { c.Top = c.Top + step; return step < 0 ? c.Top <= position2 : c.Top >= position2; }) ) System.Threading.Thread.Sleep(sleep); ControlRoutines.Invoke(c, () => { finished?.Invoke(); }); } catch(Exception e)//control disposed { } }); //controls2sliding_thread[c] = t; return t; } }
/// <summary> /// Stop the code execution if the both conditions are met: /// 1)the entry assembly was compiled as a release version; /// 2)the test period expired. /// </summary> /// <param name="year">year when the test period expires; if it is less than 1 then the check is not performed</param> /// <param name="month">month when the test period expires</param> /// <param name="day">day when the test period expires</param> /// <param name="for_release_only"></param> static public void CHECK_TEST_PERIOD_VALIDITY(int year, int month, int day, bool silently = false, bool for_release_only = false) { try { InternetDateTime.silently = silently; //if (year < 1) // return; //if (for_release_only && !IsReleaseVersion(Assembly.GetEntryAssembly())) // return; //LogMessage.Inform("It is a demo version that is valid until " + year + "-" + month + "-" + day); if (!silently) { Log.Main.Inform("It is a demo version that is valid until " + year + "-" + month + "-" + day); } ThreadRoutines.StartTry(() => { if (new DateTime(year, month, day) < InternetDateTime.GetOverHttp()) { Action a = (Action)(() => { LogMessage.Exit("The test time expired. \nPlease contact the vendor if you want to use this software."); }); if (System.Windows.Forms.Application.OpenForms.Count > 0) { System.Windows.Forms.Application.OpenForms[0].Invoke(a); } else if (System.Windows.Application.Current != null) { System.Windows.Application.Current.Dispatcher.Invoke(a); } else { ThreadRoutines.Start(() => { Thread.Sleep(5000); Environment.Exit(0); }); a(); } } }, (Exception e) => { LogMessage.Exit(e); } ); } catch (Exception e) { LogMessage.Exit(e); } }
public static object WaitForCondition(WebBrowser browser, Func <object> check_condition, int mss) { if (browser.InvokeRequired) { return(browser.Invoke(new delegateWaitForCondition(WaitForCondition), browser, check_condition, mss)); } DateTime timeout = DateTime.Now.AddMilliseconds(mss); while (DateTime.Now < timeout) { if (browser.Document != null && browser.Document.Body != null) { object o = check_condition(); if (o != null) { return(o); } } ThreadRoutines.Wait(20); } return(null); }
static void setWorkDir(bool create) { lock (lockObject) { if (workDir != null) { if (!create) { return; } if (Directory.Exists(workDir)) { return; } } List <string> baseDirs = new List <string> { Log.AppDir, CompanyUserDataDir, CompanyCommonDataDir, Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), System.IO.Path.GetTempPath() + System.IO.Path.DirectorySeparatorChar + CompanyName + System.IO.Path.DirectorySeparatorChar, }; if (Log.primaryBaseDirs != null) { baseDirs.InsertRange(0, Log.primaryBaseDirs); } foreach (string baseDir in baseDirs) { workDir = baseDir + System.IO.Path.DirectorySeparatorChar + Log.ProcessName + WorkDirNameSuffix; if (create) { try { if (!Directory.Exists(workDir)) { FileSystemRoutines.CreateDirectory(workDir); } string testFile = workDir + System.IO.Path.DirectorySeparatorChar + "test"; File.WriteAllText(testFile, "test"); File.Delete(testFile); Log.baseDir = baseDir; break; } catch //(Exception e) { workDir = null; } } } if (workDir == null) { throw new Exception("Could not access any log directory."); } workDir = PathRoutines.GetNormalizedPath(workDir, false); if (Directory.Exists(workDir) && deleteLogsOlderDays >= 0) { if (deletingOldLogsThread?.TryAbort(1000) == false) { throw new Exception("Could not abort deletingOldLogsThread"); } deletingOldLogsThread = ThreadRoutines.Start(() => { Log.DeleteOldLogs(deleteLogsOlderDays, DeleteOldLogsDialog); });//to avoid a concurrent loop while accessing the log file from the same thread } else { throw new Exception("Could not create log folder!"); } } // deletingOldLogsThread?.Join(); }
/// <summary> /// Replacement for BeginInvoke() which is not supported in .NET5 /// </summary> /// <param name="delegate"></param> /// <param name="ps"></param> public static void BeginInvoke(this Delegate @delegate, params object[] ps) { ThreadRoutines.Start(() => { @delegate.DynamicInvoke(ps); }); }