C # delegates and topics!

What are delegates and threads for?

+4
source share
8 answers

Delegates act as a logical (but safe) equivalent to functional pointers; they allow you to talk about the operation in an abstract way. Events are a typical example of this, but I'm going to use the "functional programming" example: search in a list:

List<Person> people = ... Person fred = people.Find( x => x.Name == "Fred"); Console.WriteLine(fred.Id); 

"lambda" here, in essence, is an instance of a delegate - a delegate of the type Predicate<Person> - that is, "a given person, this is something true or false." Using delegates allows very flexible code, i.e. The List<T>.Find can find all kinds of things based on the delegate that the caller passes.

Thus, they act mainly as a 1-method interface, but much more succinctly.

+11
source

Delegates : Basically, a delegate is a method reference method. It looks like a pointer to a method that you can set for different methods corresponding to its signature and use it to pass a reference to this method.

Thread is a sequential stream of instructions that are executed one after another to complete a calculation. You may have different threads running at the same time to perform a specific task. The thread runs on a single logical processor.

+7
source

Delegates are used to dynamically add methods to events.

Threads run inside processes and allow you to run two or more tasks that share resources.

+2
source

I suggest looking under these conditions, there is a lot of information. These are pretty basic concepts, a wiki is a high-level place to start:

http://en.wikipedia.org/wiki/Thread_(computer_science)
http://en.wikipedia.org/wiki/C_Sharp_(programming_language)

+1
source

Specific examples always help me, so here are the threads. Consider your web server. As requests arrive at the server, they are sent to the web server process for processing. He can process each one as it arrives, fully processing the request and creating the page before moving on to the next. But consider how much of the processing is done at the speed of the hard drive (and not at the speed of the CPU), since the requested page is retrieved from the disk (or the data is pulled from the database) before the response can be executed.

By pulling threads from the thread pool and providing each request with its own thread, we can take care of the needs of non-disk for hundreds of requests before the disk returns data for the first. This will provide a degree of virtual parallelism, which can significantly improve performance. Keep in mind that web server performance is much greater, but this should give you a concrete model of how threads can be useful.

+1
source

They are useful for the same reason that high-level languages ​​are useful. You do not need them for anything, since in reality it is just an abstraction of what is actually happening. They greatly simplify and speed up programming or understanding.

+1
source
Mark Gravell gave a good answer: "What is a delegate."

Andrew Troelsen defines flow as

... the execution path inside the process. "Pro C # 2008 and the .NET 3.5 Platform," APress.

All processes running on your system have at least one thread. Let me call it the main thread. You can create additional streams for various reasons, but the most striking example to illustrate the purpose of streams is printing.

Say you open your favorite word processing application (WPA), type a few lines, and then want to print these lines. If your WPA uses the main stream to print a document, the WPA user interface will be frozen until printing is complete. This is because the main thread must print lines before it can handle any user interface events, i.e. Button clicks, mouse movements, etc. As if the code was written as follows:

  do { ProcessUserInterfaceEvents(); PrintDocument(); } while (true); 

Clearly, this is not what users want. Users want the user interface to respond while printing a document.

The answer, of course, is to print the lines in the second thread. Thus, the user interface can focus on handling user interface events, while the secondary stream focuses on printing lines.

The illusion is that both tasks are performed simultaneously. On a single processor machine, this may not be true, since the processor can only execute one thread at a time. However, switching between threads is so fast that the illusion is usually maintained. On a multiprocessor (or multipath) machine, this can be literally true, since the main thread can run on one processor, while the secondary thread runs on another processor.

In .NET, threads are a breeze. You can use the System.Threading.ThreadPool class, use asynchronous delegates, or create your own System.Threading.Thread objects.

If you are new to streaming, I would choose two warnings.

First, you can damage your application performance if you choose the wrong thread model. Be careful not to use too many threads or try to permeate everything that should happen sequentially.

Secondly (and more importantly), keep in mind that if you are exchanging data between threads, you will most likely need to synchronize access to this shared data, for example, using the lock keyword in C #. There is a lot of information on this topic available on the Internet, so I will not repeat it here. Just keep in mind that you may encounter intermittent, not always repetitive errors if you do not do this carefully.

+1
source

Your question is unclear ...

But you probably just want to know how to use them to have a window, a lengthy process and a progress bar ...

So, create a thread to execute a time-consuming process and use delegates to increase the progress bar! :)

0
source

All Articles