本文整理汇总了C#中System.Timers.Timer.SynchronizingObject属性的典型用法代码示例。如果您正苦于以下问题:C# Timer.SynchronizingObject属性的具体用法?C# Timer.SynchronizingObject怎么用?C# Timer.SynchronizingObject使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Timers.Timer
的用法示例。
在下文中一共展示了Timer.SynchronizingObject属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Form1
//引入命名空间
using System;
using System.IO;
using Timers = System.Timers;
using System.Windows.Forms;
public partial class Form1 : Form
{
Timers.Timer timer = null;
StreamWriter sw = null;
bool hasChanged = false;
bool dialogIsOpen = false;
int elapsedMinutes = 0;
// Cache the text box cache internally without saving it.
String txt = "";
public Form1()
{
InitializeComponent();
this.Text = "Quick Text Editor";
button1.Text = "Save";
textBox1.Multiline = true;
// Configure the SaveFile dialog
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.RestoreDirectory = true;
// Create a timer with a 1-minute interval
timer = new Timers.Timer(60000);
// Define the event handler
timer.Elapsed += this.PromptForSave;
// Synchronize the timer with the text box
timer.SynchronizingObject = this;
// Start the timer
timer.AutoReset = true;
}
private void PromptForSave(Object source, Timers.ElapsedEventArgs e)
{
if (hasChanged & (!dialogIsOpen)) {
elapsedMinutes++;
dialogIsOpen = true;
if (MessageBox.Show(String.Format("{0} minutes have elapsed since the text was saved. Save it now? ",
elapsedMinutes), "Save Text",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
{
button1_Click(this, EventArgs.Empty);
dialogIsOpen = false;
}
}
}
private void button1_Click(Object sender, EventArgs e)
{
if (String.IsNullOrEmpty(saveFileDialog1.FileName)) {
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
sw = new StreamWriter(saveFileDialog1.FileName, false);
}
txt = textBox1.Text;
hasChanged = false;
timer.Stop();
}
private void form1_FormClosing(Object sender, FormClosingEventArgs e)
{
if (sw != null) {
sw.Write(txt);
sw.Close();
}
}
private void textBox1_TextChanged(Object sender, EventArgs e)
{
hasChanged = true;
timer.Start();
}
}