zap92
zap92

Reputation: 269

How to stop a function while some other function is executed?

I have 2 functions

   private void Ellipse_ManipulationCompleted_1(object sender, ManipulationCompletedEventArgs e)
    {
        Counter++;
        if (Counter == 1)
        {
            Startbtn_Click(sender, e);

        }
    }


    private async void Startbtn_Click(object sender, RoutedEventArgs e)
    {
            Startbtn.IsEnabled = false;
            Storyboard myStoryboard;
            Random rnd = new Random();
            int Story = rnd.Next(1, 3);

            if (Story == 1)
            {
                myStoryboard = (Storyboard)this.Resources["Storyboard1"];
                myStoryboard.Begin();

            }


            else if (Story == 2)
            {

                myStoryboard = (Storyboard)this.Resources["Storyboard2"];
                myStoryboard.Begin();

            }


            await Task.Delay(12000);

            Startbtn.IsEnabled = true;
            Counter = 0;

    }

I want that when Startbtn_Click is running Ellipse_ManipulationCompleted_1 gets disabled, or it should not been called.

I am new to windows app development so any help would work. thanks in advance

Upvotes: 2

Views: 123

Answers (2)

Francis
Francis

Reputation: 94

It would be a good practice to have a separate button that will disable method Ellipse_ManipulationCompleted_1 since it is very easy to get confused when using your program and the code for disabling and enabling this method is all written in one method named Startbtn_Click so I suggest you separate them to make the program's design more precise.

Next thing you can do is to execute a NotSupportedException, whic you are going to mark as Obsolete. After having done this you can use EditorBrowsable. Here's how:

    [EditorBrowsable( EditorBrowsableState.Never )]
    [Obsolete( "...", false )]

    private void Separatebtn_Click(object sender, ManipulationCompletedEventArgs e)
    {
    throw new NotSupportedException( "..." );
    }       

Be warned that it's sometimes dangerous to throw exceptions as it creates some other bugs, but this option won't hurt much if you're just trying to disable a function. By the way, this will really not remove the method as it will just replace it, it's now up to you to choose which method will receive this. Hope this was helpful!

Upvotes: 1

Romasz
Romasz

Reputation: 29792

Stopping method is totaly different subject (read more about asynchronous programming).

In your case your ManipulationCompleted is an Event and you can always unsubscribe it if you need to:

private async void Startbtn_Click(object sender, RoutedEventArgs e)
{
   // unsubscribe to the Event if you don't want to fire it
      Ellipse.ManipulationCompleted -= Ellipse_ManipulationCompleted_1;
      Startbtn.IsEnabled = false;
   // your further actions
   // ...
   // subscribe again if you need
      Ellipse.ManipulationCompleted += Ellipse_ManipulationCompleted_1;

There are other solutions, but this should be simple and work.

Upvotes: 2

Related Questions