Thursday, March 8, 2007

Updating UI from non-UI thread

In .NET 2.0:
void vj_engine_VowelGenerated(PatternDataCLR patternData)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new VJEngineCLR_vowelGenerated(vj_engine_VowelGenerated), new object[] { patternData });
return;
}
}

In .NET 3.0:
http://www.codeproject.com/KB/cpp/SyncContextTutorial.aspx
using System.ComponentModel;
...
namespace MyNamespace
{
...
public class MyClass
{
AsyncOperation operation;
...

public MyClass()
{
operation = AsyncOperationManager.CreateOperation(null);
...
}

void nonUIThreadEvent(...)
{
operation.Post(new System.Threading.SendOrPostCallback(delegate(object state)
{
// do something
}), null);
}
}
}

No comments: