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);
}
}