M
M
Mikhail2017-11-12 04:07:43
WPF
Mikhail, 2017-11-12 04:07:43

Why is the thread blocking so strangely?

Faced a problem. The stream somehow strangely is blocked. The code is like this:

LambdaMechanism Mech = new LambdaMechanism(Angle, Radius);
Mech.Draw(canvas);
Thread.Sleep(TimeSpan.FromSeconds(3));
MessageBox.Show("Yes");

According to my logic, this code should draw the necessary elements, and then after 3 seconds display Yes on the screen. But in fact, after starting, after 3 seconds, elements are drawn at the same time and Yes is displayed on the screen. What is the problem? Inside Mech.Draw(), threads, timers, etc. are not affected.
PS I know that you can do this:
Task.Delay(3000).ContinueWith(_ => MessageBox.Show("Yes"))
But that option doesn't work for me. It is necessary to "stop" the execution of the code for 3 seconds

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuuki Wesp, 2017-11-12
@mak_ufo

You are putting UIThread to sleep.
And you can't do that.
You will have to create a new thread and stop it for 3 seconds, then output Yes.

LambdaMechanism Mech = new LambdaMechanism(Angle, Radius);
Mech.Draw(canvas);
new Thread(()=> 
{
Thread.Sleep(TimeSpan.FromSeconds(3));
MessageBox.Show("Yes");
}).Start();

As an option.
So or:
It seems that the
Graphics thread does not draw elements instantly.
And you put him to sleep, and of course he does not have time to draw what you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question