I am facing an issue with my timer. The issue is, timer goes negative after reaching the specified time limit and the next levels never unlocks. This only happens whenever I change my timespan to 24 hours. When I am using seconds or minutes the timer stops when it reaches to zero (0) and make the button interactable again. I have also tested it on 1 hour and it works fine.
IEnumerator TimeUpdate()
{
if (bonus != null)
{
if (PlayerPrefs.GetInt("Bonus", 0) == 0)
{
bonus.interactable = true;
showTimer.gameObject.SetActive(false);
PlayerPrefs.SetString("BONUS_END_TIME", "");
StopAllCoroutines();
}
else
{
bonus.interactable = false;
showTimer.gameObject.SetActive(true);
}
}
while (true)
{
chkbonustime();
DateTime dt = DateTime.Now;
string bonusendtime = PlayerPrefs.GetString("BONUS_END_TIME", "");
DateTime dateComplete;
if (bonusendtime != null)
{
dateComplete = DateTime.Parse(bonusendtime);
DateTime ENDTIME = dateComplete.Add(TimeSpan.FromHours(24));
TimeSpan ABC = ENDTIME - dt;
showTimer.text = ABC.Hours + " : " + ABC.Minutes + " : " + ABC.Seconds;
}
// Debug.Log();
yield return new WaitForSeconds(1);
}
}
public void chkbonustime()
{
string bonusendtime = PlayerPrefs.GetString("BONUS_END_TIME", "");
if (!bonusendtime.Equals(""))
{
DateTime dateComplete = DateTime.Parse(bonusendtime);
DateTime xyz = DateTime.Now;
TimeSpan timespan = xyz - dateComplete;
Debug.Log(timespan.Seconds);
if (timespan.Hours >= 24)
{
// if (PlayerPrefs.GetInt("Bonus", 0) == 1)
// {
PlayerPrefs.SetInt("Bonus", 0);
bonus.interactable = true;
showTimer.gameObject.SetActive(false);
PlayerPrefs.SetString("BONUS_END_TIME", "");
StopAllCoroutines();
// }
// else
// {
// bonus.interactable = false;
// showTimer.gameObject.SetActive(true);
// }
}
//else
// return false;
// PlayerPrefs.SetString("BONUS_END_TIME", "");
}
else
{
// return false;
}
}
Hours is only between 0-23, you likely want TotalHours or TotalDays.
Related
I would like help.
There are some problems with the code.
I want to do only one inspection at a certain time every day.
In particular, the problem is the most serious in DateTime.Now.Hour == 11 part.
I am having difficulty checking certain times. I need to write code that can be checked only once at 11:00 in the whlie statement.
I created a license file and checked the date of the file.
public static CResult Dailytime(string date)
{
CResult result = new CResult();
if(result.nRet == 0)
{
while (true)
{
if (result.nRet == 1 || result.nRet == 2)
{
return result;
}
if (DateTime.Now.Hour == 11)
{
result = DailyCheckData(date);
if(result.nRet == 1 || result.nRet == 2)
{
return result;
}
}
System.Threading.Thread.Sleep(60 * 30 * 1000);
}
}
return result;
}
public static CResult DailyCheckData(string data)
{
CResult result = new CResult();
DateTime licenseDate = Convert.ToDateTime(data);
string dateNow = DateTime.Now.ToString("yyyy-MM-dd");
int compareDate = DateTime.Compare(Convert.ToDateTime(data), DateTime.Now);
if (licenseDate.ToString("yyyy-MM-dd") == dateNow)
{
result = ExpirationCertificate();
Console.WriteLine("Result = " + result.Result + " Msg = " + result.Msg + " nRet = " + result.nRet);
return result;
}
else
{
if (compareDate > 0)
{
result = TrueCertificate();
Console.WriteLine("Result = " + result.Result + " Msg = " + result.Msg + " nRet = " + result.nRet);
}
else if (compareDate <= 0)
{
result = ExpirationCertificate();
Console.WriteLine("Result = " + result.Result + " Msg = " + result.Msg + " nRet = " + result.nRet);
}
return result;
}
}
CResult class
nRet= 0 or 1 or 2
0 = fasle date
1 = false file
2 = true
Suggest or suggest ways to improve.
Can you try create a variable for DateTime.Now, after a line of code that value change.
DateTime licenseDate = Convert.ToDateTime(data);
string dateNow = DateTime.Now.ToString("yyyy-MM-dd");
int compareDate = DateTime.Compare(Convert.ToDateTime(data), DateTime.Now);
To
DateTime licenseDate = Convert.ToDateTime(data);
var now = DateTime.Now;
string dateNow = now.ToString("yyyy-MM-dd");
int compareDate = DateTime.Compare(licenseDate, now);
you shouldn't use Thread.Sleep() method for such a long duration. It is poor programming logic to make the thread sleep for such a long period.
What you can do to solve this is, is to make a Timer. There's an example attached in the link. A simple snippet to match your logic would be:
licenseStart = //setYours
lastCheck = DateTime.Now;
nextCheck = now.AddDays(1); // now.AddHours(1)
var timer = new Timer(o=>{/* Do work*/}, null, TimeSpan.Zero, nextCheck);
Hope it helps!
I asked about logic that can only loop once at 11 o'clock.
But I could not find the right answer and I found the answer.
I do not speak English well.
That's why others do not understand the intent of the question.
bool bOnce = true;
//bool s0nce = true;
while (true)
{
if (DateTime.Now.Hour == 11 && bOnce)
{
//run code
}
if (bOnce == true)
bOnce = false;
else
bOnce = true;
Thread.Sleep(10 * 60 * 1000);
}
I have an Windows Application which uses timer like in the screen:
timer it's used to run a method every X minutes between 08:00:00 (Start Time) and 21:00:00 (Stop Time)
In the screen:
Next download Start at: shows what time the next run will occur
Time Left: shows the remaining time until next run will occur
Download Interval: time span between two runs
The method runs when the Next download Start = Current Time.
My issue is that event that the application is open and runs, the timer just stops after a while.
Any ideas why this happens, is this normal or am I doing something wrong in my code (i can post it if needed but now I was just asking to see if it's normal)?
Is there a way to prevent it or shell I create a checker and check his status regularly?
Thanks in advance for your support!
private System.Windows.Forms.Timer timerFrequency = new System.Windows.Forms.Timer();
public void LoadGeneraInfoPanel()
{
timerFrequency.Interval = 1000;
timerFrequency.Enabled = true;
this.timerFrequency.Tick += new System.EventHandler(this.timerFrequency_Tick);
timerFrequency.Start();
}
private void timerFrequency_Tick(object sender, EventArgs e)
{
txtGICurrTime.Text = DateTime.Now.ToString("HH:mm:ss");
if (drSystemParam["Auto"].ToString() == "True" && timerFrequency.Enabled)
{
if (txtGIFrequency.Text.ToString() != string.Empty && txtGIStopTime.Text.ToString() != string.Empty && txtGIStartTime.Text.ToString() != string.Empty)
{
int iHours = Convert.ToInt32(txtGIFrequency.Text.Substring(0, 2));
int iMinutes = Convert.ToInt32(txtGIFrequency.Text.Substring(3, 2));
int iSeconds = Convert.ToInt32(txtGIFrequency.Text.Substring(6, 2));
DateTime dCurrStartTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, Convert.ToInt32(txtGIStartTime.Text.Substring(0, 2)), Convert.ToInt32(txtGIStartTime.Text.Substring(3, 2)), Convert.ToInt32(txtGIStartTime.Text.Substring(6, 2)));
DateTime dCurrStopTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, Convert.ToInt32(txtGIStopTime.Text.Substring(0, 2)), Convert.ToInt32(txtGIStopTime.Text.Substring(3, 2)), Convert.ToInt32(txtGIStopTime.Text.Substring(6, 2)));
DateTime dCurrStartDownloadTime = dCurrStartTime;
for (int j = 0; dCurrStartDownloadTime < DateTime.Now & dCurrStartDownloadTime <= dCurrStopTime; j++)
dCurrStartDownloadTime = dCurrStartDownloadTime.AddHours(iHours).AddMinutes(iMinutes).AddSeconds(iSeconds);
var result = dCurrStartDownloadTime;
if (DateTime.Now > dCurrStartTime && DateTime.Now < dCurrStopTime.AddHours(-iHours).AddMinutes(-iMinutes).AddSeconds(-iSeconds))
{
if (txtNextDownloadTime.Text == String.Empty)
txtNextDownloadTime.Text = result.ToString("HH:mm:ss");
TimeSpan diff = result - DateTime.Now.AddSeconds(-1);
txtGITimeLeft.Text = string.Format("{0}:{1}:{2}", diff.Hours.ToString().PadLeft(2, '0'), diff.Minutes.ToString().PadLeft(2, '0'), diff.Seconds.ToString().PadLeft(2, '0'));
if (DateTime.Now.ToString("HH:mm:ss") == txtNextDownloadTime.Text)
{
timerFrequency.Stop();
txtNextDownloadTime.Text = result.ToString("HH:mm:ss");
Thread.Sleep(800);
timerFrequency.Enabled = true;
timerFrequency.Start();
btnEecuteMethod_Click(sender, e);
}
}
else
{
result = dCurrStartTime.AddDays(1);
txtNextDownloadTime.Text = result.ToString("HH:mm:ss");
TimeSpan diff = result - DateTime.Now.AddSeconds(-1);
txtGITimeLeft.Text = string.Format("{0}:{1}:{2}", diff.Hours.ToString().PadLeft(2, '0'), diff.Minutes.ToString().PadLeft(2, '0'), diff.Seconds.ToString().PadLeft(2, '0'));
}
}
else
{
timerFrequency.Enabled = false;
timerFrequency.Stop();
}
}
}
Whats Happening:
It works for the most part except after some time after using the offline coin count is always 0. Im am trying to make it so the player cant just change their system time to try to get more money faster. I also am not trying to do this by connection to server for time check. Does anyone see whats going wrong?
If statement is in awake:
if (PlayerPrefs.GetInt("offlineCheck").Equals(1))
{
ButtonCanvas.gameObject.SetActive(false);
OfflineCanvas.gameObject.SetActive(true);
PlayerPrefs.SetInt("offlineCheck", 0);
// year not the same
if (!PlayerPrefs.GetInt("year").Equals(System.DateTime.Now.Year))
{
// Add 24 Hour Coins
if (PlayerPrefs.GetInt("year") < System.DateTime.Now.Year)
{
offlineCoinCount = PlayerPrefs.GetInt("OfflineCoinsVal") * (24 * 60);
} else {
offlineCoinCount = 0;
}
}
else
{ // same Year
// month not the same
if (!PlayerPrefs.GetInt("month").Equals(System.DateTime.Now.Month))
{
// Add 24 Hour Coins
if (PlayerPrefs.GetInt("month") < System.DateTime.Now.Month)
{
offlineCoinCount = PlayerPrefs.GetInt("OfflineCoinsVal") * (24 * 60);
} else {
offlineCoinCount = 0;
}
}
else
{ // Same Month
// day not the same
if (!PlayerPrefs.GetInt("day").Equals(System.DateTime.Now.Day))
{
// Add 24 hours?
if (PlayerPrefs.GetInt("day") < System.DateTime.Now.Day)
{
offlineCoinCount = PlayerPrefs.GetInt("OfflineCoinsVal") * (24 * 60);
} else {
offlineCoinCount = 0;
}
}
else
{ // Same Day
// hour not the same
if (!PlayerPrefs.GetInt("timeHour").Equals(System.DateTime.Now.Hour))
{
// Add Coins
if (PlayerPrefs.GetInt("timeHour") < System.DateTime.Now.Hour)
{
offlineCoinCount = PlayerPrefs.GetInt("OfflineCoinsVal") * (((System.DateTime.Now.Hour - PlayerPrefs.GetInt("timeHour")) * 60) - (System.DateTime.Now.Minute - PlayerPrefs.GetInt("timeMin")));
} else {
offlineCoinCount = 0;
}
}
else
{ // Same hour
// min not the same
if (!PlayerPrefs.GetInt("timeMin").Equals(System.DateTime.Now.Minute))
{
// Add coins
offlineCoinCount = PlayerPrefs.GetInt("OfflineCoinsVal") * (System.DateTime.Now.Minute - PlayerPrefs.GetInt("timeMin"));
}
else
{ // Same min
}
}
}
}
}
}
Rest of Code for this anti-time cheat
private void OnApplicationQuit()
{
saveDateTime();
PlayerPrefs.SetInt("offlineCheck", 1);
//offlineCoinCount = 0;
}
private void OnApplicationFocus(bool focus)
{
saveDateTime();
PlayerPrefs.SetInt("offlineCheck", 1);
//offlineCoinCount = 0;
}
private void OnApplicationPause(bool pause)
{
saveDateTime();
PlayerPrefs.SetInt("offlineCheck", 1);
//offlineCoinCount = 0;
}
public void saveDateTime()
{
//Player Restart Game
//////
/// Check Time for coin bonus
////
if (PlayerPrefs.GetInt("year").Equals(System.DateTime.Now.Year))
{ // Same Year
if (PlayerPrefs.GetInt("month").Equals(System.DateTime.Now.Month))
{ // Same Month
if (PlayerPrefs.GetInt("day").Equals(System.DateTime.Now.Day))
{ // Same Day
if (PlayerPrefs.GetInt("timeHour").Equals(System.DateTime.Now.Hour))
{ // Same Hour
if (PlayerPrefs.GetInt("timeMin").Equals(System.DateTime.Now.Minute))
{ // Same Minute
}
else
{ // Different Minute
// Update if new Minute is higher than old
if (PlayerPrefs.GetInt("timeMin") < System.DateTime.Now.Month)
{
PlayerPrefs.SetInt("timeMin", System.DateTime.Now.Minute);
}
}
}
else
{ //Different Hour
// Update if new hour is higher than old
if (PlayerPrefs.GetInt("timeHour") < System.DateTime.Now.Month)
PlayerPrefs.SetInt("timeHour", System.DateTime.Now.Hour);
PlayerPrefs.SetInt("timeMin", System.DateTime.Now.Minute);
}
}
else
{ // Different Day
// Update if new day is higher than old
if (PlayerPrefs.GetInt("day") < System.DateTime.Now.Month)
{
PlayerPrefs.SetInt("day", System.DateTime.Now.Year);
PlayerPrefs.SetInt("timeHour", System.DateTime.Now.Hour);
PlayerPrefs.SetInt("timeMin", System.DateTime.Now.Minute);
}
}
}
else
{ // Different Month
// Update if new month is higher than old
if (PlayerPrefs.GetInt("month") < System.DateTime.Now.Month)
{
PlayerPrefs.SetInt("month", System.DateTime.Now.Year);
PlayerPrefs.SetInt("day", System.DateTime.Now.Year);
PlayerPrefs.SetInt("timeHour", System.DateTime.Now.Hour);
PlayerPrefs.SetInt("timeMin", System.DateTime.Now.Minute);
}
}
}
else
{ // Different Year
// Update if new year is higher than old
if (PlayerPrefs.GetInt("year") < System.DateTime.Now.Year)
{
PlayerPrefs.SetInt("year", System.DateTime.Now.Year);
PlayerPrefs.SetInt("month", System.DateTime.Now.Year);
PlayerPrefs.SetInt("day", System.DateTime.Now.Year);
PlayerPrefs.SetInt("timeHour", System.DateTime.Now.Hour);
PlayerPrefs.SetInt("timeMin", System.DateTime.Now.Minute);
}
}
}
Want to do a loop of picture slideshow to start from a specific time and then to end at another specific time.
Also, how to set the DateTime picker to select minutes/hours?
//Under start button
if (rbtnDateTime.Checked == true)
{
DateTime startDate = dateTimePicker1.Value.Date;
DateTime stopDate = dateTimePicker2.Value.Date;
//Given time interval in seconds
if (mtxtSlideShowInterval.Text != "")
{
int interval = Convert.ToInt16(mtxtSlideShowInterval.Text);
while ((startDate = startDate.AddSeconds(interval)) <= stopDate)
{
timerSlideShow.Enabled = true;
timerSlideShow.Start();
}
timerSlideShow.Stop();
timerSlideShow.Enabled = false;
}
}
//Under timer_tick event
//Infinite Loop
else
{
if (listBoxPicturesInAlbum.SelectedIndex ==listBoxPicturesInAlbum.Items.Count - 1)
{
listBoxPicturesInAlbum.SelectedIndex = 0;
}
else
{
listBoxPicturesInAlbum.SelectedIndex++;
}
}
I would be rid of the while loop you have, and take a few steps.
First, you'll need a field to mark your current index:
int currentImageIndex = 0;
Second, you'll set your timer.Interval.
timer.Interval = int.Parse(mtxtSlideShowInterval.Text) * 1000;
Third, your timer_tick can include something like this:
if (DateTime.Now < dateTimePicker2.Value.Date)
{
listBoxPicturesInAlbum.SelectedIndex = currentImageIndex;
pictureBox.Image = Image.FromFile((string)listBoxPicturesInAlbum.SelectedValue);
currentImageIndex = (currentImageIndex + 1) % listBoxPicturesInAlbum.Items.Count;
}
I have a programming that is looping x times (10), and using a specified number of threads (2). I'm using a thread array:
Thread[] myThreadArray = new Thread[2];
My loop counter, I believe, starts the first 2 threads just fine, but when it gets to loop 3, which goes back to thread 0 (zero-based), it hangs. The weird thing is, if I throw a MessageBox.Show() in their to check the ThreadState (which shows thread 0 is still running), it will continue on through 9 of the 10 loops. But if no MessageBox.Show() is there, it hangs when starting the 3rd loop.
I'm using .NET 3.5 Framework (I noticed that .NET 4.0 utilizes something called continuations...)
Here's some code examples:
Thread[] threads = new Thread[2];
int threadCounter = 0;
for (int counter = 0; counter < 10; counter++)
{
if (chkUseThreading.Checked)
{
TestRunResult runResult = new TestRunResult(counter + 1);
TestInfo tInfo = new TestInfo(conn, comm, runResult);
if (threads[threadCounter] != null)
{
// If this is here, then it will continue looping....otherwise, it hangs on the 3rd loop
MessageBox.Show(threads[threadCounter].ThreadState.ToString());
while (threads[threadCounter].IsAlive || threads[threadCounter].ThreadState == ThreadState.Running)
Thread.Sleep(1);
threads[threadCounter] = null;
}
// ExecuteTest is a non-static method
threads[threadCounter] = new Thread(new ThreadStart(delegate { ExecuteTest(tInfo); }));
threads[threadCounter].Name = "PerformanceTest" + (counter + 1);
try
{
threads[threadCounter].Start();
if ((threadCounter + 1) == threadCount)
threadCounter = 0;
else
threadCounter++;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Application.DoEvents();
}
}
while (true)
{
int threadsFinished = 0;
for (int counter = 0; counter < threadCount; counter++)
{
if (!threads[counter].IsAlive || threads[counter].ThreadState == ThreadState.Stopped)
threadsFinished++;
}
if (threadsFinished == threadCount)
break;
else
Thread.Sleep(1);
}
Obviously the problem is something about how I'm checking to see if thread #1 or #2 is done. The IsAlive always says true, and the ThreadState always has "running" for threads loops 1 and 10.
Where am I going wrong with this?
Update, here's the ExecuteTask() method:
private void ExecuteTest(object tInfo)
{
TestInfo testInfo = tInfo as TestInfo;
Exception error = null;
DateTime endTime;
TimeSpan duration;
DateTime startTime = DateTime.Now;
try
{
if (testInfo.Connection.State != ConnectionState.Open)
{
testInfo.Connection.ConnectionString = connString;
testInfo.Connection.Open();
}
testInfo.Command.ExecuteScalar();
}
catch (Exception ex)
{
error = ex;
failedCounter++;
//if (chkCancelOnError.Checked)
// break;
}
finally
{
endTime = DateTime.Now;
duration = endTime - startTime;
RunTimes.Add(duration);
testInfo.Result.StartTime = startTime;
testInfo.Result.EndTime = endTime;
testInfo.Result.Duration = duration;
testInfo.Result.Error = error;
TestResults.Add(testInfo.Result);
// This part must be threadsafe...
if (lvResults.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(ExecuteTest);
this.Invoke(d, new object[] { tInfo });
}
else
{
lvResults.Items.Add(testInfo.Result.ConvertToListViewItem());
#region Update Results - This wouldn't work in it's own method in the threaded version
const string msPrefix = "ms";
// ShortestRun
TimeSpan shortest = GetShortestRun(RunTimes);
tbShortestRun.Text = shortest.TotalMilliseconds + msPrefix;
// AverageRun
TimeSpan average = GetAverageRun(RunTimes);
tbAverageRun.Text = average.TotalMilliseconds + msPrefix;
// MeanRun
TimeSpan mean = GetMeanRun(RunTimes);
tbMeanRun.Text = mean.TotalMilliseconds + msPrefix;
// LongestRun
TimeSpan longest = GetLongestRun(RunTimes);
tbLongestRun.Text = longest.TotalMilliseconds + msPrefix;
// ErrorCount
int errorCount = GetErrorCount(TestResults);
tbErrorCount.Text = errorCount.ToString();
#endregion
}
testInfo.Command.Dispose();
Application.DoEvents();
}
}
Can you post a snippet of run ()? Doesn't Thread.currentThread().notifyAll() help? May be each thread is waiting for other thread to do something resulting in a deadlock?