I'm trying to be able to access some controls in my main form from an other thread.
Let's consider this picture:
I want to Instantiate that control (It's a panel in my case) into the second thread.
My problem is that i have found a LOT of answers that just modifies a control (Set the text of a textbox for instance) and not be able to read/write it's properties like it's an object. (Delegates and stuff)
My current code: (Not working because i've created the panel in the other thread)
public partial class Main : Form
{
Graphics g;
Thread drawCanvasThread;
int pos = 0;
public Main()
{
InitializeComponent();
}
private void Main_Load(object sender, EventArgs e)
{
g = canvas.CreateGraphics();
drawCanvasThread = new Thread(() => HandleCanvas(canvas));
drawCanvasThread.Start();
}
private void HandleCanvas(Panel objCanvas)
{
Panel canvas = objCanvas;
Point mousePos;
while(true)
{
mousePos = canvas.PointToClient(Cursor.Position);
//UPDATE CANVAS
//DRAW CANVAS
Thread.Sleep(17); //1000 / 17 ~~= 60
}
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
drawCanvasThread.Abort();
}
}
PS: The thread "How to update the GUI from another thread in C#?" doesn't really answers my question, because i want to read the object properties, and not only write. Though it's a very interesting thread.