K
K
Konstantin Teploukhov2020-10-01 23:22:03
.NET
Konstantin Teploukhov, 2020-10-01 23:22:03

How to stop tracking an event?

There is this code

void funct()
        {
            webBrowser1.DocumentCompleted += (sender, e) =>
            {
                MessageBox.Show((sender as WebBrowser).Document.Url.ToString());
                MessageBox.Show((sender as WebBrowser).Document.Cookie);

                if ((sender as WebBrowser).Document.Url.ToString().Contains("chl_jschl_tk"))
                {
                    string link = (sender as WebBrowser).Document.Url.ToString();
                    string cookie = CookieReader.GetCookie(link);

                    File.AppendAllText("link.txt", link);

                    File.AppendAllText("cookie.txt", cookie);
                    // Вот тут нужно перестать отслеживать событие так как оно происходит много раз 
                }
            };
            webBrowser1.Navigate("https://www.site.com/list1");
        }

How to stop tracking an event after if(true)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
oleg_ods, 2020-10-02
@blood-moon

Move the handler to a separate method and unsubscribe using the operator -=
https://docs.microsoft.com/en-us/dotnet/csharp/pro...
The last point.

F
FreeBa, 2020-10-02
@FreeBa

Something like this.

using System;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            var browser = new Browser();
            browser.Bruh += (s, e) =>
            {
                Console.WriteLine("YAY");
                browser.ClearEvents();
            };

            browser.Go();
            browser.Go();
            browser.Go();
        }
    }

    class Browser
    {
        public event EventHandler Bruh;

        public void Go()
        {
            Console.WriteLine("GO");
            Bruh?.Invoke(this, null);
        }

        public void ClearEvents()
        {
            Bruh = null;
        }
    }
}

R
Roman, 2020-10-02
@yarosroman

webBrowser1.DocumentCompleted = null

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question