+ 1
Trying out threads in C#
Can anyone give me some feedback on this simple code with bouncing O:s as I am trying to understand how threads work in C#. https://code.sololearn.com/ca17A21A12a5/#cs
8 Respuestas
+ 2
At first i must to say, SoloLearn playground does not support thread.
A thread is defined as the execution path of a program. Each thread defines a unique flow of control. If your application involves complicated and time consuming operations, then it is often helpful to set different execution paths or threads, with each thread performing a particular job.
Threads are lightweight processes. One common example of use of thread is implementation of concurrent programming by modern operating systems. Use of threads saves wastage of CPU cycle and increase efficiency of an application.
So far we wrote the programs where a single thread runs as a single process which is the running instance of the application. However, this way the application can perform one job at a time. To make it execute more than one task at a time, it could be divided into smaller threads.
+ 2
But i not recommended to use thread directly. At past we use backgroundworker. We often use background threads when a time-consuming process needed to be executed in the background without affecting the responsiveness of the user interface. This is where a BackgroundWorker component comes into play.
+ 2
It's now recommended to use task. NET framework provides Threading.Tasks class to let you create tasks and run them asynchronously. A task is an object that represents some work that should be done. The task can tell you if the work is completed and if the operation returns a result, the task gives you the result
there are differences between task and thread
+ 2
The Thread class is used for creating and manipulating a thread in Windows. A Task represents some asynchronous operation and is part of the Task Parallel Library, a set of APIs for running tasks asynchronously and in parallel.
The task can return a result. There is no direct mechanism to return the result from a thread.
Task supports cancellation through the use of cancellation tokens. But Thread doesn't.
A task can have multiple processes happening at the same time. Threads can only have one task running at a time.
We can easily implement Asynchronous using ’async’ and ‘await’ keywords.
A new Thread()is not dealing with Thread pool thread, whereas Task does use thread pool thread.
A Task is a higher level concept than Thread.
+ 2
I recommend you to use Visual Studio
+ 1
Thank you very much! Now I must find time for some more crazy experiments in order to really understand tasks vs threads!! Really good to know that some things such as threads are not supported here in the playground!
0
First issue is that I cannot run it in sololearn playground. What am I missing?
Second issue as I run it in Visual Studio the threads seem to disturb eachother. How can that be solved?