线程 - 有困难。我究竟做错了什么? [英] Threading-Having difficulty. What am I doing wrong?
问题描述
我已经创建了一个线程(或者我正在尝试),它将每隔这么多分钟自动保存。我添加了一个计时器(timer1)。每当我单击按钮启动线程时,程序都不会按照指示自动保存。所以我正在寻求帮助。这是代码:
注意:我在顶部添加了此代码:
使用 System.Threading;
我创建了两个线程(t1和t2)来查看它是否会产生影响。也许我只需要一个线程来引用。
public partial class frmFindAndReplace : 表格
{
主题 t1; // 我添加了两个线程,以便可以在事件中访问它们
Thread t2; // 例如,FormClosing事件
private void timer1_Tick( object sender,EventArgs e)
{
progressBar1.Increment( 1 );
lblUpdate.Visible = false ;
lblSaving.Visible = true ;
}
私有 void AutoSave()
{
timer1.Start();
if (SaveDoc.FileName!= )
{
CreateWordDocument(FilePath,SaveDoc.FileName,pathImage);
MessageBox.Show( 已更新);
return ;
}
else
{
if (SaveDoc .ShowDialog()== DialogResult.OK)
{
CreateWordDocument(FilePath,SaveDoc.FileName,pathImage);
MessageBox.Show( New Save);
return ;
}
}
lblSaving.Visible = false ;
lblUpdate.Visible = true ;
}
单击另存为按钮后,AutoSave方法将开始。
私人 void saveAsToolStripMenuItem_Click( object sender,EventArgs e)
{
t1 = new Thread(自动保存);
}
保存按钮相同。
< pre lang =cs> private void saveToolStripMenuItem_Click( object sender,EventArgs e)
{
t2 = new 线程(AutoSave);
}
还有FormClosing事件的问题。每次运行程序时都会收到错误。
private void frmFindAndReplace_FormClosing( object sender,FormClosingEventArgs e)
{
if (t1.IsAlive)
{t1.Abort(); } // t1.Abort()在退出时关闭t1线程。
if (t2.IsAlive)
{t2.Abort(); } // t2.Abort()在退出时关闭t2线程。
}
嗯。
如果你想每五分钟自动保存一次,我会更简单地做。
设置一个线程并启动它。
private 线程保护程序;
...
saver = new 线程( new ThreadStart(SaveRegularly)) ;
saver.Start();
线程方法保存:
private bool stopAndDie = false ;
private void SaveRegularly()
{
DateTime saveDueAt = DateTime .Now.AddMinutes( 5 );
do
{
Thread.Sleep( 1000 );
if (DateTime.Now > = saveDueAt)
{
// 保存
...
saveDueAt = DateTime.Now .AddMinutes( 5 );
}
} while (!stopAndDie);
}
您还处理表单结束事件,并将stopAndDie设置为true,然后使用Thread.Join等待它退出:
stopAndDie = true ;
saver.Join( 2000 );
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:
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.
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
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.
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
t1 = new Thread(AutoSave);
}
Same for the Save button.
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.
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.
}
Um.
If you want to autosave every five minutes, I'd do it rather more simply.
Set up a Thread and start it.
private Thread saver; ... saver = new Thread(new ThreadStart(SaveRegularly)); saver.Start();
The thread method soes teh save:
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:
stopAndDie = true; saver.Join(2000);
这篇关于线程 - 有困难。我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!