Question

I've written a small WCF WebService (.svc) which I want to call from other applications. It's working fine so far.

In another application I've got a Windows Form. When this is shown, it will call BackgroundWorker1 to get a list of objects from the WebService and show it to the user on it's completed1-method. So far, so good.

Now, after the user selects one object and clicks "ok", then BackgroundWorker2 will call the WebService again to gather more information. But here, within the doWork2-method exactly after the WebService-call, the whole application breaks without any exceptions or errors. It just closes directly after the WebService is called.

This is very weird, because as I have a look at the WebServices log files, it seems to work normal and still logs the successful operation AFTER the other application is closed. So the WebService cannot be the problem, I think.

Another weird thing: If I call the WebService the second time on the GUI thread (and not with BackgroundWorker2), it just works. It blocks the UI, but it works.

So, why is my application just "broken" after the second call without any notification? Any ideas are very much welcomed.

Thanks in advance.

Greets

Here's some simplified code. The application closes in "bgwGetSingleCar_DoWork":

public partial class MyForm : Form
{

    private Controller _ctrl { get; set; }
    private Config _config { get; set; }
    private List<Cars> _cars { get; set; }
    public bool Result { get; private set; }

    public MyForm(Controller ctrl, Config config)
    {
        this._ctrl = ctrl;
        this._config = config;
        this.Result = false;
        InitializeComponent();
    }

    private void MyForm_Load(object sender, EventArgs e)
    {
        try
        {
            this.bgwGetAllOffers.RunWorkerAsync(new WorkerInfo()
            {
                WorkerType = WorkerType.Type1,
                IdLocal = this._config.IdLocal,
                IdExternal = this._config.IdExternal,
            });
        }
        catch (Exception ex)
        {
            // ...
        }
    }

    private void FillList(List<Cars> list)
    {
        // ...
    }

    private void btnOk_Click(object sender, EventArgs e)
    {
        CarListItem v = (CarListItem)this.lstBoxCars.SelectedValue;
        this._config.IdExternal = v.IdExternal;

        try
        {
            this.bgwGetSingleCar.RunWorkerAsync(new WorkerInfo()
            {
                WorkerType = WorkerType.Type2,
                IdLocal = this._config.IdLocal,
                IdExternal = this._config.IdExternal,
            });
        }
        catch (Exception ex)
        {
            // ...
        }
    }

    private void bgwGetAllCars_DoWork(object sender, DoWorkEventArgs e)
    {
        try
        {
            WorkerInfo info = (WorkerInfo)e.Argument;
            Client client = new Client();
            GetCarsResult result = client.GetAllCars(new GetAllCarsRequest()
            {
                IdLocal = info.IdLocal,
                IdExternal = info.IdExternal
            });

            if (!result.Success)
            {
                // ...
            }

            if (result.Cars != null)
            {
                this._cars = result.Cars.ToList();
            }
        }
        catch (Exception ex)
        {
            /// ...
        }
    }

    private void bgwGetAllCars_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        this.FillList(this._cars);
    }

    private void bgwGetSingleCar_DoWork(object sender, DoWorkEventArgs e)
    {
        try
        {
            WorkerInfo info = (WorkerInfo)e.Argument;
            Client client = new Client();

            // Application closes after this line, but WebService log still shows     activity
            GetCarsResult result = client.GetSingleCar(new GetSingleCarRequest()
            {
                IdLocal = info.IdLocal,
                IdExternal = info.IdExternal
            });

            if (result.Success)
            {
                this.Result = true;
            }
        }
        catch (Exception ex)
        {
            /// ...
        }
    }

    private void bgwGetSingleOffer_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        this.DialogResult = System.Windows.Forms.DialogResult.OK;
    }
}

public class CarListItem : Car
{
    public override string ToString()
    {
        return ((DateTime)this.LastUpdate).ToString("dd.MM.yyyy - HH:mm");
    }
}
Was it helpful?

Solution

I just found my "design break". It was the button's "DialogResult" which caused the application to close too soon. But strange that the debugger didn't step ahead...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top