1

I know this sounds like a simple fix, but I've been at it for a few hours. This is my first attempt at multithreading, and I'm really at a loss as for what to do next.

Here is my code, and I'll just explain it briefly afterwards.

    public static void Main(string[] args)
    {
        Thread tid1 = new Thread(new ThreadStart(LoadRemoveForm.formCreate.create));
        Program @object = new Program();
        Thread thread = new Thread(new ThreadStart(@object.Watch));
        LoadRemoveForm RemovalForm = new LoadRemoveForm();
        RemovalForm.Show();
        thread.Start();
        tid1.Start();
        Program.game1.Run();
        thread.Abort();
        tid1.Abort();
    }

So, I have two threads being created at the start of the program. One isn't relevant to this. The one that is, is the second one, just titled "thread".

If I create my form inside of Watch(), it crashes. I have to do it outside, however, when I create the form "RemovalForm", none of my threads can access it and do RemovalForm.listBox.Items.Add("blah blah");

I have been looking for different solutions online, but none have helped. Is it possible to pass the form into the thread, or something of the sort?

Lauren Hinch
  • 340
  • 1
  • 11
  • 1
    The operating system only allows the thread that created the control (this includes forms) to modify it. If you need to touch a control from a different thread you have to use the `Invoke` or `BeginInvoke` method on the control to marshal the call to the correct thread, usually referred to as "The UI Thread". – Bradley Uffner Jan 28 '17 at 03:37
  • 2
    Possible duplicate of [Using thread to open form](http://stackoverflow.com/questions/19596091/using-thread-to-open-form) – OzizLK Jan 28 '17 at 03:38
  • You should also never, ever, call `Abort` on a thread. See https://stackoverflow.com/questions/1559255/whats-wrong-with-using-thread-abort for more information. – Bradley Uffner Jan 28 '17 at 03:40
  • @BradleyUffner I didn't write that part of the code, the abort is the last line of the code, and it only triggers AFTER the program has been closed anyways (it opens a game and closes when the game closes). – Lauren Hinch Jan 28 '17 at 04:53

0 Answers0