-2

For some reason my windows form closes immediately after I run the program. I can see the form for about a second and then it closes. Here is my form load method

private void Form1_Load(object sender, EventArgs e)
    {
        CustGen = new CustomerGenerator(); 
        fuelType = null; 

        //set data on form initialization.
        unleadedtank = 10000f;
        dieseltank = 10000f;
        u_price = 136.9f;
        d_price = 152.2f;

        //event subscriptions

        CustGen.CustomerReady += CustomerReadySub; //Subscribes to ready event
        CustGen.PumpProgress += PumpProgressSub; //subscribes to progress event
        CustGen.PumpingFinished += PumpingFinishedSub; //subscribes to stop event

    }

and here is my program.cs for that particular form, although this is automatically generated I wasn't sure if it was needed.

        [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

This is within the customerGenerator class

  public class CustomerGenerator
{
    public CustomerGenerator();

    public event CustomerGenerator.CustomerReadyHandler CustomerReady;
    public event CustomerGenerator.PumpingFinishedHandler PumpingFinished;
    public event CustomerGenerator.PumpProgressHandler PumpProgress;

    public void ActivatePump();
    public void Start();

    public delegate void CustomerReadyHandler(object sender, CustomerReadyEventArgs e);

    public delegate void PumpingFinishedHandler(object sender, EventArgs e);

    public delegate void PumpProgressHandler(object sender, PumpProgressEventArgs e);
}

I have ran the program and came across this after carrying out what one of the users said below in the comments.

 public void CustomerReadySub(object sender, CustomerReadyEventArgs fuel)
    {
        string CustReady = null;

        //checks what fuel is chosen and then activates the pump
        fuelType = fuel.SelectedFuel.ToString();

        if (!String.IsNullOrEmpty(fuelType))
        {
            fTypeLabel.Text = fuelType;

It is this line that is throwing the exception. Also it says "Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on."

fuelType = fuel.SelectedFuel.ToString();

Thanks

DorkMonstuh
  • 819
  • 2
  • 19
  • 38

1 Answers1

1

Also it says "Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on."

Use the following pattern to access controls on your form:

private void MyHandler(object sender, EventArgs e) {
  if (InvokeRequired) Invoke(new EventHandler(MyHandler), sender, e);
  else {
    // code to handle the event
  }
}

When you listen to events generated from some objects that are executed on a thread, the code that handles the event will be run on that thread. Whenever you access UI objects from threads other than those that created those objects you will get an exception. InvokeRequired checks if you're running on the UI thread and if not the method is invoked to run on the UI thread. This allows you to safely access the controls.

Andreas
  • 6,447
  • 2
  • 34
  • 46
  • Hi Andreas, Thanks for the input, I was just wondering if I have done this right...If you look above at my original post I have a CustomerReadySub would I just change MyHandler to CustomerReadySub? – DorkMonstuh Apr 21 '13 at 23:41
  • Since you use custom event arguments `new EventHandler(MyHandler)` won't work and you have to use the appropriate delegate constructor e.g. `new EventHandler(CustomerReadySub), sender, customerReadyEventArgs);`. You can also cast the handler to an `Action`. – Andreas Apr 22 '13 at 07:24