Help with Invoke and Threading menu

Shout-Out

User Tag List

Results 1 to 4 of 4
  1. #1
    ReidE96's Avatar Archer Authenticator enabled
    Reputation
    470
    Join Date
    Dec 2006
    Posts
    1,625
    Thanks G/R
    1/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with Invoke and Threading

    Hey folks,

    So I decided to try to code in c# instead of vb. You know - using a real language. However, I've hit one wall. I'm building a chat client (IM type thing), which contains a loop that runs for as long as the client is connected. Naturally, that loop runs in a seperate thread. However, the loop is checking for new messages from the server. I've tried using invoke to call a method in the main thread to handle received messages, but it's not working right. Here's what I've got that's relevant.

    The invoke call:
    Code:
    this.Invoke(new UpdateLogCallback(UpdateLog), new object[] { srReceiver.ReadLine() });
    I also tried this for invoke:
    Code:
    txtLog.Invoke(new UpdateLogCallback(UpdateLog), new object[] { srReceiver.ReadLine() });
    Defined in the class:
    Code:
    private delegate void UpdateLogCallback(string strMessage);
    Code:
    private void UpdateLog(string strMessage)
    {
          txtLog.AppendText(strMessage + "\r\n");
    }
    Can anyone spot what I'm doing wrong? I've also tried with a static string, and have tried stepping through the code. The error comes from the invoke call, not its content, and reads thus:

    InvalidOperationException was caught:
    Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
    I'm using a try-catch block to stop the thing crashing, but whilst it connects (the server part (which just uses 1 thread because it doesn't need to do anything but listen after it starts doing so) reports the connection being made, and step through of client code confirms it happened) it doesn't get recorded in the textbox. Could someone help? I can post more code if needed.

    Help with Invoke and Threading
  2. #2
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems as though you're trying to invoke a window that isn't actually created. (NativeWindow does that)

    Honestly; I suggest just setting up an event, and catching it in your chat window form. Events are blocking, so keep that in mind.

  3. #3
    ReidE96's Avatar Archer Authenticator enabled
    Reputation
    470
    Join Date
    Dec 2006
    Posts
    1,625
    Thanks G/R
    1/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But it's trying to invoke the main form...window...thing as shown on program startup. Or that's what I meant for it to do, anyway.

    Would using an event be practical when they're sending output too? For the server app I didn't bother with the thread as all it has to do is listen for messages, but the client needs to take typed input and handle it too.

  4. #4
    ReidE96's Avatar Archer Authenticator enabled
    Reputation
    470
    Join Date
    Dec 2006
    Posts
    1,625
    Thanks G/R
    1/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I got one way that works. Created an instance of frmMain form and used it as the main one of the program, after some playing with Program.cs. Now it works

Similar Threads

  1. need help with morpher and teleporter
    By lolzz in forum World of Warcraft Emulator Servers
    Replies: 2
    Last Post: 07-25-2008, 08:21 PM
  2. need help with items and professions
    By greekkiller in forum World of Warcraft General
    Replies: 3
    Last Post: 06-16-2008, 02:41 PM
  3. help with chests and loot
    By backlash52 in forum World of Warcraft Emulator Servers
    Replies: 3
    Last Post: 06-13-2008, 10:40 AM
  4. help with mysql and navicat
    By Aes in forum World of Warcraft Emulator Servers
    Replies: 2
    Last Post: 04-17-2008, 06:29 AM
  5. Need help with website and server
    By Ukrajinc in forum World of Warcraft Emulator Servers
    Replies: 0
    Last Post: 12-26-2007, 08:41 PM
All times are GMT -5. The time now is 12:46 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search