0

This is all part of a massive project, but essentially I have a welcome text box that I want to close.

The class is: public partial class Registration : Form

The method is: private void Register()

The line of code is: this.Close();

Why is this breaking? Do I just need to do a try/catch?

User
  • 23,729
  • 38
  • 124
  • 207
  • A stack trace would be good. Who is calling Register()? In what context is this happening? Has the form even been shown yet? – siride Sep 12 '16 at 03:18
  • 1
    In Ui the most probable reason is the attempt to access the UI control from different thread / Synchronization context except Ui, check that. Please provide more details / code to debug the issue further – Mrinal Kamboj Sep 12 '16 at 03:22
  • In search of the stack trace, I found a detail that said: `{"Cross-thread operation not valid: Control 'Registration' accessed from a thread other than the thread it was created on."}` – User Sep 12 '16 at 03:25
  • @MrinalKamboj given that the class definition inherits `Form`, wouldn't I always be in the right thread? Would changing the method to static help? – User Sep 12 '16 at 03:31
  • I was correct, stack trace says the same thing, What is happening is, point where you are calling `close` is part of some `thread pool thread`, which is not a `Ui thread`, hence error, options are: 1. Do that operation using `Async - Await`, then you have same `Ui context` available for update 2. Use `BackGroundWorker` to update the `Textbox` using the `Ui thread context` Cannot suggest anything beyond W/o looking at the actual code – Mrinal Kamboj Sep 12 '16 at 03:44
  • 1
    Possible duplicate of [Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on](http://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the) – rene Sep 12 '16 at 07:29

0 Answers0