Hack Forums
[.NET] Make a thread wait (sleep) for 1s - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Make a thread wait (sleep) for 1s (/showthread.php?tid=5052785)



Make a thread wait (sleep) for 1s - SIM-2362662 - Nov 10, 2015

So i'm using a thread let's say to count up to 1000. after it gets to 1000 i want it to want it to wait 1s and continue.
I tried with Threading.Thread.Sleep(1000) but this command also make the gui unresponding...
Any better alternative?


* - Hawk HF - Nov 10, 2015

I haven't worked with threads very much, but if I had to guess, I'd say you're making the GUI sleep instead of your worker thread.

Code
Dim myThread As Sytem.Threading.Thread
myThread.Sleep(1000)



* - SIM-2362662 - Nov 10, 2015

(Nov 10, 2015 07:06 AM)Hawk HF Wrote:  I haven't worked with threads very much, but if I had to guess, I'd say you're making the GUI sleep instead of your worker thread.

Code
Dim myThread As Sytem.Threading.Thread
myThread.Sleep(1000)

I tried that. GUI is still not responding.


* - Hawk HF - Nov 10, 2015

(Nov 10, 2015 07:09 AM)♣ SoFt ♣ Wrote:  I tried that. GUI is still not responding.

It should not be since you have two separate threads.

Can you provide some code?


* - Streetlight. - Nov 10, 2015

Yeah post some code so we can help.


* - Lovely❤️ - Nov 10, 2015

Yea, we need some code to see what's wrong.


* - Odus - Nov 10, 2015

I hope you're not calling Thread.Sleep() from the main thread.

Code
'Dim t As New Thread(AddressOf CountUp)
't.Start()

Private Sub CountUp()
    For i As Integer = 0 To 1000
        Debug.WriteLine(i)
    Next

    Thread.Sleep(1000)
End Sub



* - SIM-2362662 - Nov 10, 2015

(Nov 10, 2015 07:16 AM)Hawk HF Wrote:  It should not be since you have two separate threads.

Can you provide some code?

I managed to fix it . Apperantly the main ui freezed because i used a set.visibility before sleeep (wtf..)
I removed that line and now it works fine. I still can't understand why that line would create such a error .. Anyway thank you man.


* - nathan72389 - Nov 10, 2015

(Nov 10, 2015 07:54 AM)♣ SoFt ♣ Wrote:  I managed to fix it . Apperantly the main ui freezed because i used a set.visibility before sleeep (wtf..)
I removed that line and now it works fine. I still can't understand why that line would create such a error .. Anyway thank you man.

why would you do that?
there is no logic there.


* - +Haze - Nov 10, 2015

(Nov 10, 2015 07:54 AM)♣ SoFt ♣ Wrote:  I managed to fix it . Apperantly the main ui freezed because i used a set.visibility before sleeep (wtf..)
I removed that line and now it works fine. I still can't understand why that line would create such a error .. Anyway thank you man.

Post your code here so we can have a look. It's better for you to understand why a piece of code works than you to leave without the understanding.


* - IAmNotHere - Nov 10, 2015

Add a stopwatch, no CPU will usage will harm your computer or your computer fan starts making noise and your gui does respond.

Take care.