Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've created a thread (or I'm trying to) which will autosave every so many minutes. I've added a timer (timer1). Whenever I click the button to start the thread, the program doesn't autosave as instructed. So I'm reaching out for help. Here's the code:

Note: I've added this code at the top:
C#
using System.Threading;

I created two threads (t1 & t2) to see if it would make a difference. Maybe I only need one thread to refer to.
CSS
public partial class frmFindAndReplace : Form
    {
        Thread t1;  //I added the two threads so they can be accessed in events
        Thread t2;  //eg, the FormClosing event


C#
private void timer1_Tick(object sender, EventArgs e)
{
    progressBar1.Increment(1);
    lblUpdate.Visible = false;
    lblSaving.Visible = true;
}
private void AutoSave()
{
    timer1.Start();

    if (SaveDoc.FileName != "")
    {
        CreateWordDocument(FilePath, SaveDoc.FileName, pathImage);
        MessageBox.Show("Updated");
        return;
    }
    else
    {
        if (SaveDoc.ShowDialog() == DialogResult.OK)
        {
            CreateWordDocument(FilePath, SaveDoc.FileName, pathImage);
            MessageBox.Show("New Save");
            return;
        }
    }
    lblSaving.Visible = false;
    lblUpdate.Visible = true;
}


Once the Save As button has been clicked, the AutoSave method will begin.
C#
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            t1 = new Thread(AutoSave);           
        }


Same for the Save button.
C#
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            t2 = new Thread(AutoSave);            
        }


Also having a problem with the FormClosing event. I receive an error everytime I run the program.
C#
private void frmFindAndReplace_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (t1.IsAlive)  
            { t1.Abort(); } //t1.Abort() closes the t1 thread upon exit.

            if (t2.IsAlive)
            { t2.Abort(); } //t2.Abort() closes the t2 thread upon exit.
        }
Posted
Updated 26-Sep-15 2:56am
v5

1 solution

Um.
If you want to autosave every five minutes, I'd do it rather more simply.
Set up a Thread and start it.
C#
            private Thread saver;
...
            saver = new Thread(new ThreadStart(SaveRegularly));
            saver.Start();

The thread method soes teh save:
C#
private bool stopAndDie = false;
private void SaveRegularly()
    {
    DateTime saveDueAt = DateTime.Now.AddMinutes(5);
    do
        {
        Thread.Sleep(1000);
        if (DateTime.Now >= saveDueAt)
            {
            // Do your save
            ...
            saveDueAt = DateTime.Now.AddMinutes(5);
            }
        } while (!stopAndDie);
    }

You also handle the form closing event, and set stopAndDie to true, then use Thread.Join to wait for it to exit:
C#
stopAndDie = true;
saver.Join(2000);
 
Share this answer
 
Comments
Member 11909035 26-Sep-15 13:27pm    
Thanks for your post. I tried the code you provided, and when I run the program, it stops and highlights this line of code: if (SaveDoc.ShowDialog() == DialogResult.OK)
This hasn't happened before I added the thread. Any ideas? I'm thinking the SFD is involved with the original UI thread and there is a clash of some sort.
BillWoodruff 26-Sep-15 17:33pm    
Think about the standard behavior of 'ShowDialog.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900