-2

First, I sorry for my bad Engish.I have just learned C# recently and I still don't know much. I am wiring a C# WinForm app and having trouble when I use 'async' and 'await' to change UI. The program is built successfully. When I debuged, I got this message at 'ActiveForm.Size = scrres.Size;'

An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code

Additional information: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.

I want to change the form size as same as the screen resolution. Thanks for reading.

using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RandomNoTHPTChuyenBT
{
    public partial class ldsc : Form
    {
        public ldsc()
        {
            InitializeComponent();
        }

        Rectangle scrres = Screen.PrimaryScreen.Bounds;
        public Rectangle scrresr
        {
            get
            {
                return scrres;
            }
        }
        private async void ldsc_Load(object sender, EventArgs e)
        {
            await Task.Run(() =>
            {
                if (scrres == new Rectangle(0, 0, 1366, 768))
                {
                    ActiveForm.Size = scrres.Size;
                    pictureBox2.Size = scrres.Size;
                }
                if (scrres == new Rectangle(0, 0, 1024, 768))
                {
                    ActiveForm.Size = scrres.Size;
                    pictureBox2.Size = scrres.Size;
                }
            });
            if (scrres == new Rectangle(0, 0, 1366, 768))
            {
                pictureBox1.Location = new Point(608, 310);
            }
            if (scrres == new Rectangle(0, 0, 1024, 768))
            {
                pictureBox1.Location = new Point(437, 309);
            }
       }
Liam
  • 27,717
  • 28
  • 128
  • 190
  • You can't alter the UI thread from a Task (another thread) – Liam Mar 14 '16 at 15:33
  • 3
    Possible duplicate of [Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on](http://stackoverflow.com/questions/10775367/cross-thread-operation-not-valid-control-textbox1-accessed-from-a-thread-othe) – Liam Mar 14 '16 at 15:33
  • Thanks for reply. But how I can change my form size without using another thread? – Gia Bảo Trương Mar 14 '16 at 15:36
  • 2
    By not using another thread?! Why do you think you need a thread to change the form size? – Liam Mar 14 '16 at 15:47
  • Well, I searched in Google and someone said that. So how I can change the form size? Please help. – Gia Bảo Trương Mar 14 '16 at 15:49
  • a simple google search results in plenty of answers, not least of which this [MSDN article](https://msdn.microsoft.com/en-us/library/ms229606(v=vs.110).aspx). BTW, Please read the [help]. comments shouldn't become extended discussions. Your question above is a duplicate, if you have another (different question) you should ask that separately, but **only after checking for duplicates first** – Liam Mar 14 '16 at 15:55

1 Answers1

3

You've wrapped your UI code in Task.Run. Don't do that. The whole point of Task.Run is to offload the work to another thread. You don't want to do that, you want to run it in the UI thread.

Once you've done that you have nothing left to await, so you can remove the async modifier from the method, as it is unnecessary.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • Thanks for reply. So I have to use 'delegate' instead? – Gia Bảo Trương Mar 14 '16 at 15:38
  • 1
    @GiaBảoTrương I see no reason for you to use a delegate. All you need to do is remove a bunch of code that you shouldn't have added in the first place. – Servy Mar 14 '16 at 15:40
  • When I remove async and await, I received another error. – Gia Bảo Trương Mar 14 '16 at 15:43
  • An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll Additional information: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on. – Gia Bảo Trương Mar 14 '16 at 15:44
  • 1
    @GiaBảoTrương Do not wrap the code in your `ldsc_Load` method in a `Task.Run(...)` but just run everything syncronously on the UI thread. – Yannick Meeus Mar 14 '16 at 15:45
  • Thanks for reply. If I don't but it in ldsc_load, where I can put it? Because I want that the form size is fixed with the sceen size so it must be done before the form is loaded. Please help. – Gia Bảo Trương Mar 14 '16 at 15:52