3

I am writing a small forms based application to connect to an LDAP server, and I wanted the "connect" button to work in the background. So I was following the information and discussion here

but for whatever reason my code doesn't appear to be working right: I set a breakpoint at 'worker.RunWorkerAsync();' And it just steps right through it.

What am I doing wrong? I am working in Visual Studio 2010, in case it matters.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
using System.Threading;

namespace ldapconnect
{


public partial class Form1 : Form
{
    private void Form1_Load(object sender, EventArgs e)
    {

    }

    public Form1()
    {
        InitializeComponent();
    }

    //server
    public string lds;
    //naming context
    public string root;

    public string username;
    public string password;

    BackgroundWorker worker = new BackgroundWorker();

    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        worker = sender as BackgroundWorker;
        foreach (string s in connect(worker, e, lds + "/" + root, txt_user.Text.ToString(), txt_pass.Text.ToString()))
        {
            rtb_results.Text += s + "\r\n";
        }
    }

    private List<string> connect(BackgroundWorker worker, DoWorkEventArgs e, String serv, string usr, string pass)
    {
        //Directory search code taking server path and creds passed in from form
        DirectoryEntry conn = new DirectoryEntry(serv, usr, pass);
        DirectorySearcher ds = new DirectorySearcher(conn);

        //I only want users
        ds.Filter = "objectClass=user";

        List<string> sendBack = new List<string>();

        try
        {
            SearchResultCollection results = ds.FindAll();

            foreach (SearchResult result in results)
            {
                sendBack.Add(result.ToString());                    
            }                
        }
        catch (Exception ex)
        {
            sendBack.Clear();
            sendBack.Add(ex.ToString());
        }

        return sendBack;
    }

    //connect button start background worker
    private void btn_connect_Click(object sender, EventArgs e)
    {
        worker.RunWorkerAsync();
    }

    //Exit Button
    private void btn_close_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    //set server path
    private void btn_server_Click(object sender, EventArgs e)
    {
        string serv = inputBox("ldap://", "IP or DNS Name of LDS Server", "");
        lds = serv;
        lbl_server.Text = lds;
    }

    //set default context
    private void btn_context_Click(object sender, EventArgs e)
    {
        string cntx = inputBox("In CN=,DC=,DC= Form:", "Default Naming Context", "");
        root = cntx;
        lbl_cntx.Text = root;
    }

    //VB interaction box
    private string inputBox(string a,string b,string c)
    {
        return Microsoft.VisualBasic.Interaction.InputBox(a, b, c);
    }

    private void btn_Defaults_Click(object sender, EventArgs e)
    {
        lds = "LDAP://127.0.0.1";
        root = "DC=USERS,DC=TEST,DC=LOCAL";
        txt_user.Text = "reader";
        txt_pass.Text = "password";
        lbl_server.Text = lds;
        lbl_cntx.Text = root;
    }
}
}
Community
  • 1
  • 1
jagallout
  • 43
  • 1
  • 1
  • 5
  • WARNING! You are accessing the UI thread from inside the background worker thread when getting and setting `TextBox` values. – Lloyd Feb 06 '13 at 17:00
  • Also note, if you are using .NET 4 you can dispense with `BackgroundWorker` and just use `Parallel.ForEach` judging by the code. – Lloyd Feb 06 '13 at 17:03
  • What's the point of this line: `worker = sender as BackgroundWorker;`? – Chris Dunaway Feb 06 '13 at 23:52
  • Chris, it was left over from a previous attempt. Thanks for pointing it out for the sake of the question! – jagallout Feb 07 '13 at 14:22

4 Answers4

10

You are never wiring up the event.

   public Form1()
    {
        InitializeComponent();
        worker.DoWork += new DoWorkEventHandler(worker_DoWork);
    }
Wonko the Sane
  • 10,623
  • 8
  • 67
  • 92
  • Thank you all for the info. And Wonko the Sane got it working. Thats what I get for jumping around forum posts trying to figure out a relatively high level concept... – jagallout Feb 07 '13 at 14:13
  • No problem. Don't forget that there are additional events you can wire up, including for "reporting progress" (which you can use for updating almost anything on the UI thread, not just progress), and for when the worker is complete. – Wonko the Sane Feb 07 '13 at 15:45
6

You have not set

worker.DoWork += new DoWorkEventHandler(worker_DoWork);

before calling worker.RunAsync()

paul
  • 21,653
  • 1
  • 53
  • 54
1

RunWorkerAsync() starts the worker thread and immediately returns thus the debugger seems to "step through it". Set a breakpoint in the worker_DoWork() method.

roryWoods
  • 4,798
  • 1
  • 15
  • 12
  • You are correct that `RunWorkerAsync` won't ever block, even when it's working, but his code won't work for the reason mentioned in all of the other answers. – Servy Feb 06 '13 at 17:55
  • Agreed, the event must be wired up first. – roryWoods Feb 06 '13 at 19:55
1

If u still have event and is not working, try the following

Just call

System.Windows.Forms.Application.DoEvents();

before calling RunWorkerAsync()

Sarath Subramanian
  • 20,027
  • 11
  • 82
  • 86