none
Thread recommendations RRS feed

  • Question

  • Hi,

    What are your suggestions on how to thread this?

    foreach (String wg in WorkGroup)
                {
    Dictionary<String,String> DicWgComp = new Dictionary<String,String>();               
    int total = (100 / WorkGroup.Count);
                    DirectoryEntry DomainEntry = new DirectoryEntry(("WinNT://" + wg.Trim()));
                    try
                    {
                        DomainEntry.Children.SchemaFilter.Add("computer");
                        foreach (DirectoryEntry Machine in DomainEntry.Children)
                        {
                            if (DicWgComp.ContainsKey(wg)) DicWgComp[wg] = DicWgComp[wg] + "," + Machine.Name.Trim();
                            else DicWgComp.Add(wg, Machine.Name.Trim());
                        }
                    }
                    catch { MessageBox.Show("Problem!"); }
                    finally { DomainEntry.Close(); }
                }

    I want to report the progress as soon as it founds a computer.

    Monday, January 11, 2010 2:01 PM

Answers

  • hi,

    I suggest you to use BackgroundWorker.

    Create your procedure including this your and launch it with the DoWork event.

                BackgroundWorker bw = new BackgroundWorker();
                bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                bw.RunWorkerAsync(); //Start your thread
    
    
            }
            public void YourProcedure()
            {
                //Your code
            }
    
            void bw_DoWork(object sender, DoWorkEventArgs e)
            {
                YourProcedure();
            }


    Hope this helps,

    Mathieu
    Mathieu Francesch Sharplog Engineering
    • Edited by Mathieu Francesch Monday, January 11, 2010 2:18 PM
    • Marked as answer by batanyah Tuesday, January 12, 2010 4:05 PM
    Monday, January 11, 2010 2:15 PM

All replies

  • hi,

    I suggest you to use BackgroundWorker.

    Create your procedure including this your and launch it with the DoWork event.

                BackgroundWorker bw = new BackgroundWorker();
                bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                bw.RunWorkerAsync(); //Start your thread
    
    
            }
            public void YourProcedure()
            {
                //Your code
            }
    
            void bw_DoWork(object sender, DoWorkEventArgs e)
            {
                YourProcedure();
            }


    Hope this helps,

    Mathieu
    Mathieu Francesch Sharplog Engineering
    • Edited by Mathieu Francesch Monday, January 11, 2010 2:18 PM
    • Marked as answer by batanyah Tuesday, January 12, 2010 4:05 PM
    Monday, January 11, 2010 2:15 PM
  • I tried that but it takes too long. Any thoughts on why? or any other suggestions?
    Monday, January 11, 2010 2:17 PM
  • Hi batanyah,

    the problem comes from your code. Use thread doesn't solve your problem.

    Have you tried your code for just one computer for indentify which line is slow ?

    Mathieu
    Mathieu Francesch Sharplog Engineering
    Monday, January 11, 2010 8:42 PM