Socks Posted July 5, 2004 Posted July 5, 2004 I'm making a msn client usin the DotMSN library. Any form i create in an eventhandler for one of its events has an unresponsive ui. All other events work properly. Here is some code to better illustrate my problems: Sub ConversationCreated(ByVal sender As Messenger, ByVal e As ConversationEventArgs) Handles Msn.ConversationCreated frm_Convo = New Convo(e.Conversation) frm_Convo.Show() End Sub Here is the constructor of convo: Public Sub New(ByVal a As Conversation) MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call Conversation = a End Sub And some other code in convo: Sub MessageReceived(ByVal sender As Conversation, ByVal e As MessageEventArgs) Handles Conversation.MessageReceived Chat(e.Sender.Mail + ": " + e.Message.Text) Me.Refresh() End Sub chat() just appends a string to a listbox. This is what happens: 1. Msn.ConversationCreated is raised. The form is created, and it appears onscreen. However everything inside the form borders is white, the text in the titlebar has "(Not Responding)" appended to it, and when you drag a window in front of it you see trails (I'm sure you all know these symptoms as an unresponsive window). The rest of the program runs perfectly fine. 2. Conversation.MessageReceived is raised (by talking to myself using windows messenger). If I've got the form onscreen, it gets refreshed, and it looks normal, the message appears onscreen, if i put a breakpoint execution stops... So it's just the ui thats unresponsive. I've stepped through this with the debugger, from the debuggers point of view it all seems to be working properly. This happens with any form, I tried creating a random form, and putting her in an event handler, and that still happens. This also happens if i declare the form in a function called by the eventhandler. Does DotMSN hate me? Quote
Arch4ngel Posted July 5, 2004 Posted July 5, 2004 I know that Application.DoEvents() continue to render your form... but I don't know if it can help you. Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
Socks Posted July 5, 2004 Author Posted July 5, 2004 I found some more... It seems if i show() the form inside a event handler form, it will have this unresponsivness problem. I tried declaring a form (public frm_convo = new convo), then if i called show() during one of those events it had that problem, but if i wrote somewhere else before show() then hide() it would work perfectly. My guess is .net doesn't bother instantiating the form until its actually needed, and for some reason when you instantiate a form during an event raised by the dotmsn library it messes up for some reason... im thinking of creating some kind of pre-loaded convo forms to get around that, but that hardly seems very efficient, and its very inelegant... An unrelated question: Is there a difference between using Addhandler and Handles to declare event handlers? Quote
Arch4ngel Posted July 5, 2004 Posted July 5, 2004 No. AddHandler are dynamic event handler assignement while Handles is much more "static". Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
*Gurus* divil Posted July 7, 2004 *Gurus* Posted July 7, 2004 Is your event being fired on a different thread to the rest of your UI? All UI objects must be created on the same thread. To marshal between threads, see Control.Invoke. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Wile Posted July 9, 2004 Posted July 9, 2004 You can use the InvokeRequired property to check if you can directly use GUI commands, or if you have to invoke them as you are not in the GUI thread. Here is an example of using InvokeRequired and Invoke public void SetTitleBarText(string sTitleBarText) { //are we on the GUI thread? if (this.InvokeRequired) { //not on the GUI thread, Invoke to be called again but on the GUI thread this time. ProgressTitleUpdate delUpdate = new ProgressTitleUpdate(SetTitleBarText); //params in the invoke have to be put as object in an array object objText = (object) sTitleBarText; object[] arParams = {objText}; //call Invoke BeginInvoke(delUpdate, arParams); } else //if (this.InvokeRequired) ... { //now on the GUI thread it is possible to set the text. this.Text = sTitleBarText; } } Basicly the method calls Invoke to let the GUI thread call the same method again. Only on the GUI thread you can change the GUI. Quote Nothing is as illusive as 'the last bug'.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.