Problems in real-time display of Stock Information in Ajax instances

Source: Internet
Author: User

For the code of this instance, please refer to the previous blog "ajax instance real-time stock information display". Although the syntax of Java and C # is very similar, there are still many differences in the details, so there are several problems in the conversion process:

  1. Java contains httpservlet and general processing programs under C #.
  2. Java has a hashmap and a dictionary under C #.
  3. In Java, the random class has nextboolean (), and C # flexible use of nextdouble ()
  4. In Java, Timer uses anonymous functions, and C # uses delegation.
Different General processors

Httpservlet is specially used to respond to requests. At first, I used web forms under Asp.net, but web forms are obviously more visible, and the server required in this example does not have to have pages, therefore, the general processing program (*. ashx), which is very similar to httpservlet and can be used to process simple requests. It saves resources and responds faster than web forms.

Dictionary

C # There is no hashmap, but you can use its dictionary to achieve the same effect. The dictionary stores the same key-value pairs. Of course, the value can be of the object type.

Nextboolean ()

In Java, nextboolean () can be used to determine whether to increase or decrease.

// Create a new random number generation object final random = new random (); // increase or decrease if (random. nextboolean () {SZ = 0-SZ ;}

C? This allows flexible use of nextdouble (), nextdouble () value is 0 ~ 1. Compared with 0.5, you can decide whether to increase or decrease.

Random RDM = new random (); // rising floating if (RDM. nextdouble ()> 0.5) {SZ = 0-SZ ;}
Anonymous Functions

Well, the first three problems are very simple, and the fourth one is quite difficult. Let's take a look at the previous timing code that has not been solved (part of the server code ):

/// <Summary> /// initialize configuration data /// </Summary> Public void initnew () {// set the stock dictionary stock = new dictionary <string, stock> (); stock. add ("300001", szzs); stock. add ("600000", pfyh); stock. add ("601398", gsyh); stock. add ("601857", zgsy); // set the timer parameter timer = new system. timers. timer (); timer. enabled = true; timer. interval = 50; // execute the timer function theout timer. elapsed + = new system. timers. elapsedeventhandler (theout );} /// <summary> /// the timer executes the function // </Summary> /// <Param name = "Source"> event source </param> /// <param name = "E"> event parameter </param> Public void theout (Object source, system. timers. elapsedeventargs e) {// stock change range: Random RDM = new random (); double rdm2 = RDM. nextdouble (); // floating double SZ = rdm2 * 30; double pF = rdm2 * 0.5; double GS = rdm2 * 0.1; double zg = rdm2 * 0.3; // falling floating if (rdm2> 0.5) {SZ = 0-SZ;} If (rdm2> 0.5) {pF = 0-PF;} If (rdm2> 0.5) {GS = 0-GS;} If (rdm2> 0.5) {zg = 0-ZG;} // current stock price szzs. setcurrent (math. round (szzs. getcurrent () + sz) * 100)/100.0); pfyh. setcurrent (math. round (pfyh. getcurrent () + PF) * 100)/100.0); gsyh. setcurrent (math. round (gsyh. getcurrent () + GS) * 100)/100.0); zgsy. setcurrent (math. round (zgsy. getcurrent () + zg) * 100)/100.0 );}

Directly run the server code without adding the client code. The running result is:

This is a commonly used delegation method, but it is obvious that the stock floating Value Attribute "ran" cannot be read. If you put the code in Vs, debugging will find: the program will be randomly executed repeatedly in theout events.

The changed code is as follows:

/// <Summary> /// initialize configuration data /// </Summary> private void Init () {// create four new stocks: stock szzs = new stock ("300001", "Shanghai Stock Index", 300); stock pfyh = new stock ("600000", "Shanghai Pudong Development Bank ", 25); stock gsyh = new stock ("601398", "ICBC", 6.5); stock zgsy = new stock ("601857", "CNPC", 19.1 ); // set the stock dictionary stock = new dictionary <string, stock> (); // Add the stock Stock. add ("300001", szzs); stock. add ("600000", pfyh); stock. add ("601398", gsyh); stock. add ("601857", zgsy); // set the timer parameter, not too large timer = new system. timers. timer (50); timer. enabled = true; // execute the timer function theout random RDM = new random. nextdouble (); // The timer oscillating event. The timer is executed using an anonymous function. elapsed + = delegate (Object source, system. timers. elapsedeventargs e) {// stock change range // floating up double SZ = MCM * 30; double pF = MCM * 0.5; double GS = MCM * 0.1; double zg = MCM * 0.3; // down floating if (MCM> 0.5) {SZ = 0-SZ;} If (MCM> 0.5) {pF = 0-PF ;} if (MCM> 0.5) {GS = 0-GS;} If (MCM> 0.5) {zg = 0-ZG;} // current stock price szzs. setcurrent (math. round (szzs. getcurrent () + sz) * 100)/100.0); pfyh. setcurrent (math. round (pfyh. getcurrent () + PF) * 100)/100.0); gsyh. setcurrent (math. round (gsyh. getcurrent () + GS) * 100)/100.0); zgsy. setcurrent (math. round (zgsy. getcurrent () + zg) * 100)/100.0 );};}

The running result is:

We can see that this time we can read the stock Floating Field "ran". In the vs process debugging, we will also find that the code for the oscillating event of each timer is only executed once.

Compare the differences between the two codes. The difference is only in the position of the timer oscillator Event code: the first part of the clock Event code is put into theout event, the second part of the code is put into the anonymous function, which seems to be slightly different, but it involves the scope of variables. In the first part of the code, if you want to use

Stock szzs = new stock ("300001", "Shanghai index", 300); stock pfyh = new stock ("600000", "Shanghai Pudong Development Bank", 25 ); stock gsyh = new stock ("601398", "ICBC", 6.5); stock zgsy = new stock ("601857", "CNPC", 19.1 );

This variable can only be placed outside the function or event and used as a global variable; and

Random rdm = new Random();        double rdm2 = rdm.NextDouble();

These two lines of code will have different running results in theout and anonymous functions.

Repeated execution

When we talk about repeated execution

/// <Summary> /// the timer executes the function // </Summary> /// <Param name = "Source"> event source </param> /// <param name = "E"> event parameter </param> Public void theout (Object source, system. timers. elapsedeventargs e) {// stock change range: Random RDM = new random (); double rdm2 = RDM. nextdouble (); // floating double SZ = rdm2 * 30; double pF = rdm2 * 0.5; double GS = rdm2 * 0.1; double zg = rdm2 * 0.3 ;}

The execution of these lines of code is irregular and does not solve this problem. However, because the anonymous function method is used, there is no problem in the overall function implementation, for more information, see http://download.csdn.net/detail/lidaasky/4936350.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.