I'm creating events according to the code below, however it seems to be a slightly dangerous way, since if there is no event cancellation it will accumulate with every "trigger" of the event, ie if it occurs three times on the fourth time it will run four times and not just one. How can I rewrite the code so that the event cancellation does not depend on a "= -"?
private static MainWindow _mainWindow;
public static void Start(MainWindow mainWindow)
{
_mainWindow = mainWindow;
}
public static void AtivaProgressBar()
{
_mainWindow.ProgressBar1.IsIndeterminate = true;
Suporte.Processo();
Suporte.OnProcessoLongo += Suporte_OnProcessoLongo;
}
static void Suporte_OnProcessoLongo()
{
_mainWindow.StackPanel.Dispatcher.Invoke((Action)(() =>
{
_mainWindow.ProgressBar2.IsIndeterminate = true;
MessageBox.Show("Call me");
Suporte.OnProcessoLongo =- Suporte_OnProcessoLongo;
}));
}
public class Suporte
{
public delegate void ProcessoLongo();
public static event ProcessoLongo OnProcessoLongo;
public static void Processo()
{
Task.Factory.StartNew((() => {
Thread.Sleep(3000);
RaiseProcessoLongo();
}));
}
static void RaiseProcessoLongo()
{
if (OnProcessoLongo != null)
OnProcessoLongo();
}
}