Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

ManagementObjectCollection

$
0
0

Below code is working fine in Window 2003 32 bit. but i am using code in window 2008 R2 and above OS. It takes lot of time 

Please any help me. Is there any setting related issue.....

ManagementObjectCollection moReturn;ManagementObjectCollection moReturnTotalPorts;


                //Get The Total Number of services for Depth
                moSearch = new ManagementObjectSearcher("Select * from  Win32_Service WHERE Name like  'DEPTH_REMOTE_WIN_SERVER_" + PortFrom + "%' ");
                moReturnTotalPorts = moSearch.Get();
                iTotalNoofPorts = moReturnTotalPorts.Count;

                //Get The Total Number of Running services for Depth
                moSearch = new ManagementObjectSearcher("Select * from  Win32_Service WHERE State='Running' and Name like 'DEPTH_REMOTE_WIN_SERVER_" + PortFrom + "%'  ");
                moReturn = moSearch.Get();
                iActivePorts = moReturn.Count;
                System.Diagnostics.EventLog.WriteEntry("GetPortDetails", "GetPortDetails start 1");
                if (NoOfMainPortUseres > 0)
                {
                 if (RemoteServers.Trim() != "")
                  {
                      System.Diagnostics.EventLog.WriteEntry("GetPortDetails", "GetPortDetails start 2 RemoteServers.Trim()!=''");
                    //NoOfMainPortUseres.                     
                      string[] sServerInfo;
                      string[] sPortInfo;

                      sServerInfo = RemoteServers.Trim().ToString().Split(("#").ToCharArray());
                      sPortInfo = sServerInfo[0].Trim().ToString().Split((",").ToCharArray());
                      System.Diagnostics.EventLog.WriteEntry("GetPortDetails", "sServerInfo --" + RemoteServers);
                      System.Diagnostics.EventLog.WriteEntry("GetPortDetails", "sPortInfo --" + sPortInfo);

                      CreatePort(sPortInfo[1].ToString() , sPortInfo[2].ToString(),sPortInfo[3].ToString());

                      try
                      {
                          objSession_Info = (IDepth.ISession_Info)Activator.GetObject(typeof(IDepth.ISession_Info), "tcp://" + sPortInfo[0].ToString() + ":" + sPortInfo[1].ToString()+ "/Session_Info");
                          objSession_Info.MaxUserinRootPort();

                          i = objSession_Info.MaxUserInMainPort;
                          if (NoOfMainPortUseres > i )
                          {
                              return objSession_Info.GetPortDetails() + "#" + sPortInfo[0].ToString() + "," + sPortInfo[2].ToString() + ","+ sPortInfo[3].ToString();
                              //return
                          }
                          System.Diagnostics.EventLog.WriteEntry("GetPortDetails", "GetPortDetails End 2 RemoteServers.Trim()!=''");

                      }

                      catch (Exception ex)
                      {
                          System.Diagnostics.EventLog.WriteEntry("Depth Distributor", ex.Message);
                     //     throw;
                      }

                  }
                }
                System.Diagnostics.EventLog.WriteEntry("GetPortDetails", "GetPortDetails start 2 RemoteServers.Trim()--"+ MaxUser.ToString());
               int iPortCounter = 0;
               Boolean bPortHang = false;
                //Get The Total Number of Running services for Depth
