Deadlock with worker thread making a control visible

D

Douglas Peterson

Was able to reproduce this with a simple example (using .NET 1.1):

1) Create a new WindowsApplication project
2) Add a button to the form and set its Visible property to false
3) Double click on the form to add Form1_Load handler
3) Add the following code to Form1:

private delegate void DoAsync();
private void Form1_Load(object sender, System.EventArgs e)
{
new DoAsync(DoIt).BeginInvoke(null, null);
}
private void DoIt()
{
this.button1.Visible = true;
}

The main thread will lock up for about 5 minutes or more.
Can someone explain what's happening here to me?
I'm using this method to set all sorts of properties on all sorts of
controls, it's just the Visible property that's causing me grief.
How do I solve this?
In Win32 I might post a user message, can you do that in .NET?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You have to use Control.Invoke

A UI control cannot be accessed from other than the UI thread.
 
D

Douglas Peterson

Thank you!
Douglas

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

You have to use Control.Invoke

A UI control cannot be accessed from other than the UI thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top