CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Cross Thread operation not valid

    This is new to me. I been playing a round with some libraries for an FTP server. It's just that the person who made it is from Japan.
    Any way I keep getting a,

    Cross Thread operation not valid control 'm_ListBoxMessages' accessed from a thread other then the thread it was created on.

    Well i check google.com again and found some fixes. It just I don't under stand what to do or how to do it.

    Here the code that calls it
    Code:
    Assemblies.Ftp.FtpServerMessageHandler.Message += new Assemblies.Ftp.FtpServerMessageHandler.MessageEventHandler(MessageHandler_Message);
    and here the code it calls.

    Code:
    private void MessageHandler_Message(int nId, string sMessage)
            {
                m_listBoxMessages.BeginUpdate();
    
                int nItem = m_listBoxMessages.Items.Add(string.Format("({0}) <{1}> {2}", nId, System.DateTime.Now, sMessage));
    
                if (m_listBoxMessages.Items.Count > 5000)
                {
                    m_listBoxMessages.Items.RemoveAt(0);
                }
    
                if (m_listBoxMessages.SelectedIndex < 0)
                {
                    m_listBoxMessages.TopIndex = nItem;
                }
                else if (m_listBoxMessages.SelectedIndex == nItem - 1)
                {
                    m_listBoxMessages.SelectedIndex = nItem;
                }
    
                m_listBoxMessages.EndUpdate();
            }
    can some one please tell me how to fix this. This is all new to me. This kind of programing is too complexed for me.

    Thanks
    Joe
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Cross Thread operation not valid

    You need to Invoke it to the main thread

    Code:
    public delegate void UpdateListbox(int nId, string sMessage);
    
    public void MessageHandler_Message(int nId, string sMessage)
    {
       if (m_listBoxMessages.InvokeRequired)
       {
          m_listBoxMessages.Invoke(new UpdateListbox(MessageHandler_Message), new object[] { int nId, string sMessage });
       } 
       else
       {
          // your original code here
       }
    }

  3. #3
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Re: Cross Thread operation not valid

    Thanks Danny, But I'm all new to this. Any chance I can talk you into setting it up like it should be. I just trying to under stand what your telling me to do and I'm sorry I don't.

    Code:
    public delegate void UpdateListbox(int nId, string sMessage);
    
    public void MessageHandler_Message(int nId, string sMessage)
    {
       if (m_listBoxMessages.InvokeRequired)
       {
          m_listBoxMessages.Invoke(new UpdateListbox(MessageHandler_Message), new object[] { int nId, string sMessage });
       } 
       else
       {
          // your original code here
    
    m_listBoxMessages.BeginUpdate();
    
                int nItem = m_listBoxMessages.Items.Add(string.Format("({0}) <{1}> {2}", nId, System.DateTime.Now, sMessage));
    
                if (m_listBoxMessages.Items.Count > 5000)
                {
                    m_listBoxMessages.Items.RemoveAt(0);
                }
    
                if (m_listBoxMessages.SelectedIndex < 0)
                {
                    m_listBoxMessages.TopIndex = nItem;
                }
                else if (m_listBoxMessages.SelectedIndex == nItem - 1)
                {
                    m_listBoxMessages.SelectedIndex = nItem;
                }
    
                m_listBoxMessages.EndUpdate();
       }
    }
    is this what you mean. And if so then what calls it.
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Cross Thread operation not valid

    Quote Originally Posted by bigjoe11a View Post
    is this what you mean. And if so then what calls it.
    Joe, this may sound flip (which I don't intend), but have you discovered how to set breakpoints and use the debugger? If not, please say so and we will explain it.

  5. #5
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Re: Cross Thread operation not valid

    Thanks ArJay and Yes I do. That's how I fount out where the code is crashing at. Did I also say that the code I'm working with now is not mine. Its a sample code for an FTP server that I been playing with. When I set the breakpoint on this line

    Code:
    m_listBoxMessages.BeginUpdate();
    That's where the error is at and it tells me what the error is. It just doesn't tell me how to fix it. It doesn't say what lines I need to add or change.

    Does that give you a better idea about whats going on. If I can't fix it. or can't find a answer to my question. Then I post and hope and pray that some one can help. I have posted before and never got one reply.

    Joe
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Cross Thread operation not valid

    Quote Originally Posted by bigjoe11a View Post
    That's where the error is at and it tells me what the error is. It just doesn't tell me how to fix it. It doesn't say what lines I need to add or change.
    What is the error? In general, when you get an error, post the error here.

  7. #7
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Re: Cross Thread operation not valid

    Quote Originally Posted by Arjay View Post
    What is the error? In general, when you get an error, post the error here.
    here the error that the breakpoints shows me

    Cross Thread operation not valid control 'm_ListBoxMessage' accessed from a thread other then the thread it was created on.

    and then it show me an help option below. I just didn't under stand what it was talking about

    How to Make Cross Thread Calls to windows form controls
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Cross Thread operation not valid

    Quote Originally Posted by bigjoe11a View Post
    here the error that the breakpoints shows me

    Cross Thread operation not valid control 'm_ListBoxMessage' accessed from a thread other then the thread it was created on.

    and then it show me an help option below. I just didn't under stand what it was talking about

    How to Make Cross Thread Calls to windows form controls
    Click on the help, download the sample (or copy the sample into a new project), compile the sample and use the debugger to step through the sample. This is how you can understand the sample enough that you can apply the sample in your code. Once you've done that, post back with specific questions on anything you don't understand.

    To get you started, here some threading background:

    When threading there is a basic concept that threads must be synchronized in order to access any resources shared between two or more threads. Actually it's a touch more complicated than this - any number of threads can be allowed read access, but only one thread can be allowed to write at a time. Typically a shared resource is protected against multiple thread access by a critical section or a mutex.

    What these do is lock out threads so only one thread has access to the resource at a time. Another approach to synchronization is to use a queue and have one thread service the queue and perform the 'access requests' for the other threads.

    .Net controls such as listboxes can be considered shared resources (across threads). As such, only thread can access the resource at a time. By default this thread is the thread that created the listbox. .Net uses the queue approach for synchronization and 'queues' up any change requests for a control via the Invoke mechanism.

  9. #9
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Re: Cross Thread operation not valid

    Arjay. If I knew how to do that I would have done so. and all I want is this sample to work so I can read threw the code and get a better under standing of how the program works.

    What your asking me to do is some thing a long way off. Since it was only parts of code and not an sample part of a program.

    The help section only gives very small samples and it doesn't relate to a program. That was I hate about there help section in VC#.

    All i want to do is get this sample code working so I can learn from it and mapbe apply it to a bigger program that I'm setting up.

    Joe
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Cross Thread operation not valid

    Since I don't see the actual help error, I can't see what was provided.

    At any rate, the text you included, "How to Make Cross Thread Calls to windows form controls" looks promising.

    Throwing that into google, yields the msdn topic:

    http://msdn.microsoft.com/en-us/library/ms171728.aspx

    Change the filter of the topic to only include C# code and you'll notice that there is enough code to build a sample.

    You create a forms project and put the code from the link into it.

    This is part of the learning process understanding how to interpret and use code samples.

    Try this, and if you run into trouble, ask specific questions.

  11. #11
    Join Date
    Apr 2009
    Location
    Ohio Summit County
    Posts
    114

    Re: Cross Thread operation not valid

    Quote Originally Posted by bigjoe11a View Post
    here the error that the breakpoints shows me

    Cross Thread operation not valid control 'm_ListBoxMessage' accessed from a thread other then the thread it was created on.

    and then it show me an help option below. I just didn't under stand what it was talking about

    How to Make Cross Thread Calls to windows form controls
    Arjay. You missed the Error help code. I'm reading it now. It the line right above these lines. Look here

    This is the answer I got from the help. and I'm reading it again.
    >>How to Make Cross Thread Calls to windows form controls
    and it still makes no since to me at all.
    ToppersBBS on the web - http://toppersbbs.dtdns.net
    Forums Section - http://toppersbbs.dtdns.net/smf

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Cross Thread operation not valid

    Quote Originally Posted by bigjoe11a View Post
    Arjay. You missed the Error help code. I'm reading it now. It the line right above these lines. Look here

    This is the answer I got from the help. and I'm reading it again.
    >>How to Make Cross Thread Calls to windows form controls
    and it still makes no since to me at all.
    Did you try what I suggested? I.e., did you filter so only C# code shows up in the link I provided and use the code to create a sample project?

    There's only so much we can do on this forum, if you don't try what we are pointing you to. So put the code into a sample, set some break points and try to understand what is going on. Don't just read the code, compile the code and step through it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured