13 Replies - 1091 Views - Last Post: 09 December 2016 - 07:14 PM Rate Topic: -----

#1 danis_marf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 03-November 16

How to update field variable to wait for value update from event

Posted 09 December 2016 - 12:06 PM

I have two events of System.Speech.Synthesis;, with Event handlers for speech state events, if in progress and if completed:

synth.SpeakProgress += new EventHandler<SpeakProgressEventArgs>(sSynth_SpeakProgress);
synth.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(sSynth_SpeakCompleted);


My question is, if I have field variable x,

    public Form1()
    {
        InitializeComponent();            
    }

    int x = 1;


I want to update its value from this events and pass it to condition, which must be looped to wait until it become 0 to allow and implement its following function.

  while (x > 0) 
    {       
    }
    Function();


or other way, which is less acceptable for my current case, but no matter, the question is not about loop, but about update of given value:

   do
    {
    } while (x == 0);


It does not updates field variable properly, immediately becomes 0. At the same time, displaying of process from events with textBox2.Text works properly:

 public void sSynth_SpeakProgress(object sender, SpeakProgressEventArgs e) 
    {
        x = (x = 1); 

        textBox1.Text = ("Current word being spoken: " + e.Text);
    }

    public void sSynth_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
    {
        x = (x = 0); 

        textBox1.Text = ("Speak operation completed");           
    }


So can't get, why I can't update field variable x same way. Maybe I can do it somehow directly with event handler for condition, but not sure how.

Other attempt:

 public void sSynth_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
 {                 
   Function();              
 }


I have Process.Start(); of console prog.exe application running inside function();. And if I call it in separate condition out of event, it works proper except of first problem about waiting.

But if I call it directly in event, of course it waits for event condition, but then as prog.exe completes its work, and when it calls for prog.exe again, to be precise, on the third call, I got opening of two consoles, then more and more with each new call.

There is nothing that could multiply consoles opening of separate prog.exe, but it happens that way, and I can't get why

Is This A Good Question/Topic? 0
  • +

Replies To: How to update field variable to wait for value update from event

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: How to update field variable to wait for value update from event

Posted 09 December 2016 - 02:26 PM

You are programming in an event driven environment. Don't try to fight it. Use it to your advantage whenever possible.

Your approach of calling Function() in the event handler is the correct way to do things.

I suspect that you may have registered the same speech completion event handler multiple times, or that you simply may have invoked speech more than once.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,569
  • Joined: 12-December 12

Re: How to update field variable to wait for value update from event

Posted 09 December 2016 - 02:49 PM

What are you attempting with this x = (x = 1);? This sets x to 1, and to 0 in the following code. If you wanted to increment the value you would use x = x + 1 or x += 1, or even just x++.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: How to update field variable to wait for value update from event

Posted 09 December 2016 - 03:22 PM

Actually it sets x to 1.

        static void Main(string[] args)
        {
            int x = 3;
            x = (x = 1);
            Console.WriteLine(x);
        }



Repl.it
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,569
  • Joined: 12-December 12

Re: How to update field variable to wait for value update from event

Posted 09 December 2016 - 03:31 PM

Isn't that what I said? And x = (x = 0) similarly sets x to 0.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: How to update field variable to wait for value update from event

Posted 09 December 2016 - 03:32 PM

View Postandrewsw, on 09 December 2016 - 04:49 PM, said:

What are you attempting with this x = (x = 1);? This sets x to 1, and to 0 in the following code.


The "and to 0" does not happen.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: How to update field variable to wait for value update from event

Posted 09 December 2016 - 03:40 PM

Ah! Confusion dispelled.

Now the question is how the OP was getting x becoming 0 instantly. In some ways, I can see the code looping infinitely if optimizations are turned on and the compiler places x into a register and never hits a memory barrier. But that would mean that x is 1. Unfortunately, he was reporting that x was 0. So that means that something must have set it to 0. Based on the presented code, it's the event handler.

Any which way, doing a busy wait like that is very very bad, specially in a GUI. The correct thing to do if one must absolutely wait on a something like that is to use semaphores or mutexes.
Was This Post Helpful? 0
  • +
  • -

#8 danis_marf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 03-November 16

Re: How to update field variable to wait for value update from event

Posted 09 December 2016 - 04:03 PM

View PostSkydiver, on 09 December 2016 - 02:26 PM, said:

I suspect that you may have registered the same speech completion event handler multiple times, or that you simply may have invoked speech more than once.


Hello,

yes I'm trying to look for if it, if subscribed to the event multiple times, but can get where can it be


I have SpeechSynthesizer:

        public Form1()
        {
            InitializeComponent();         
        }
        SpeechSynthesizer synth = new SpeechSynthesizer(); 



