Morning all,
Using C# .NET 4.0 and Visual Studio 2012 Express.
I have a program that runs some SQL stored procedures, then opens and Access database and auto generates the reports then emails them.
I have it set on a date so it runs as soon as midnight comes.
The following is a code sample.
private void button1_Click(object sender, EventArgs e)
{
Thread myUltiThread = new Thread(GetCurrentDate);
myUltiThread.Start();
}
private void GetCurrentDate()
{
string myDate = "";
myDate = DateTime.Today.Day.ToString();
if(myDate == "7" && myDateToggle == false)
{
Task t = new Task(() => RunMonthBackUp());
t.Start();
}
if (myDate == "8" && myDateToggle == true)
{
myDateToggle = false;
}
}
So as you can see once the button is pressed my program then starts the thread, which uses the getdate method, this method checks if the date is correct.
if it is then it starts a task that runs the whole process.
My problem is that this is not happening, I believe its because the method is only running once. I have a feeling this is due to my lack of understanding working with threads (only ever deal with tasks usually).
Could someone point out to me where I am going wrong and how to get this set up properly?
Many thanks guys
Your thread will be terminated when
GetCurrentDatemethod reaches the end. You should provide a loop in your method and check for the condition periodically. Or you can set a timer (System.Threading.Timer) to check periodically for the condition.Here is the first way:
I also made the thread a background thread, so you don’t need to worry about terminating the thread. The thread will continue to function until the program exits.