Resolved Consume custom events

rakit

Member
Joined
Jul 24, 2020
Messages
8
Programming Experience
Beginner
Currently, I've been using Winforms - Thread() to trigger custom events, however, I don't know if it's the best way to achieve the result

Let's suppose that I want to check if the text of the element was changed using a webbrowser ... how can I fire the event without using a thread to check if the text was changed?

Page

C#:
<span id='text'>Text</span>
Event
C#:
OnTextElementChanged()

Thread
C#:
while(true)
  // check if element text changed
  if(text != oldText)
      OnTextElementChanged();
 
Cross posted on : Use threads to dispare events

It was also closed for lack of clarity. Can you kindly explain what events you are referring too, as not everyone here will know what you are on about. Some people will assume one thing when it could be something else you are doing entirely different.
 
Its just a random example... for example, using Selenium to check if web page source changes... or using a third party posts API and check if has new posts...

Lets assume another example:

You want to build your own Whatsapp scraper using selenium
You want to create a custom event named OnNewMessage()

How I can fire this event? i need to create a thread to read all the page source, and check if any new message appeared and if yes raise OnNewMessage()
 
Ok but in future, you need to tell us these things. We don't always recognise event/method names etc. So if you are using selenium, you need to tell us that... This is why your question on SO was closed. Clarity is important, so when asking something, be as clear and descriptive as possible.
 
I think I found the answer:

If we don't have built-in events, the way to do is using threads... if you use web-browser, you can use Javascript event hooks/dom mutation
 
Since you never explained what you are doing, there is nothing I can advise you regarding what you're doing is write or wrong. Because you still haven't told us properly or shown us any relevant code that alludes to what it is you are doing.

Vague questions get vague answers.
 
Since you never explained what you are doing, there is nothing I can advise you regarding what you're doing is write or wrong. Because you still haven't told us properly or shown us any relevant code that alludes to what it is you are doing.

Vague questions get vague answers.

I gave just one generic example.

Try to imagine with me, let's assume that you have a TextBox component that there is no built-in event to check if your text has been changed and you need to implement this, how would you do it?

C#:
var thread = new Thread(() =>
            {
                var currentText = textBox1.Text; // this will fire a exception, but let do it in this example

                while (true)
                {
                    if (textBox.Text != currentText)
                    {
                        OnTextChanged();
                        currentText = textBox1.text;

                    }

                    Thread.Sleep(1);
                }
            });

this is the best way to implement?
 
Winforms or web forms, or ASP? This : <span id='text'>Text</span> - has no place here in winforms unless you are scraping a web browser control or something.

I'm trying but you haven't told us what project type this is for. I know its posted in Winforms forums, and I know Skydiver mentioned it might be for Selenium, but I want to be sure...

Never mind, I just seen where you updated your post above.

How I can fire this event?

Here's a generic link Handling and Raising Events start here.
 
Why not share it with the forum, so other users know how you resolved your issue?

I already shared...

lets say that you want to check if selenium pagesource changed:


C#:
var watcher = new Thread(() = >{
    var source = driver.PageSource;

    while (true) {
        if (driver.PageSource != source) {
            // Raise event
            PageSourceChanged();
            source = driver.PageSource;
        }

        Thread.Sleep(10);
    }
});

watcher.Start();
 
Back
Top Bottom