in separate function I have event handler with speech:

        public void Function2(string text)
        {
        synth.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(synth_SpeakCompleted); 
        synth.Speak(text);
        }


I have event:

        public void synth_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
        {                      
             Function();       
        }


and I have Function:

        public void Function() 
        {
             Process.Start(filePath);   
        }

Was This Post Helpful? 0
  • +
  • -

#9 danis_marf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 03-November 16

Re: How to update field variable to wait for value update from event

Posted 09 December 2016 - 04:27 PM

View Postandrewsw, on 09 December 2016 - 02:49 PM, said:

What are you attempting with this x = (x = 1);? This sets x to 1, and to 0 in the following code. If you wanted to increment the value you would use x = x + 1 or x += 1, or even just x++.


Hello, x = (x = 1); not sure if it is needed there, but the goal was to keep field variable x equal to 1 initially, keep it equal to 1 with SpeakProgress, and make it 0 with SpeakCompleted when it reaches. And then while loop until x > 0 and after followed function, but I'm not sure, even if event updates value, in result I got two wrong things, seems like it is immediately becomes 0, because I have function implementation before speech ends, and at the same time program hangs with endless loop as it is 1 and not 0.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: How to update field variable to wait for value update from event

Posted 09 December 2016 - 04:44 PM

How is Function2() related Function()?

When is Function2() called? How often is it called? I ask because you register the same event handler each time so that means your event handler will be called N times where N is the number of times Function2() is called.

View Postdanis_marf, on 09 December 2016 - 06:27 PM, said:

Hello, x = (x = 1); not sure if it is needed there, but the goal was to keep field variable x equal to 1 initially, keep it equal to 1 with SpeakProgress, and make it 0 with SpeakCompleted when it reaches.

Then simply use x = 1; when you want the value to be 1, and x = 0; when you want the value to be 0.


View Postdanis_marf, on 09 December 2016 - 06:27 PM, said:

And then while loop until x > 0 and after followed function, but I'm not sure, even if event updates value, in result I got two wrong things, seems like it is immediately becomes 0, because I have function implementation before speech ends

I suggest setting breakpoints on your various functions and looking at the callstack to see when things get called. It's likely you have a call to Function() leftover someplace else. Also you'll see exactly when you are setting x to 1 or 0.

View Postdanis_marf, on 09 December 2016 - 06:27 PM, said:

at the same time program hangs with endless loop as it is 1 and not 0.

This is actually the behavior I was anticipating would happen.
Was This Post Helpful? 0
  • +
  • -

#11 JacobH   User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 181
  • Joined: 07-September 15

Re: How to update field variable to wait for value update from event

Posted 09 December 2016 - 05:10 PM

Make sure you're not calling Function2() more than once, as mentioned. Either set a break point and look at call stack, or use a dirty trick to test, then find the culprit.

private bool _subscribeToSpeakCompleted;
public void Function2(string text)
{
    if(!_subscribeToSpeakCompleted)
    {
        synth.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(synth_SpeakCompleted); 
        synth.Speak(text);
        _subscribeToSpeakCompleted = true;
    }
}


If the above makes anything better, you're probably calling it multiple times.

This post has been edited by JacobH: 09 December 2016 - 05:10 PM

Was This Post Helpful? 0
  • +
  • -

#12 danis_marf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 03-November 16

Re: How to update field variable to wait for value update from event

Posted 09 December 2016 - 05:15 PM

View PostSkydiver, on 09 December 2016 - 04:44 PM, said:

How is Function2() related Function()?

When is Function2() called? How often is it called? I ask because you register the same event handler each time so that means your event handler will be called N times where N is the number of times Function2() is called.


public void Function2()(string text) is takes string value from different places in the code, but each implementation is separate, and this Function2()(string text) also contains event handler of SpeakCompleted. The relation between this Function2()(string text) is actually nothing. Only needed relation, event must call Function(); when speech ends with reaching of SpeakCompleted and Function(); must run independent prog.exe file.
Was This Post Helpful? 0
  • +
  • -

#13 danis_marf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 03-November 16

Re: How to update field variable to wait for value update from event

Posted 09 December 2016 - 06:08 PM

View PostJacobH, on 09 December 2016 - 05:10 PM, said:

If the above makes anything better, you're probably calling it multiple times.


This way I got speech only once, and then it just does not work
Was This Post Helpful? 0
  • +
  • -

#14 danis_marf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 03-November 16

Re: How to update field variable to wait for value update from event

Posted 09 December 2016 - 07:14 PM

It was my my basic silly mistake, I just did not Initialize event handler, when it was wrongly inside the function. Anyway thank you for your support. Problem solved
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1