Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

How to return array of string to the Form?

$
0
0

Hello friends. I am developing an application for scanning IPs in Local Area Network (LAN). It works well if I am not using class for this application. When I intend to return array of string from class to Form, it returns nothing. 

.
.
.
.
public Form1()
        {
            InitializeComponent();
        }

private void Btn_Start_Click(object sender, EventArgs e)
        {


Classes.ClassIPScan ipsc = new Classes.ClassIPScan( StartIPAddress, EndIPAddress);            
            ipsc.ipSCAN();
            lvw_Show.Text += ipsc.finish();
}
}

Here is class code:

namespace IP_SC.Classes
{
    public class ClassIPScan
    {
        AutoResetEvent resetEvent = new AutoResetEvent(false);
        //=========================================================================================
        // Define a thread 
        private Thread th;

        // Wanted to write a progress bar later or just think. 

        public int intStart = 0;
        public int intEnd = 0;
        string ipBefore = null;

        // Define a storage IP ArrayList 
        ArrayList ip = new ArrayList();
        private string StartIPAddress;
        private string EndIPAddress;


        string[] Send = new string[200];


        // Define a thread commissioned 
        private delegate string returnStrDelegate();
        //=========================================================================================


        public ClassIPScan(string StartIPAddress, string EndIPAddress)
        {
            // TODO: Complete member initialization
            this.StartIPAddress = StartIPAddress;
            this.EndIPAddress = EndIPAddress;
        }

        public void ipSCAN()
        {
            try
            {
                ipBefore = StartIPAddress.Substring(0, StartIPAddress.LastIndexOf("."));
                intStart = Int32.Parse(StartIPAddress.Substring(StartIPAddress.LastIndexOf(".") + 1));
                intEnd = Int32.Parse(EndIPAddress.Substring(EndIPAddress.LastIndexOf(".") + 1));
                // Open a thread. 
                th = new Thread(new ThreadStart(SearchIP));
                th.Start();
            }
            catch
            {

                if (th != null)
                {
                    // Determine the the scan port number of the county is in progress 
                    if (th.ThreadState == ThreadState.Running)
                    {
                        th.Abort();
                    }
                }
            }
        }


        private void SearchIP()
        {
            try
            {
                // Define a new IP 
                string newIP = null;
                for (int i = intStart; i <= intEnd; i++)
                {
                    // Use the ping class 
                    Ping myPing = new Ping();
                    // Add the class function of the response time of the completion of 
                    myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);

                    newIP = ipBefore + "." + i.ToString();
                    // Send the ping asynchronously
                    myPing.SendAsync(newIP, 1000, null);
                }
            }
            catch
            {
                MessageBox.Show("not found IP");
            }
        }

        // Add data to the ListBox 
        public void _myPing_PingCompleted(object sender, PingCompletedEventArgs e)
        {
            int i = 0;
            while (e.Reply.Status == IPStatus.Success & !ip.Contains(e.Reply.Address.ToString().Trim()))
            {
                ip.Add(e.Reply.Address.ToString().Trim());
            }

            try
            {
                foreach (string st in ip)
                {
                    //lvw_Show.LabelWrap the = true; 
                    //string strHostName = null;
                    // IP Scan your computer name - speed is very slow. 

                    //strHostName = Dns.GetHostEntry(st).HostName;
                    //String IP; // fight 
                    //Send[i++] = (st + ">>" + strHostName);
                    Send[i++] = (st);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finish();
        }

        public string[] finish()
        {
            return Send;
        }
    }
}
Please show me where am I doing wrong?

I really appreciated.



Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>