Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Main and Worker Thread Model

c



+ Font mai mare | - Font mai mic



Main and Worker Thread Model

This is the commonest model and the one used throughout this book so far. It is illustrated in Figure 3:


Figure 3



In the Main and Worker thread model, the main thread receives all input and passes it to other threads to perform particular tasks. The main thread may or may not wait for the worker threads to finish. In this model, the worker threads don't interact directly with the input sources as they read their input from the main thread. For example, we could have three buttons on a Windows Forms application that trigger three separate events:

Get data from a web service

Get data from a database

Do something else such as parsing an XML file

This is the simplest threading model. The main thread is contained within the Main method, and the model is very common in client GUI applications.

Let's look at some code to demonstrate this. We'll use a form like the following:

When you click on a button, it will fire off a worker thread that will perform some calculations and return the results in the space below the buttons. We won't detail the UI code here; the full code can be downloaded from the https://www.Apress.com, but here are the relevant sections:

public class MainWorker

factors.Add(number.ToString());
return factors;



public long CalculateFactorial(int number)



The above methods are quite straightforward and are wrapped in a class for modularity reasons. The first returns an ArrayList containing all of the factors of the number passed to it, whereas the second simply returns a Long. Remember that factorials very quickly get very large. The factorial of 13 is 6,227,020,800. The factorial method doesn't tie up the processor for very long, but it can be used to illustrate this model.

public frmCalculate()

The constructor just contains an instantiation of a new MainWorker object that will be used in the methods. Below we show the methods used for the button click event handlers:

private void cmdFactors Click(object sender, System.EventArgs e)


void FactorsThread()


//Create and invoke the delegate with the new value
UpdateValue updVal = new UpdateValue(DisplayValue);
string[] Arugs = ;
this.Invoke(updVal,Arugs);

The cmdFactors_Click() method instantiates a new thread with the FactorsThread() method, which formats and acts upon the result contained in MainWorker.CalculateFactors().

This method will need to be wrapped because thread methods cannot have return values.

private void cmdFactorial Click(object sender, System.EventArgs e)


private void FactorialThread()

this.Invoke(updVal,Arugs);

The FactorialThread() method is much simpler. Whenever the cmdFactorial button is clicked, the main thread fires off a new thread and updates the lblResult text label when the results have been achieved.

This was a straightforward example of main and worker threads in actions. Obviously, this example can easily be changed to deal with a connection to a database, or other more time-consuming activities.

However, you need to take care of several issues relating to threads when you use this mode. You can have threads spawning threads, threads accessing the same resource, and threads going into an infinite loop.

This is the simplest model, but also the one that requires most work from the developer.

In addition, the threads are completely independent of each other, each being controlled entirely by its parent - in this case the main thread.



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 754
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved