tags:

views:

53

answers:

3

I have a small app that a client uses to manually update some data. It is very basic; on load it populates a list of companies the user can select from and click a button to update the data in another system. The client wants to be able to run this same functionality from command line so I have added a background worker. When the app is launched, I check for a specific arg and if found I kick off the worker. Once the worker completes I call Close();

This all seems to work fine (the data gets updated as expected and the app appears to close), but when I check task manager the app is still running.

My questions:

  1. What thing/things should I be looking for to find the cause of the app still running?
  2. Is there a better way to do this?

Here's a sample of the code:

public MainForm()
        {
            InitializeComponent();

            var args = Environment.GetCommandLineArgs();
            Array.ForEach(args, x => x.ToLower());

           if (args.Contains("u"))
                backgroundWorker.RunWorkerAsync();

        }
 private void BackgroundWorkerDoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
           //call to web service to update data
        }

 private void BackgroundWorkerRunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
           Close();
        }
A: 

Rather than calling Close(), try calling Application.Exit(). This will end the application rather than just closing the form.

Darvis Lombardo
Tried that. The app is still present in task manager.
Chuck
+3  A: 

2) Is there a better way to do this?

You could run the code from a Console App and be sure you have no WinForms/Threading problems.

As a variation on that, you can check the Commandline directly in Main() and not even execute the Application.Run(mainForm).

Henk Holterman
I'm refactoring to do just that now. I should be done in a minute.
Chuck
I like this solution much better than what I was doing! Thank you!
Chuck
A: 

The backgroundworker is in another thread than the UI thread.

Create a delegate for handling stuff (stopping in this case) in the UI thread. Example here

rdkleine
Not entirely correct. BackgroundWorker events are marshalled back onto the UI thread - he Close()'s the form in the BackgroundWorker event.
Adam
Hmm, now your mentioning it..
rdkleine