I got a control instance My_Control.
Depending on the current step different sub controls should displayed within that control.
public partial class Teach_All : User_Control
{
/// <summary>
/// this event gets called on an index change
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Current_index_index_changed(object sender, CIndex_Updater.Index_Changed_Eventargs e)
{
//remove old Controll
int old_index = e.new_index - 1;
if (old_index >= 0)
{
if (InvokeRequired)
{
this.BeginInvoke((MethodInvoker)(() => Controls.Remove(teach_steps[old_index])));
}
else
{
this.Controls.Remove(teach_steps[old_index]);
}
}
//do some stuff....
//Display next control
if (InvokeRequired)
{
this.BeginInvoke((MethodInvoker)(() => Controls.Add(teach_steps[e.new_index])));
}
else
{
this.Controls.Add(teach_steps[e.new_index]);
}
}
}
However the Control add causes an invalid thread Access exception. So the Invoke does not work as expected. This Event can be fired form any thread, therefore i believe i Need an Invoke.
Could you give me some hints on how to get this invoke to work?
Related
I have a question in Windows Forms on setting timer when the user is idle or Inactive. I need the timer to set even on any Mouse Events. If the user makes any moment then I need to reset the timer. So this is the requirement. Here goes the code.
using System;
using System.Windows.Forms;
using Timer = System.Windows.Forms.Timer;
namespace FormsTimerSetup.Globals
{
public class SetApplicationTimeOut : Form
{
#region
/// <summary>
/// Private Timer Property
/// </summary>
private static Timer _timer;
/// <summary>
/// Timer Property
/// </summary>
public static Timer Timer
{
get
{
return _timer;
}
set
{
if (_timer != null)
{
_timer.Tick -= Timer_Tick;
}
_timer = value;
if (_timer != null)
{
_timer.Tick += Timer_Tick;
}
}
}
#endregion
#region Events
public event EventHandler UserActivity;
#endregion
#region Constructor
/// <summary>
/// Default/Parameterless SetApplicationTimeOut Constructor
/// </summary>
public SetApplicationTimeOut()
{
KeyPreview = true;
FormClosed += ObservedForm_FormClosed;
MouseMove += ObservedForm_MouseMove;
KeyDown += ObservedForm_KeyDown;
}
#endregion
#region Inherited Methods
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected virtual void OnUserActivity(EventArgs e)
{
// Invoking the UserActivity delegate
UserActivity?.Invoke(this, e);
}
/// <summary>
///
/// </summary>
public void SetTimeOut()
{
// postpone auto-logout by 30 minutes
_timer = new Timer
{
Interval = (30 * 60 * 1000) // Timer set for 30 minutes
};
Application.Idle += Application_Idle;
_timer.Tick += new EventHandler(Timer_Tick);
}
#endregion
#region Private Methods
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ObservedForm_MouseMove(object sender, MouseEventArgs e)
{
OnUserActivity(e);
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ObservedForm_KeyDown(object sender, KeyEventArgs e)
{
OnUserActivity(e);
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ObservedForm_FormClosed(object sender, FormClosedEventArgs e)
{
FormClosed -= ObservedForm_FormClosed;
MouseMove -= ObservedForm_MouseMove;
KeyDown -= ObservedForm_KeyDown;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void Application_Idle(object sender, EventArgs e)
{
_timer.Stop();
_timer.Start();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void Timer_Tick(object sender, EventArgs e)
{
_timer.Stop();
Application.Idle -= Application_Idle;
MessageBox.Show("Application Terminating");
Application.Exit();
}
#endregion
}
}
I have implemented the code but unsure whether it is the right way of doing it.
Any leads would be appreciated.
Q: "I need the timer to set on any Mouse Events...if the user makes any movement then I need to reset the timer...Any leads would be appreciated."
A: I'll try to offer a few leads that I hope you find useful. You say you want MouseMove event to reset the timer but there's a problem: Any time a child Control has focus, it's the child that receives the mouse event and the Main Form doesn't. This is fixable.
The short answer: "Implement the IMessageFilter interface on the main window class so that the Timer is Reset when Mouse Movement is Detected." Adding a MessageFilter can intercept the mouse messages before they are sent to the focused control.
So, now I have to give you all the details so here's the long answer: It starts by adding IMessageFilter interface to our main Form1 like this:
public partial class Form1 : Form, IMessageFilter
The IMessageFilter requires our class to implement just one method:
public bool PreFilterMessage(ref Message m)
{
switch (m.Msg)
{
case WM_MOUSEMOVE:
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// Commit f9367d7c added at OP's request
case WM_KEYDOWN:
// This makes WakeUp persist if user is typing in the textbox.
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TimeOutState = TimeOutState.WakeUp;
break;
}
return false; // Do not suppress downstream message
}
const int // WinOS Messages
WM_KEYDOWN = 0x0100,
WM_MOUSEMOVE = 0x0200;
You can see that now any mouse movement sets our app's TimeOutState back to 'WakeUp'.
enum TimeOutState{ WakeUp, Sleeping, Warning, Exit }
We only need one Timer and every tick interval (here set to 5 seconds) of the timer reduces the state by one. If the mouse doesn't move, it decrements all the way down and finally exits.
Here's a 60-second video of running the app for 60 seconds. You can see changes occur either every 5 seconds or when the mouse moves. If you'd like to run the sample you can clone the latest commit from our GitHub repo.
Here are the rest of the details:
The MessageFilter needs to be connected. Since we need our Form to have its window handle, so we do this here and start the timer:
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
// When our main window is ready for messages, add the MessageFilter
Application.AddMessageFilter(this);
// ...and start the timer for the first time.
TimeOutState = TimeOutState.WakeUp;
}
We need to instantiate the Timer, but only once in the CTor:
public Form1()
{
InitializeComponent();
_wdt = new Timer();
_wdt.Interval = 5000; // Use a very short time-out for this demo
_wdt.Tick += _wdt_Tick;
}
Timer _wdt; // Watch-Dog Timer
The Timer.Tick needs to be handled:
private void _wdt_Tick(object sender, System.EventArgs e)
{
// A tick reduces the TimeOutState by 1
TimeOutState = (TimeOutState)(TimeOutState - 1);
}
Finally, handle the state changes of TimeOutState and show our messages.
TimeOutState TimeOutState
{
get => _timeOutState;
set
{
switch (value)
{
case TimeOutState.WakeUp:
_wdt.Stop();
_wdt.Start();
break;
case TimeOutState.Exit:
_wdt.Stop();
Application.Exit();
return;
}
if (value != _timeOutState) // If state changes, write message
{
Debug.WriteLine(value.ToString(), _timeOutState.ToString());
// In a timer callback that changes the UI, it's
// best to post the action in the message queue.
BeginInvoke((MethodInvoker)delegate
{
textBox1.AppendText(_timeOutState.ToString());
if (TimeOutState == TimeOutState.Warning)
{
textBox1.AppendText(
": Closing in " + (_wdt.Interval / 1000).ToString() + " seconds.");
}
textBox1.AppendText(Environment.NewLine);
textBox1.Select(textBox1.TextLength, 0);
});
}
_timeOutState = value;
}
}
TimeOutState _timeOutState = (TimeOutState)(-1); // Initialize to invalid state
I have used IMessageFilter very reliably in my own apps and I'm confident suggesting it to you as one alternative for answering your post.
I won't go much deeper into your code but I would like to directly approach the issue.
I think a 'roundabout' would work in this case.
For e.g. You can check whenever the mouse moves and compare it with the initial position.
Add this above Initialize Component();
GlobalMouseHandler gmh = new GlobalMouseHandler();
gmh.TheMouseMoved += new MouseMovedEvent(gmh_TheMouseMoved);
Application.AddMessageFilter(gmh);
Then add this:
void gmh_TheMouseMoved()
{
if(XY==false)
{
MouseX = Convert.ToInt32(Cursor.Position.X);
MouseY = Convert.ToInt32(Cursor.Position.Y);
}
else
{
MouseX1 = Convert.ToInt32(Cursor.Position.X);
MouseY1 = Convert.ToInt32(Cursor.Position.Y);
XY = true;
if(MouseX1==MouseX && MouseY1==MouseY)
{
if(yourTimerNameHere.Enabled==false)
{
yourTimerNameHere.Start();
}
}
else
{
yourTimerNameHere.Stop();
yourTimerNameHere.Start();
}
}
}
Add this outside the class of your form:
public delegate void MouseMovedEvent();
public class GlobalMouseHandler : IMessageFilter
{
private const int WM_MOUSEMOVE = 0x0200;
public event MouseMovedEvent TheMouseMoved;
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_MOUSEMOVE)
{
if (TheMouseMoved != null)
{
TheMouseMoved();
}
}
return false;
}
}
Next create 4 ints named MouseX = 0, MouseY = 0, MouseX1 = 0 and MouseY1 = 0 and a bool XY = false;
So actually, whenever the cursor moves, the position gets recorded and gets compared with the next. So you can check if the mouse is idle or no!
Pls note that I haven't tested this code so feel free to revert back for any errors.
Is there a way to change the language of all the controls in the form in runtime?
For example, I have a button in the form and it has a text of "Hello". How can I change that into different language on runtime? dynamically every language that I can set. Is there a way to do it??
I've found an answer, but it seems that its not working it has something to do with cultureinfo. Any suggestions how to do it?
This is my code
public partial class Form1 : BaseLanguageForm
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.TriggerLanguageChange("fr-FR");
}
}
public class LanguageArgs : EventArgs
{
string _languageSymbol;
/// <summary>
/// Gets the language symble.
/// </summary>
public string LanguageSymbol
{
get { return _languageSymbol; }
}
/// <summary>
/// Initializes a new instance of the <see cref="LanguageArgs"/> class.
/// </summary>
/// <param name="symbol">The symbol.</param>
public LanguageArgs(string symbol)
{
this._languageSymbol = symbol;
}
}
public class BaseLanguageForm : Form
{
/// <summary>
/// Triggers the language change event.
/// </summary>
/// <param name="languageSymbol">The language symbol.</param>
protected void TriggerLanguageChange(string languageSymbol)
{
if (Form1.onLanguageChanged != null)
{
LanguageArgs args = new LanguageArgs(languageSymbol);
Form1.onLanguageChanged(this, args);
}
}
/// <summary>
/// This should be triggered whenever the combo box value chages
/// It is protected, just incase you want to do any thing else specific to form instacne type
/// </summary>
protected static event EventHandler<LanguageArgs> onLanguageChanged;
/// <summary>
/// This will be called from your form's constuctor
/// (you don't need to do anything, the base class constuctor is called automatically)
/// </summary>
public BaseLanguageForm()
{
//registering to the event
BaseLanguageForm.onLanguageChanged += new EventHandler<LanguageArgs>(BaseLanguageForm_onLanguageChanged);
}
/// <summary>
/// The function that was regidtered to the event
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
void BaseLanguageForm_onLanguageChanged(object sender, LanguageArgs e)
{
string lang = e.LanguageSymbol;
foreach (Control c in this.Controls)
{
ComponentResourceManager crm = new ComponentResourceManager(typeof(Form1));
crm.ApplyResources(c, c.Name, new CultureInfo(lang));
}
}
}
There are two kinds of things you must translate: The controls that are visible on the form and the strings you you need during runtime to say, change a button's Text or a MessageBox's caption etc..
You translate the controls you have on the form in the designer:
First do all the layout stuff in the Laguage = Standard.
Then change the Languge in the properties tab to another language.
Now translate all Texts in all controls.
You also may need to change the layout a little to allow for longer texts; this is fine!
Look at the project explorer: For each form and for each language there is now a new FormN.lang.resx file, e.g:
Form1.resx
Form1.en.resx
Form1.de.resx
Now when you call a changeLanguage function, maybe like this one:
private void ChangeLanguage(Control ctl, string lang)
{
resources.ApplyResources(ctl, ctl.Name, new CultureInfo(lang));
foreach (Control c in ctl.Controls) ChangeLanguage(c, lang);
}
maybe like this:
private void cb_language_Click(object sender, EventArgs e)
{
if (cb_language.Text == "DE")
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
resources = new ComponentResourceManager(typeof(Form1));
ChangeLanguage(this, "de-DE");
cb_language.Text = "EN";
}
else
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
resources = new ComponentResourceManager(typeof(Form1));
ChangeLanguage(this, "en-US");
cb_language.Text = "DE";
}
}
..all visible controls will change their Texts.
The second thing to do is to create strings resource files, best one for standard and one for each language (repeating the standard values!): Add new element - resource file with names like these:
strings.resx
strings.de.resx
strings.en.resx
Then you add one name-value pair for each string you need in code, e.g:
msgCap_OW File Exists
msgTxt_OW Overwrite (Yes)
Use next free Index? (No)
Cancel?
cb_autoScrapeStart Start Timed Screenshots
Note: By shift-enter in a value field you can enter multiline values..
And finally you must change your code to use the strings resources, e.g. like this:
MessageBox.Show(strings.msgTxt_OW, strings.msgCap_OW, ..);
or:
cb_autoScrape.Text = (scrapeTimer.Enabled ?
strings.cb_autoScrapeStop : strings.cb_autoScrapeStart);
I'm trying to build a button that displays a loading circle and loads some data in a C# application.
I have a boolean value called Loading that tracks the state of the current data that is being loaded.
I have a delegate called LoadHandler that returns a boolean value based on whether the loading is complete or not, I also have an event that can be subscribed to in a separate class that takes the LoadHandler signature and handles the actual loading.
I have a private Load() function that gets called when an event handler for input is called in the base class, this is called on a new Thread.
Here's the code for the LoadEntry class (Keep in mind this is just a button that displays a loading circle when hit, and delegates loading to a separate class)
public class LoadingEntry : ButtonEntry
{
private readonly object objLock = new object();
/// <summary>
/// The loading circle texutre
/// </summary>
Texture2D loadingReticle;
/// <summary>
/// Tracks the rotation of the loading reticle.
/// </summary>
float loadingRotation;
/// <summary>
/// Tells whether we are loading or not
/// </summary>
public bool Loading { get; private set; }
public LoadingEntry(string buttonText)
: base(buttonText)
{
}
/// <summary>
/// Handler that will return true when loading information is complete
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <returns>True if loading is complete</returns>
public delegate bool LoadHandler(object sender, EventArgs e);
/// <summary>
/// The LoadHandler event that should be subscribed to in a menu class
/// </summary>
public event LoadHandler onLoad;
private void Load()
{
if (onLoad != null)
{
lock (objLock)
{
Loading = true;
while (true)
{
Loading = onLoad(this, EventArgs.Empty);
if (!Loading)
break;
}
}
}
}
protected internal override void OnSelectEntry(PlayerIndex playerIndex)
{
Thread t = new Thread(new ThreadStart(Load));
t.Start();
base.OnSelectEntry(playerIndex);
}
public override void Draw(MenuScreen screen, bool isSelected, GameTime gameTime, bool fade)
{
if (Loading)
{
loadingReticle = screen.ScreenManager.LoadingReticle;
Vector2 origin = new Vector2(loadingReticle.Width / 2, loadingReticle.Height / 2);
loadingRotation += 0.01f;
screen.ScreenManager.SpriteBatch.Draw(loadingReticle, position, null, Color.White, loadingRotation, origin, 1f, SpriteEffects.None, 1f);
}
else base.Draw(screen, isSelected, gameTime, fade);
}
The base class ButtonEntry shouldn't be important.
And here's what is subscribed to onLoad in another class: (just for practice, this is where actual loading code would go)
int sampleTics = 0;
bool sampleLoad_onLoad(object sender, System.EventArgs e)
{
sampleTics++;
if (sampleTics < 1000)
return true;
return false;
}
My question is am I using mulit-threading properly? The code doesn't even display the circle, I haven't done much testing yet because i'm going to bed. Is the lock necessary in this situation? Is there a better way to do this that i'm not seeing?
bool is thread safe according to the documentation. See this post for a similar question. As suggested by the post you also need to declare your boolean variable volatile so that its value is not cached. You can't declare properties volatile so you'll need to add a backing property and declare that volatile.
It turns out that I needed to call Thread.Sleep(1); in order to see the results. Here's the complete code:
private void Load()
{
if (onLoad != null)
{
loading = true;
while (true)
{
loading = onLoad(this, EventArgs.Empty);
if (!loading)
break;
Thread.Sleep(1);
}
}
}
I want to know what how to avoid or stop FileSystemWatcher raise event twice in C#? I have a solution that will detect everytime if there is newly created xml file from a folder. I test my application using creating xml file using notepad but from the listbox it displays twice.
How can I fix this issue?
Here is my code:
private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
try
{
fileSystemWatcher1.EnableRaisingEvents = false;
listBox1.Items.Add(e.FullPath);
}
finally
{
fileSystemWatcher1.EnableRaisingEvents = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult dialogSelectFolder = folderBrowserDialog1.ShowDialog();
if (dialogSelectFolder.ToString() == "OK")
{
textBox1.Text = folderBrowserDialog1.SelectedPath;
button2.Enabled = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = true;
fileSystemWatcher1.EnableRaisingEvents = true;
fileSystemWatcher1.Path = textBox1.Text;
fileSystemWatcher1.Filter = "*.xml";
}
private void button3_Click(object sender, EventArgs e)
{
button1.Enabled = true;
button3.Enabled = false;
textBox1.Text = "";
fileSystemWatcher1.EnableRaisingEvents = false;
}
}
I have stumbled upon this problem myself twice and i created a class that helps you get only one event at a time. You might also get false events when the file is not in read mode (such as when you copy a file).
You have to create a queue and store all events there and if a time interval passes then raise the appropriate event.
Unfortunately this is not a simple function thus i will include complete code.
using System;
using System.IO;
using System.Timers;
using System.Collections;
using System.ComponentModel;
namespace menelabs.core
{
/// <summary>
/// This class wraps FileSystemEventArgs and RenamedEventArgs objects and detection of duplicate events.
/// </summary>
internal class DelayedEvent
{
private readonly FileSystemEventArgs _args;
/// <summary>
/// Only delayed events that are unique will be fired.
/// </summary>
private bool _delayed;
public DelayedEvent(FileSystemEventArgs args)
{
_delayed = false;
_args = args;
}
public FileSystemEventArgs Args
{
get
{
return _args;
}
}
public bool Delayed
{
get
{
return _delayed;
}
set
{
_delayed = value;
}
}
public virtual bool IsDuplicate(object obj)
{
DelayedEvent delayedEvent = obj as DelayedEvent;
if (delayedEvent == null)
return false; // this is not null so they are different
FileSystemEventArgs eO1 = _args;
RenamedEventArgs reO1 = _args as RenamedEventArgs;
FileSystemEventArgs eO2 = delayedEvent._args;
RenamedEventArgs reO2 = delayedEvent._args as RenamedEventArgs;
// The events are equal only if they are of the same type (reO1 and reO2
// are both null or NOT NULL) and have all properties equal.
// We also eliminate Changed events that follow recent Created events
// because many apps create new files by creating an empty file and then
// they update the file with the file content.
return ((eO1 != null && eO2 != null && eO1.ChangeType == eO2.ChangeType
&& eO1.FullPath == eO2.FullPath && eO1.Name == eO2.Name) &&
((reO1 == null & reO2 == null) || (reO1 != null && reO2 != null &&
reO1.OldFullPath == reO2.OldFullPath && reO1.OldName == reO2.OldName))) ||
(eO1 != null && eO2 != null && eO1.ChangeType == WatcherChangeTypes.Created
&& eO2.ChangeType == WatcherChangeTypes.Changed
&& eO1.FullPath == eO2.FullPath && eO1.Name == eO2.Name);
}
}
/// <summary>
/// This class wraps a FileSystemWatcher object. The class is not derived
/// from FileSystemWatcher because most of the FileSystemWatcher methods
/// are not virtual. The class was designed to resemble FileSystemWatcher class
/// as much as possible so that you can use FileSystemSafeWatcher instead
/// of FileSystemWatcher objects.
/// FileSystemSafeWatcher will capture all events from the FileSystemWatcher object.
/// The captured events will be delayed by at least ConsolidationInterval milliseconds in order
/// to be able to eliminate duplicate events. When duplicate events are found, the last event
/// is droped and the first event is fired (the reverse is not recomended because it could
/// cause some events not be fired at all since the last event will become the first event and
/// it won't fire a if a new similar event arrives imediately afterwards).
/// </summary>
internal class FileSystemSafeWatcher
{
private readonly FileSystemWatcher _fileSystemWatcher;
/// <summary>
/// Lock order is _enterThread, _events.SyncRoot
/// </summary>
private readonly object _enterThread = new object(); // Only one timer event is processed at any given moment
private ArrayList _events;
private Timer _serverTimer;
private int _consolidationInterval = 1000; // milliseconds
#region Delegate to FileSystemWatcher
public FileSystemSafeWatcher()
{
_fileSystemWatcher = new FileSystemWatcher();
Initialize();
}
public FileSystemSafeWatcher(string path)
{
_fileSystemWatcher = new FileSystemWatcher(path);
Initialize();
}
public FileSystemSafeWatcher(string path, string filter)
{
_fileSystemWatcher = new FileSystemWatcher(path, filter);
Initialize();
}
/// <summary>
/// Gets or sets a value indicating whether the component is enabled.
/// </summary>
/// <value>true if the component is enabled; otherwise, false. The default is false. If you are using the component on a designer in Visual Studio 2005, the default is true.</value>
public bool EnableRaisingEvents
{
get
{
return _fileSystemWatcher.EnableRaisingEvents;
}
set
{
_fileSystemWatcher.EnableRaisingEvents = value;
if (value)
{
_serverTimer.Start();
}
else
{
_serverTimer.Stop();
_events.Clear();
}
}
}
/// <summary>
/// Gets or sets the filter string, used to determine what files are monitored in a directory.
/// </summary>
/// <value>The filter string. The default is "*.*" (Watches all files.)</value>
public string Filter
{
get
{
return _fileSystemWatcher.Filter;
}
set
{
_fileSystemWatcher.Filter = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether subdirectories within the specified path should be monitored.
/// </summary>
/// <value>true if you want to monitor subdirectories; otherwise, false. The default is false.</value>
public bool IncludeSubdirectories
{
get
{
return _fileSystemWatcher.IncludeSubdirectories;
}
set
{
_fileSystemWatcher.IncludeSubdirectories = value;
}
}
/// <summary>
/// Gets or sets the size of the internal buffer.
/// </summary>
/// <value>The internal buffer size. The default is 8192 (8K).</value>
public int InternalBufferSize
{
get
{
return _fileSystemWatcher.InternalBufferSize;
}
set
{
_fileSystemWatcher.InternalBufferSize = value;
}
}
/// <summary>
/// Gets or sets the type of changes to watch for.
/// </summary>
/// <value>One of the System.IO.NotifyFilters values. The default is the bitwise OR combination of LastWrite, FileName, and DirectoryName.</value>
/// <exception cref="System.ArgumentException">The value is not a valid bitwise OR combination of the System.IO.NotifyFilters values.</exception>
public NotifyFilters NotifyFilter
{
get
{
return _fileSystemWatcher.NotifyFilter;
}
set
{
_fileSystemWatcher.NotifyFilter = value;
}
}
/// <summary>
/// Gets or sets the path of the directory to watch.
/// </summary>
/// <value>The path to monitor. The default is an empty string ("").</value>
/// <exception cref="System.ArgumentException">The specified path contains wildcard characters.-or- The specified path contains invalid path characters.</exception>
public string Path
{
get
{
return _fileSystemWatcher.Path;
}
set
{
_fileSystemWatcher.Path = value;
}
}
/// <summary>
/// Gets or sets the object used to marshal the event handler calls issued as a result of a directory change.
/// </summary>
/// <value>The System.ComponentModel.ISynchronizeInvoke that represents the object used to marshal the event handler calls issued as a result of a directory change. The default is null.</value>
public ISynchronizeInvoke SynchronizingObject
{
get
{
return _fileSystemWatcher.SynchronizingObject;
}
set
{
_fileSystemWatcher.SynchronizingObject = value;
}
}
/// <summary>
/// Occurs when a file or directory in the specified System.IO.FileSystemWatcher.Path is changed.
/// </summary>
public event FileSystemEventHandler Changed;
/// <summary>
/// Occurs when a file or directory in the specified System.IO.FileSystemWatcher.Path is created.
/// </summary>
public event FileSystemEventHandler Created;
/// <summary>
/// Occurs when a file or directory in the specified System.IO.FileSystemWatcher.Path is deleted.
/// </summary>
public event FileSystemEventHandler Deleted;
/// <summary>
/// Occurs when the internal buffer overflows.
/// </summary>
public event ErrorEventHandler Error;
/// <summary>
/// Occurs when a file or directory in the specified System.IO.FileSystemWatcher.Path is renamed.
/// </summary>
public event RenamedEventHandler Renamed;
/// <summary>
/// Begins the initialization of a System.IO.FileSystemWatcher used on a form or used by another component. The initialization occurs at run time.
/// </summary>
public void BeginInit()
{
_fileSystemWatcher.BeginInit();
}
/// <summary>
/// Releases the unmanaged resources used by the System.IO.FileSystemWatcher and optionally releases the managed resources.
/// </summary>
public void Dispose()
{
Uninitialize();
}
/// <summary>
/// Ends the initialization of a System.IO.FileSystemWatcher used on a form or used by another component. The initialization occurs at run time.
/// </summary>
public void EndInit()
{
_fileSystemWatcher.EndInit();
}
/// <summary>
/// Raises the System.IO.FileSystemWatcher.Changed event.
/// </summary>
/// <param name="e">A System.IO.FileSystemEventArgs that contains the event data.</param>
protected void OnChanged(FileSystemEventArgs e)
{
if (Changed != null)
Changed(this, e);
}
/// <summary>
/// Raises the System.IO.FileSystemWatcher.Created event.
/// </summary>
/// <param name="e">A System.IO.FileSystemEventArgs that contains the event data.</param>
protected void OnCreated(FileSystemEventArgs e)
{
if (Created != null)
Created(this, e);
}
/// <summary>
/// Raises the System.IO.FileSystemWatcher.Deleted event.
/// </summary>
/// <param name="e">A System.IO.FileSystemEventArgs that contains the event data.</param>
protected void OnDeleted(FileSystemEventArgs e)
{
if (Deleted != null)
Deleted(this, e);
}
/// <summary>
/// Raises the System.IO.FileSystemWatcher.Error event.
/// </summary>
/// <param name="e">An System.IO.ErrorEventArgs that contains the event data.</param>
protected void OnError(ErrorEventArgs e)
{
if (Error != null)
Error(this, e);
}
/// <summary>
/// Raises the System.IO.FileSystemWatcher.Renamed event.
/// </summary>
/// <param name="e">A System.IO.RenamedEventArgs that contains the event data.</param>
protected void OnRenamed(RenamedEventArgs e)
{
if (Renamed != null)
Renamed(this, e);
}
/// <summary>
/// A synchronous method that returns a structure that contains specific information on the change that occurred, given the type of change you want to monitor.
/// </summary>
/// <param name="changeType">The System.IO.WatcherChangeTypes to watch for.</param>
/// <returns>A System.IO.WaitForChangedResult that contains specific information on the change that occurred.</returns>
public WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType)
{
//TODO
throw new NotImplementedException();
}
/// <summary>
/// A synchronous method that returns a structure that contains specific information on the change that occurred, given the type of change you want to monitor
/// and the time (in milliseconds) to wait before timing out.
/// </summary>
/// <param name="changeType">The System.IO.WatcherChangeTypes to watch for.</param>
/// <param name="timeout">The time (in milliseconds) to wait before timing out.</param>
/// <returns>A System.IO.WaitForChangedResult that contains specific information on the change that occurred.</returns>
public WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType, int timeout)
{
//TODO
throw new NotImplementedException();
}
#endregion
#region Implementation
private void Initialize()
{
_events = ArrayList.Synchronized(new ArrayList(32));
_fileSystemWatcher.Changed += new FileSystemEventHandler(this.FileSystemEventHandler);
_fileSystemWatcher.Created += new FileSystemEventHandler(this.FileSystemEventHandler);
_fileSystemWatcher.Deleted += new FileSystemEventHandler(this.FileSystemEventHandler);
_fileSystemWatcher.Error += new ErrorEventHandler(this.ErrorEventHandler);
_fileSystemWatcher.Renamed += new RenamedEventHandler(this.RenamedEventHandler);
_serverTimer = new Timer(_consolidationInterval);
_serverTimer.Elapsed += new ElapsedEventHandler(this.ElapsedEventHandler);
_serverTimer.AutoReset = true;
_serverTimer.Enabled = _fileSystemWatcher.EnableRaisingEvents;
}
private void Uninitialize()
{
if (_fileSystemWatcher != null)
_fileSystemWatcher.Dispose();
if (_serverTimer != null)
_serverTimer.Dispose();
}
private void FileSystemEventHandler(object sender, FileSystemEventArgs e)
{
_events.Add(new DelayedEvent(e));
}
private void ErrorEventHandler(object sender, ErrorEventArgs e)
{
OnError(e);
}
private void RenamedEventHandler(object sender, RenamedEventArgs e)
{
_events.Add(new DelayedEvent(e));
}
private void ElapsedEventHandler(Object sender, ElapsedEventArgs e)
{
// We don't fire the events inside the lock. We will queue them here until
// the code exits the locks.
Queue eventsToBeFired = null;
if (System.Threading.Monitor.TryEnter(_enterThread))
{
// Only one thread at a time is processing the events
try
{
eventsToBeFired = new Queue(32);
// Lock the collection while processing the events
lock (_events.SyncRoot)
{
DelayedEvent current;
for (int i = 0; i < _events.Count; i++)
{
current = _events[i] as DelayedEvent;
if (current.Delayed)
{
// This event has been delayed already so we can fire it
// We just need to remove any duplicates
for (int j = i + 1; j < _events.Count; j++)
{
if (current.IsDuplicate(_events[j]))
{
// Removing later duplicates
_events.RemoveAt(j);
j--; // Don't skip next event
}
}
bool raiseEvent = true;
if (current.Args.ChangeType == WatcherChangeTypes.Created || current.Args.ChangeType == WatcherChangeTypes.Changed)
{
//check if the file has been completely copied (can be opened for read)
FileStream stream = null;
try
{
stream = File.Open(current.Args.FullPath, FileMode.Open, FileAccess.Read, FileShare.None);
// If this succeeds, the file is finished
}
catch (IOException)
{
raiseEvent = false;
}
finally
{
if (stream != null) stream.Close();
}
}
if (raiseEvent)
{
// Add the event to the list of events to be fired
eventsToBeFired.Enqueue(current);
// Remove it from the current list
_events.RemoveAt(i);
i--; // Don't skip next event
}
}
else
{
// This event was not delayed yet, so we will delay processing
// this event for at least one timer interval
current.Delayed = true;
}
}
}
}
finally
{
System.Threading.Monitor.Exit(_enterThread);
}
}
// else - this timer event was skipped, processing will happen during the next timer event
// Now fire all the events if any events are in eventsToBeFired
RaiseEvents(eventsToBeFired);
}
public int ConsolidationInterval
{
get
{
return _consolidationInterval;
}
set
{
_consolidationInterval = value;
_serverTimer.Interval = value;
}
}
protected void RaiseEvents(Queue deQueue)
{
if ((deQueue != null) && (deQueue.Count > 0))
{
DelayedEvent de;
while (deQueue.Count > 0)
{
de = deQueue.Dequeue() as DelayedEvent;
switch (de.Args.ChangeType)
{
case WatcherChangeTypes.Changed:
OnChanged(de.Args);
break;
case WatcherChangeTypes.Created:
OnCreated(de.Args);
break;
case WatcherChangeTypes.Deleted:
OnDeleted(de.Args);
break;
case WatcherChangeTypes.Renamed:
OnRenamed(de.Args as RenamedEventArgs);
break;
}
}
}
}
#endregion
}
}
You may find the code at:
https://github.com/melenaos/FileSystemSafeWatcher/blob/master/FileSystemSafeWatcher.cs
I have a numericUpDown control (Windows Forms).
I want to listen to the ValueChanged event.
I set it in the properties and it works.
But:
I want that i can "scroll" up or down. (If I make this longer it will be faster)
When I'm done with the "scroll", I want that the event xyz is fired now and not during the scrolling.
How can I do that?
Try using the mouseup event. It fires when you take your finger off of the left mouse button, so in theory it should solve your issue.
[Edited by James]
Try this control on your form.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Example.CustomControl
{
/// <summary>
/// Provides an extra event for the numericUpDown control that fires after the value stops scrolling.
/// </summary>
public class NumericDelayedChange : NumericUpDown
{
/// <summary>
/// Flag that the value has actually changed.
/// </summary>
/// <devdoc>
/// Just in case the control was clicked somewhere other than the up/down buttons.
/// </devdoc>
private bool valueHasChanged = false;
/// <summary>
/// Fires when the value has stopped being scrolled.
/// </summary>
public event EventHandler OnAfterScollValueChanged;
/// <summary>
/// Captures that value as having changed.
/// </summary>
/// <param name="e"></param>
protected override void OnValueChanged(EventArgs e)
{
valueHasChanged = true;
base.OnValueChanged(e);
}
/// <summary>
/// Captures the mouse up event to identify scrolling stopped when used in combination with the value changed flag.
/// </summary>
/// <param name="mevent"></param>
protected override void OnMouseUp(MouseEventArgs mevent)
{
base.OnMouseUp(mevent);
if (mevent.Button == System.Windows.Forms.MouseButtons.Left)
{
PerformOnAfterScollValueChanged();
}
}
/// <summary>
/// Captures the key up/down events to identify scrolling stopped when used in combination with the value changed flag.
/// </summary>
/// <param name="mevent"></param>
protected override void OnKeyUp(KeyEventArgs e)
{
if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
{
PerformOnAfterScollValueChanged();
}
base.OnKeyUp(e);
}
/// <summary>
/// Checks the value changed flag and fires the OnAfterScollValueChanged event.
/// </summary>
private void PerformOnAfterScollValueChanged()
{
if (valueHasChanged)
{
valueHasChanged = false;
if (OnAfterScollValueChanged != null) { OnAfterScollValueChanged(this, new EventArgs()); }
}
}
}
}
You need to define what is I'm done with the scroll. This could be removing the mouse from the scroll button, or a certain time passed since the last click. In any case, I don't think you can cause the event not to run, but you can do some checks in the event handler. For example:
private DateTime? lastClickTime;
public void MyUpDown_ValueChanged(object sender, EventArgs e)
{
if (lastClickTime != null && DateTime.Now.Subtract(lastClickTime.Value).Seconds > someInterval)
{
// Do the work
}
lastClickTime = DateTime.Now
}
But, as I said, you need first to define what is I'm done. This code is not perfect.
Here's a direct way of approaching the problem:
Just check if a mouse button is being pressed with Control.MouseButtons, like this:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
//don't update until done scrolling
if (Control.MouseButtons == MouseButtons.None)
{
// Do the work
}
}
Here's how I solved this problem:
A task checks a "valueIsChanging" flag. This flag is set by the ValueChanged event. After this flag stops being set to True by the ValueChanged event, the while loop completes and your code is executed.
Task waitForValueSettleTask;
volatile bool valueIsChanging; // marked volaltile since it is access by multiple threads (the GUI thread, and the task)
private void numericUpDown_ValueChanged(object sender, EventArgs e)
{
valueIsChanging = true;
if (waitForValueSettleTask == null || waitForValueSettleTask != null &&
(waitForValueSettleTask.IsCanceled || waitForValueSettleTask.IsCompleted || waitForValueSettleTask.IsFaulted))
{
waitForValueSettleTask = Task.Run(() =>
{
while (valueIsChanging)
{
valueIsChanging = false;
Thread.Sleep(1000); // Set this to whatever settling time you'd like
}
// Your code goes here
});
}
}