Which code segment should you use?

You are developing an application to perform mathematical calculations.
You develop a class named CalculationValues.
You write a procedure named PerformCalculation that operates on an instance of the class.
You need to ensure that the user interface of the application continues to respond while calculations are being performed.
You need to write a code segment that calls the PerformCalculation procedure to achieve this goal.
Which code segment should you use?

You are developing an application to perform mathematical calculations.
You develop a class named CalculationValues.
You write a procedure named PerformCalculation that operates on an instance of the class.
You need to ensure that the user interface of the application continues to respond while calculations are being performed.
You need to write a code segment that calls the PerformCalculation procedure to achieve this goal.
Which code segment should you use?

A.
private void PerformCalculation() {…}
private void DoWork(){
CalculationValues myValue = new CalculationValues();
Thread newThread = new Thread(new ThreadStart(performCalculation));
newThread.Start(myValues);
}

B.
private void PerformCalculation() {…}
private void DoWork(){
CalculationValues myValue = new CalculationValues();
ThreadStart delStart = new ThreadStart(PerformCalculation);
Thread newThread = new Thread(delStart);
if (newThread.IsAlive) {newThread.Start(myValue);}
}

C.
private void PerformCalculation (CalculationValues values) {…}
private void DoWork(){
CalculationValues myValue = new CalculationValues();
Application.DoEvents();
PerformCalculation(myValue);
Application.DoEvents();
}

D.
private void PerformCalculation(object values) {…}
private void DoWork(){
CalculationValues myValues = new CalculationValues();
Thread newThread = new Thread(new ParameterizedThreadStart(PerformCalculation));
newThread.Start(myValues);
}

Explanation:
It is a requirement that the UI continues to respond, hence PerformCalculation should execute in a separate thread.
PerformCalculation requires a parameter hence you should use the ParameterizedThreadStart delegate.
A& B attempt to supply a parameter to the ThreadStart delegate. This is not possible.
C Does not run in a new thread and hence may leave the UI unresponsive.



Leave a Reply 1

Your email address will not be published. Required fields are marked *


networkmanagers

networkmanagers

I have the same idea. D