4
I am creating events according to the code below, however it seems to be a somewhat dangerous way, since if there is a cancellation of the event it will accumulate every "shot" of the event, that is, if it occurs three times in the fourth time it will run four times and not only one. How can I rewrite the code so that it cancels the event 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();
}
}
That is, you want to remove all event subscriptions at once?
– Omni
@Omni not necessarily, what I want is to make sure that once the event is signed, the developer doesn’t have to remove the subscription. Suppose a Framework contains a class that has events, the way it is whenever the developer signs an event he needs to remove the subscription, I wanted this removal to be done by the Framework itself and not by the Developer.
– julius_cesars
I was going to try to help but this code seems to have design problems. I mean, in the current form it’s either too broad or it’s unclear.
– Maniero
The correct is to put the event assignment += in a place that will only be called once (PAGE_LOAD?)
– PauloHDSousa
@Paulohdsousa but as it is an event, it can be triggered more than once. The idea is to do something similar to Uielemtent events that events (Gotfocus, Lostfocus, etc.) are "called" and do not require unsubscribe.
– julius_cesars
@julius_cesars the EVENT can be triggered more than once, his registration should not.
– PauloHDSousa
@julius_cesars Managed to solve?
– PauloHDSousa