M
M
maranqz2015-12-01 13:00:41
C++ / C#
maranqz, 2015-12-01 13:00:41

Access from another thread, Invoke hanging, how to solve?

There is a function that is written to Action.

void videoRecieved(Google.Apis.YouTube.v3.Data.Video video)
        {
            Debug.WriteLine(video.Id);

            this.Invoke((Action)(
                () => progressBar.Value = progressBar.Maximum
            ));

            Debug.WriteLine(video.Id);
        }

It has a line in which the value of progressBar changes.
I call it through invoke , thereby causing a new thread, but for some reason this thread does not end and the whole program stops and does not go further.
If so, this function is passed twice. It is transmitted first to One class (A1), and then A1 passes to the next class A2.
What am I doing wrong?)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rainbird, 2015-12-01
@Rainberd

Invoke() raises the event not on a new thread, but on the main GUI thread of the application, and waits for the event to be processed. Probably, the GUI thread is busy with something and cannot process the event. Well, or the GUI thread, when progressBar.Value changes, should do something with video, but it cannot do this, because the video thread is blocked by waiting for the GUI thread.
option 1: figure out what the GUI thread is doing or blocking;
option 2: use BeginInvoke() - trigger the event but don't wait. Then the calling thread is not blocked, but there is no guarantee that upon exiting BeginInvoke() progressBar.Value has already changed its value, and will change it someday.
Yes, instead of this.Invoke((Action)(..) it is better to write progressBar.Invoke((Action)(..), but you need to change the progressBar - that means you need to do it in its thread.

A
Alexey Pavlov, 2015-12-01
@lexxpavlov

The question is in which thread this function is called.
Instead this.Invokeput Dispatcher.BeginInvoke.

M
MonkAlex, 2015-12-01
@MonkAlex

And it's even better to use bindings in wpf and not think about some kind of invocations.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question