Stumbling into cross-threading issues with winforms


Having very little prior UI programming experience, I ventured into writing an instant messenger application (using SQL Service Broker of course). After doodling around with VC# and generously locking shared data structures, I thought I had got it right. But when I started the application and attempted to send a message the receiving client threw an Illegal cross-threading exception. My reaction was “huh?” The textbox I was trying to update was under a lock. And I was certain this worked with .NET 1.1 (and Java Swing). But soon I searched on the web and realized this wasn’t possible with .NET 2.0. I’m sure they had reasons to prevent it and there are workarounds involving delegates and events. But by then I had lost interest in my chat program. Maybe I need to find out more about how Windows processes events using the message pump. The answer may be hidden deep there.


Comments (0)