void backgroundWorkerTimeAgo_DoWork(object sender, DoWorkEventArgs e) { try { backgroundWorkerTimeAgoArgument arguments; try { arguments = e.Argument as backgroundWorkerTimeAgoArgument; } catch { arguments = new backgroundWorkerTimeAgoArgument(DateTime.Now, null); } if (e.Cancel) { return; } bool stopUpdating = false; int NextUpdateInXSeconds = 60; string output = Helper.DateHelpers.getHumanReadableAgo(arguments.CreatedAt, out NextUpdateInXSeconds, out stopUpdating); e.Result = new backgroundWorkerTimeAgoResult(output, NextUpdateInXSeconds, false); if (string.IsNullOrEmpty(arguments.OldHumanReadableString)) { e.Result = new backgroundWorkerTimeAgoResult(output, NextUpdateInXSeconds, stopUpdating); } else if (output != arguments.OldHumanReadableString) { e.Result = new backgroundWorkerTimeAgoResult(output, NextUpdateInXSeconds, stopUpdating); } else { e.Result = new backgroundWorkerTimeAgoResult(output, NextUpdateInXSeconds, stopUpdating); } } catch (Exception exp) { Console.WriteLine(exp.Message); } }
void backgroundWorkerTimeAgo_DoWork(object sender, DoWorkEventArgs e) { try { backgroundWorkerTimeAgoArgument arguments; try { arguments = e.Argument as backgroundWorkerTimeAgoArgument; } catch { arguments = new backgroundWorkerTimeAgoArgument(DateTime.Now, null); } if (e.Cancel) { return; } string output = String.Empty; bool stopUpdating = false; TimeSpan time = DateTime.Now - arguments.CreatedAt; int NextUpdateInXSeconds = 60; if (time.TotalSeconds <= 0) { output += " right now"; // if date in the future wait until it in the current time e.Result = new backgroundWorkerTimeAgoResult(output, Math.Min(2, -1 * Convert.ToInt32(time.TotalSeconds))); return; } if (time.Days > 0) { output = arguments.CreatedAt.ToLongDateString(); NextUpdateInXSeconds = -1; stopUpdating = true; } else if (time.Hours > 0) { if (time.Hours == 1) { output += "one hour ago"; } else { output += time.Hours + " hours ago"; } NextUpdateInXSeconds = 10; NextUpdateInXSeconds += (60 - time.Minutes) * 60; NextUpdateInXSeconds += (60 - time.Seconds); } else if (time.Minutes > 0) { if (time.Minutes == 1) { output += "one minute ago "; } else { output += time.Minutes + " minutes ago "; } NextUpdateInXSeconds += (60 - time.Seconds); } else { NextUpdateInXSeconds = 1; if (time.Seconds == 1) { output += time.Seconds + " second ago"; } else { output += time.Seconds + " seconds ago"; } } if (string.IsNullOrEmpty(arguments.OldHumanReadableString)) { e.Result = new backgroundWorkerTimeAgoResult(output, NextUpdateInXSeconds, stopUpdating); } else if (output != arguments.OldHumanReadableString) { e.Result = new backgroundWorkerTimeAgoResult(output, NextUpdateInXSeconds, stopUpdating); } else { e.Result = new backgroundWorkerTimeAgoResult(output, NextUpdateInXSeconds, stopUpdating); } } catch (Exception exp) { Console.WriteLine(exp.Message); } }