示例#1
1
        private static void InsertInfoRun(string Key, GridEX grid, bool DontInsertNull)
        {
            Cursor.Current = Cursors.WaitCursor;
            grid.ClearItems();

            ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + Key);

            DateTime start = DateTime.Now;

            bool cancel = false;

            Thread t = new Thread( new ThreadStart(delegate() {
                bool run = true;

                while (run)
                {
                    TimeSpan elapsed = DateTime.Now.Subtract(start);

                    if (elapsed.Seconds > 5)
                    {
                        DialogResult res = MessageBox.Show("This operation could take a while, would you like to continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                        if (res == DialogResult.Yes)
                        {
                            run = false;
                        }
                        else
                        {
                            cancel = true;
                            run = false;                            
                        }
                    }

                    Thread.Sleep(100);
                }
            }));
            t.Start();


            try
            {
                foreach (ManagementObject share in searcher.Get())
                {
                    foreach (PropertyData PC in share.Properties)
                    {
                        if (cancel)
                        {
                            return;
                        }

                        if (PC.Value != null && PC.Value.ToString() != "")
                        {
                            grid.AddItem(new object[] { PC.Name, PC.Value, share["Name"].ToString() });
                        }
                        else
                        {
                            if (!DontInsertNull)
                                grid.AddItem(new object[] { PC.Name, "No Information available", share["Name"].ToString() });
                            else
                                continue;
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                t.Abort();
                Cursor.Current = Cursors.Default;

                MessageBox.Show("Data could not be loaded: " + exp.Message +"\n\n" +
                "Some data is unavailable in certain hardware or software configurations. " +
                "If you have any concerns please contact the manufacturer of the device you " +
                "were trying to obtain information for", exp.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (t.ThreadState != ThreadState.Aborted)
            {
                t.Abort();
            }

            Cursor.Current = Cursors.Default;
        }
示例#2
0
        private static void InsertInfoRun(string Key, GridEX grid, bool DontInsertNull)
        {
            Cursor.Current = Cursors.WaitCursor;
            grid.ClearItems();

            ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + Key);

            DateTime start = DateTime.Now;

            bool cancel = false;

            Thread t = new Thread(new ThreadStart(delegate() {
                bool run = true;

                while (run)
                {
                    TimeSpan elapsed = DateTime.Now.Subtract(start);

                    if (elapsed.Seconds > 5)
                    {
                        DialogResult res = MessageBox.Show("This operation could take a while, would you like to continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                        if (res == DialogResult.Yes)
                        {
                            run = false;
                        }
                        else
                        {
                            cancel = true;
                            run    = false;
                        }
                    }

                    Thread.Sleep(100);
                }
            }));

            t.Start();


            try
            {
                foreach (ManagementObject share in searcher.Get())
                {
                    foreach (PropertyData PC in share.Properties)
                    {
                        if (cancel)
                        {
                            return;
                        }

                        if (PC.Value != null && PC.Value.ToString() != "")
                        {
                            grid.AddItem(new object[] { PC.Name, PC.Value, share["Name"].ToString() });
                        }
                        else
                        {
                            if (!DontInsertNull)
                            {
                                grid.AddItem(new object[] { PC.Name, "No Information available", share["Name"].ToString() });
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                t.Abort();
                Cursor.Current = Cursors.Default;

                MessageBox.Show("Data could not be loaded: " + exp.Message + "\n\n" +
                                "Some data is unavailable in certain hardware or software configurations. " +
                                "If you have any concerns please contact the manufacturer of the device you " +
                                "were trying to obtain information for", exp.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (t.ThreadState != ThreadState.Aborted)
            {
                t.Abort();
            }

            Cursor.Current = Cursors.Default;
        }