myNewLabel: //Added by JK for Port Issue
                if (MaxUser != 0)
                {
                    System.Diagnostics.EventLog.WriteEntry("GetPortDetails", "GetPortDetails start 2 if (MaxUser != 0)");
                    foreach (ManagementObject Mo in moReturn)
                    {
                        field = Mo["Name"].ToString().Split(("_").ToCharArray());
                        if (bPortHang == false)
                        {
                            StrPortName = field[field.Length - 1];
                        }
                        else
                        {
                            StrPortName = Convert.ToString(Convert.ToInt16(StrPortName) + 1);
                            bPortHang = false;
                        }

                        objSession_Info = (IDepth.ISession_Info)Activator.GetObject(typeof(IDepth.ISession_Info), "tcp://localhost:" + StrPortName.Trim() + "/Session_Info");
                        int iCurrentUserPerPort = objSession_Info.CurrentUser;

                        //Checking For the Maxmimum Per Port
                        //Else Create New Port
                        if (iCurrentUserPerPort >= MaxUser)
                        { 


                                ////System.ServiceProcess.ServiceController myController1 = null;
                                ////myController1 = new System.ServiceProcess.ServiceController("DEPTH_REMOTE_WIN_SERVER_" + StrPortName);
                                ////myController1.Refresh();
                                ////        if (myController1.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                                ////        {
                                ////            //bPortStarted = true; commented by JK for port issue

                                ////            /*Port issue testing*/
                                ////            IDepth.ICommonTransactionFunc objCon;
                                ////            objCon = (IDepth.ICommonTransactionFunc)Activator.GetObject(typeof(IDepth.ICommonTransactionFunc),"tcp://localhost:" + StrPortName.Trim() + "/CommonTransactionFunc");
                                ////            DataSet dSet=null;
                                ////            String sTmp=objCon.RetrieveSearchData("GLVER", "", "", "","", ref dSet, null);


                                ////            if (dSet == null)
                                ////            {
                                ////                sReturnPortNO = "";
                                ////                continue; 
                                ////            }
                                            sReturnPortNO = "";
                                            continue; 
                                            /*Port issue testing*/

                                       // }          

                        }
                        else
                        { sReturnPortNO = StrPortName; return sReturnPortNO; }

                        iPortCounter = iPortCounter + 1;
                    }

                    //Checking For The Stoppe Ports 
                    if ((iTotalNoofPorts - iActivePorts) == 0)
                    {
                        if (bCreatePort == true)
                        {
                            if (moReturn.Count == 0) { CreateNewPort(PortFrom + "00"); return PortFrom + "00"; }
                            else
                            { if (sReturnPortNO == "")sReturnPortNO = CreateNewPort(StrPortName); }
                        }
                    }

                     //If Ports Present with Stopped Status Then Restart the Port
                    else
                    {
myLabel:
                        if (sUnResponcePort == "")
                        { moSearch = new ManagementObjectSearcher("Select * from  Win32_Service WHERE  State='Stopped' and Name like 'DEPTH_REMOTE_WIN_SERVER_" + PortFrom +"%'"); }
                        else
                        { moSearch = new ManagementObjectSearcher("Select * from  Win32_Service WHERE  State='Stopped' and Name like 'DEPTH_REMOTE_WIN_SERVER_" + PortFrom +"%'  " + sUnResponcePort.Trim() + " "); }

                        moReturn = moSearch.Get();

                        //Start Of 6 ----> if All Stopped Ports are Not Responding
                        if (moReturn.Count == 0)
                        {
                            int iTempNumOfPorts =1;
                            foreach (ManagementObject Mo in moReturnTotalPorts)
                            {
                                //Check For The Last Port
                                if (iTotalNoofPorts == iTempNumOfPorts)
                                {
                                    //if Last Port 
                                    string[] Tempfield;
                                    string sTempPortNumber = "";
                                    Tempfield = Mo["Name"].ToString().Split(("_").ToCharArray());
                                    sTempPortNumber = Tempfield[Tempfield.Length - 1];

                                    //Check for User Has Rights to Create Port
                                    if (bCreatePort == true)
                                    {
                                        //Creating a New Port
                                        return sReturnPortNO = CreateNewPort((int.Parse(sTempPortNumber) + 1).ToString());
                                    }
                                    else
                                    {return sReturnPortNO = "";}

                                }
                                iTempNumOfPorts++;
                            }

                        }
                        //End Of 6

                        foreach (ManagementObject Mo in moReturn)
                        {
                            System.Diagnostics.EventLog.WriteEntry("Get Port Detail", "Before Restarting Available Port;-"+DateTime.Now.ToString());
                            field = Mo["Name"].ToString().Split(("_").ToCharArray());
                            StrPortName = field[field.Length - 1];
                            sReturnPortNO = "";// StrPortName;
                            Assembly a = Assembly.LoadFrom(Application.StartupPath + "\\DEPTH_REMOTE_ADMIN.exe");
                            Type mytypes = a.GetType("DEPTH_REMOTE_ADMIN.DepthProfiler");
                            Object obj = Activator.CreateInstance(mytypes);
                            iBool = Boolean.Parse(mytypes.InvokeMember("RestartServer", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, new object[] { StrPortName }).ToString());

                            System.Diagnostics.EventLog.WriteEntry("Get Port Detail", "After Restarting Available Port;-" + DateTime.Now.ToString());
                            if (iBool == true)
                            {
                                DateTime dtStartime = DateTime.Now;
                                DateTime dtEndtime = DateTime.Now;
                                Boolean bPortStarted = false;


                                System.ServiceProcess.ServiceController myController = null;
                                myController = new System.ServiceProcess.ServiceController("DEPTH_REMOTE_WIN_SERVER_" + StrPortName);

                                do
                                {
                                        myController.Refresh();
                                        if (myController.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                                        {
                                            bPortStarted = true; //commented by JK for port issue

                                            /*Port issue testing*/
                                            ////IDepth.ICommonTransactionFunc objCon;
                                            ////objCon = (IDepth.ICommonTransactionFunc)Activator.GetObject(typeof(IDepth.ICommonTransactionFunc),"tcp://localhost:" + StrPortName.Trim() + "/CommonTransactionFunc");
                                            ////DataSet dSet=null;
                                            ////String sTmp=objCon.RetrieveSearchData("GLVER", "", "", "","", ref dSet, null);


                                            ////if (dSet == null)
                                            ////{
                                            ////    bPortStarted = false;
                                            ////    bPortHang = true;
                                            ////    goto myNewLabel;
                                            ////}
                                            ////else
                                            ////{
                                            ////    bPortStarted = true;
                                            ////}
                                            /*Port issue testing*/

                                        }                                   

                                    dtEndtime = DateTime.Now;

                                    if (DateDiff(DateInterval.Second, dtStartime, dtEndtime) >= 15)
                                    {sUnResponcePort = sUnResponcePort + " AND Name <> 'DEPTH_REMOTE_WIN_SERVER_" + StrPortName + "'";
                                     goto myLabel;}
                                }
                                while (bPortStarted == false);

                               sReturnPortNO = StrPortName;
                                return sReturnPortNO;
                            }

                        }


                    }

                                                                                                                                                                                                                                    

Viewing all articles
Browse latest Browse all 8156

Trending Articles



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