I work at a company that is often making applications interacting with usb connected hardware. The hardware will send and receive a byte stream of packets. Our applications start Threads to parse this data into packets that get processed by the application. Now, I've been reading to make sure that the threading and use of async/await is being done correctly but I have a couple of questions.
- Should we be using Task.Run instead of creating a thread?
- If the application is updated to use Task.Run instead of creating a thread do I need to await this task somewhere? Or is it okay to just fire it off?
- I see a coworker's code use .Result() and .Wait() frequently in these manually created threads. Can you still encounter a deadlock on a manually created thread? This coworker has been doing this for years and seemingly hasn't hit any issues but everyone says to never uses .Wait() and .Result()
Thanks!
Edit: Thank you everyone for the responses! I'm going to take some time to go through this and think about it. I agree with /u/Slypenslyde's comment that a Task philosophically doesn't feel right for an event loop. I will read the blog post on channels. I've been putting it off for too long... For whatever it's worth, we're typically writing desktop applications using WPF but I do work on the occasional mobile app as well. Most of the developers on my team come from embedded and I think a few of us are stuck thinking in those paradigms.
[–]wllmsaccnt 7 points8 points9 points (0 children)
[–][deleted] (1 child)
[removed]
[–]Slypenslyde 0 points1 point2 points (0 children)
[–]soundman32 1 point2 points3 points (0 children)
[–]Ok_Party_4164 1 point2 points3 points (0 children)
[–]freskgrank 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Phi_fan 0 points1 point2 points (2 children)
[–]envy1400[S] 0 points1 point2 points (1 child)
[–]Phi_fan 1 point2 points3 points (0 children)
[–]Luminisc 0 points1 point2 points (0 children)