-3

i'm new to winform and i can't figure out how to use the BackgroundWorker.

basically what i'm trying to do is this:

i have 1 form with 2 buttons: "Import" and "Exit" . when calling ImporButton_Click all it does is creates an HttpListener and listen to a given URL. ExitButton_Click closes the form.

the problem is when i press "Import" the form get stuck and it's in "not responding" status until someone is calling the listener and free it.

i'm trying to under stand how BackgroundWorker can help me to overcome that "stuck" problem. i don't understand how to invoke the backgroundWorker1_DoWork method

Here is my code so far:

//Program.cs 
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new ConfigurationForm());
    }
}

//ConfigurationForm.cs
public partial class ConfigurationForm : Form
{
    public ConfigurationForm()
    {
        InitializeComponent();
        UrlTextBox.Text = @"enter URL here";
    }

    private void ImporButton_Click(object sender, EventArgs e)
    {
        try
        {
            String urlToListen = UrlTextBox.Text;

            //Invoke MyListener 
            MyListener.StartListen(urlToListen); //assume this is implemented
        }
        catch (Exception exception)
        {
            string errorMsg = String.Format("An exception occured = {0}", exception);
            MessageBox.Show(errorMsg);
        }
    }

    private void ExitButton_Click(object sender, EventArgs e)
    {
        this.Close();
    }


    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
    }
}

so, what now ? how do i invoke the backgroundWorker1_DoWork ???

10x to anyone who can help

Dardar
  • 624
  • 3
  • 13
  • 30
  • You don't show the code where your real problem lies leaving as *"assume this is implemented"* :) For example , see [this](http://stackoverflow.com/questions/10017564/url-mapping-with-c-sharp-httplistener) or [this](http://stackoverflow.com/questions/13385633/serving-large-files-with-c-sharp-httplistener) – L.B Oct 10 '13 at 16:50
  • well , this is exactly my point. i don't know where to call the backgroundWorker1 from. – Dardar Oct 10 '13 at 18:06
  • You need a tutorial; there are already many on the web, try [MSDN](http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker%28v=vs.95%29.aspx). – Dour High Arch Oct 10 '13 at 19:07

1 Answers1

1

You need to call this method RunWorkerAsync

backgroundWorker1.RunWorkerAsync();

If you want to pass some argument into the handler DoWork via the DoWorkEventArgs, try the second overload of RunWorkerAsync:

backgroundWorker1.RunWorkerAsync(yourArgument);
King King
  • 61,710
  • 16
  • 105
  • 130
  • But where do i call it from ? from the ImporButton_Click() method ? should my MyListener.StartListen(urlToListen) logic be inside the RunWorkerAsync() ? – Dardar Oct 10 '13 at 18:08
  • @Dardar it depends on what you want your `BackgroundWorker` to do. Right after calling the method, your `BackgroundWorker` will do the thing you want in the `DoWork` event handler. – King King Oct 10 '13 at 18:12