3

我正在编写一个基于小型表单的应用程序来连接到 LDAP 服务器,并且我希望“连接”按钮在后台工作。所以我在 这里关注信息和讨论

但无论出于何种原因,我的代码似乎无法正常工作:我在 'worker.RunWorkerAsync();' 处设置了一个断点 它只是一步步穿过它。

我究竟做错了什么?我在 Visual Studio 2010 中工作,以防万一。

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;
    }
}
}
4

4 回答 4

10

你永远不会把事件联系起来。

   public Form1()
    {
        InitializeComponent();
        worker.DoWork += new DoWorkEventHandler(worker_DoWork);
    }
于 2013-02-06T16:59:51.350 回答
6

你没有设置

worker.DoWork += new DoWorkEventHandler(worker_DoWork);

打电话之前worker.RunAsync()

于 2013-02-06T16:58:27.337 回答
1

RunWorkerAsync()启动工作线程并立即返回,因此调试器似乎“单步执行”。worker_DoWork()在方法中设置断点。

于 2013-02-06T16:58:26.390 回答
1

如果您仍然有事件并且无法正常工作,请尝试以下操作

打电话

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

在调用 RunWorkerAsync() 之前

于 2014-09-05T05:12:43.150 回答