简体   繁体   中英

C# threading, thread not persistant

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 method only runs the once as you have no code to repeat the check for the time after it has completed.

You need to start a timer that activates once a day, and then call GetCurrentDate from that.

Or better still write the code as a simple program that just performs your tasks once and run it as a scheduled task.

Why not doing it this way ?

    private void button1_Click(object sender, EventArgs e)
    {
        Task.Factory.StartNew( () => GetCurrentDate() );
    }

    private void GetCurrentDate()
    {
        if ( DateTime.Today.Day == 7 && myDateToggle == false )
        {
            Task.Factory.StartNew( () => RunMonthBackUp());
        }
        else if ( DateTime.Today.Day == 8 && myDateToggle == true )
        {
            myDateToggle = false;
        }
    }

If something else is wrong with this, it got to do with your strange "toggling"

Your thread will be terminated when GetCurrentDate method 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:

private void button1_Click(object sender, EventArgs e)
{
    Thread myUltiThread = new Thread(GetCurrentDate);
    myUltiThread.IsBackground = true;
    myUltiThread.Start();
}

private void GetCurrentDate()
{
    while(true)
    {
        int = DateTime.Today.Day;

        if(myDate == 7 && myDateToggle == false)
        {
            Task t = new Task(() => RunMonthBackUp());
            t.Start();
        }

        if (myDate == 8 && myDateToggle == true)
        {
            myDateToggle = false;
        }

        Thread.Sleep(1);
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM