Hack Forums
[C#] Delay code execution - 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: [C#] Delay code execution (/showthread.php?tid=5717574)



Delay code execution - sososerious - Sep 14, 2017

After searching hours and hours and debugging and whatnot seems like i just cant find a solution to so simple problem... what i need is something like this

for(int i=0; i<100; i++)
{
wait here 2-3 seconds
string result = doSomeGUIworkHere();

if(result == desiredResult)
{
break;
}
}

I tried Thread.Sleep, timer with elapsed event, task delay with async await... But to no awail. If i use thread.sleep it just blocks thread and wont do doSomeGUIworkHere();
If i use timer then it just fires doSomeGUIworkHere(); 100 times.

Please help.


* - SIM-2071734 - Sep 14, 2017

Use a thread? charlimit

[warn]


* - sososerious - Sep 14, 2017

(Sep 14, 2017 12:56 PM)lupettohf Wrote:  Use a thread? *charlimit*
Any code sample? :)


* - MrRopain - Sep 14, 2017

Why would you have to wait? Maybe theres a better approach to what youre trying to achieve than to wait.


* - 0xDEAD10CC - Sep 14, 2017

https://msdn.microsoft.com/en-us/library/hh139096(v=vs.110).aspx


* - Sorzus - Sep 14, 2017

Do not use a thread! That'd be a huge waste of processor time. Just use await or a timer (which is actually the same, await Task.Delay as mentioned above might be the best solution). Can you show us your not working code with async/await?


* - ForceJew - Sep 15, 2017

async void Dank()
{
for(int i=0; i<100; i++)
{
await Task.Delay(2000);
string result = doSomeGUIworkHere();

if(result == desiredResult)
{
break;
}
}
}
should work i guess


* - sososerious - Sep 15, 2017

Thank you guys! I have found your suggestions very helpful and i have found solution so thanks all!! :)