How to make a function located in form load to run again in button click function?

  • Thread starter Thread starter samiarja
  • Start date Start date
S

samiarja

Guest
Hi all,

I am currently developing an application that read from a JSON file as shown in the figure below.:

[
{
"Record": 1,
"IPaddress": "192.168.1.9",
"Machinename": "taurus",
"username": "root",
"password": "root",
"sourcefolder": "/home/root/data/completed",
"destfolder": "C:\\temp",
"filextension": ".db",
"removedownloaded": 1,
"removecsv": 1,
"removedb": 1,
"eagleIOHost": "sftp.eagle.io",
"eagleIOUsername": "sftp.eagle.io",
"eagleIOpassword": "nautitech259",
"eagleIOdirectory": "/usr/share/tomcat8"
},
{
"Record": 2,
"IPaddress": "192.168.1.10",
"Machinename": "taurus",
"username": "root",
"password": "root",
"sourcefolder": "/home/root/data/completed",
"destfolder": "C:\\temp",
"filextension": ".db",
"removedownloaded": 1,
"removecsv": 1,
"removedb": 1,
"eagleIOHost": "sftp.eagle.io",
"eagleIOUsername": "sftp.eagle.io",
"eagleIOpassword": "nautitech250",
"eagleIOdirectory": "/usr/share/tomcat8"
}
]

it is a dynamic application that shows entries depending on the number of record.

At the back-end there is a function that ping each ip address form load and then display the IP address status whether it is "in range" or "out of range" as shown in the figure below.


1526038.png

The user will click on start to begin transferring data to the local server and do other tasks, all the function in this "button click" will repeat the whole process every 10 seconds through a timer loop in order to get the IP time average per 'ms' like below:

for (Int32 i = 0; i < 10; ++i) hosts.Add(item.IPaddress);

var average = hosts.AsParallel().WithDegreeOfParallelism(64).
Select(h => new Ping().Send(h).RoundtripTime).Average();

and, at some points some machines will exit the network field so they will become "out of range". Therefore, the label should be updated whenever the application is running.

The code that run when the form is loaded is below:

Ping p = new Ping();
PingReply r;
string s = item.IPaddress;
r = p.Send(s);
if (r.Status == IPStatus.Success)
{
statusLabel.Text = "In Range";
}
else
{
statusLabel.Text = "Out of Range";
}

This is perfectly working so the user will know how many machines are in range when they first start the application.

However, whenever I try to integrate it in "button click" function, it doesn't recognize statusLabel as it is only declared in form load.

Is there a way to keep the variable statusLabel visible to the other functions and included it in the timer loop inside the button click function?

Thank you very much in advance.


Sami Arja

Continue reading...
 
Back
Top