I'm trying to develop an application where when the value of a bool variable changes to false it should run a script. Should I use background_work to do this? If yes, how should I use it? And if not, how should I do it?
I'm trying to develop an application where when the value of a bool variable changes to false it should run a script. Should I use background_work to do this? If yes, how should I use it? And if not, how should I do it?
Avoid doing this. Look for other alternatives first.
The reason for this recommendation comes from the simple fact that you should avoid, to the maximum extent possible, actively checking for a condition.
Active checks typically consume more resources (cpu) than passive checks, because you will eventually have to have a periodic check (for example within a loop) of a particular state.
Passive verifications usually use techniques where there is a notification when a given condition is satisfied, this means that there may not even be verification whether the condition is already satisfied.This is not to say that you should always use passive checks. Depending on the passive verifications can also have associated costs, such as the context switch that is not an inexpensive operation. But if you can choose between having the CPU to be used exhaustively to check a certain state over a long period of time or to do a passive check you should choose passive checking.
You can use various passive verification techniques.
public class DadosDoEvento : EventArgs
{
public string Dados { get; set; }
}
public class PublicadorDeEventos{
public event EventHandler<DadosDoEvento> Evento;
protected virtual void OnEvento(OMeuEvento e)
{
Evento?.Invoke(this, e);
}
/*Um método qualquer que chame OnEvento */
}
var publicador = new PublicadorDeEventos();
publicador.Evento += (src, args) => Console.WriteLine(args.Dados);
public class PublicadorDeEventos{
public ManualResetEvent Evento{ get { return new ManualResetEvent(false); } }
/*Um método qualquer que chame Evento.Set();*/
}
var publicador = new PublicadorDeEventos();
publicador.Evento.WaitOne();
INotifyPropertyChanged
: I did not put this approach into my initial response because it is a specific scenario of using events (notification mechanism) and, in my opinion, it is not that simple to use. But that does not mean that it does not have its merit (maybe it's a subject for another question), but here's an example:
public class Modelo : INotifyPropertyChanged
{
private string _dados;
public string Dados {
get {return _dados;}
set {
_dados = value;
OnPropertyChanged(nameof(Dados));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(
[CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
var modelo = new Modelo();
modelo.PropertyChanged += (sender, args) => {
if (args.PropertyName == "Dados")
{
Console.WriteLine("Os dados mudaram vou executar o meu script aqui");
}
}
What should you use? Personally I have preference in choosing the first one. Usually the second alternative provides a more difficult to use API, and it does not solve every case. You need to take care in choosing the appropriate synchronization mechanism and in special cases implement your own synchronization mechanism.