Cross-Thread Operation Not Valid
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Cross-Thread Operation Not Valid

  1. #1
    Join Date
    Sep 2011
    Posts
    69

    Cross-Thread Operation Not Valid

    I am trying to run the following code in a thread but I keep getting this error for both listboxes:


    Cross-thread operation not valid


    Heres the code I am using:

    Code:
         
        try
        {
         
        for (int i = 0; i < listBox4.Items.Count; i++)
        {
        listBox4.SetSelected(i, true);
        listBox5.SetSelected(i, true);
        listBox4.SelectedItem.ToString();
         
        string[] details = { listBox4.SelectedItem.ToString(), listBox5.SelectedItem.ToString() };
         
        foreach (string element in details)
        {
         
         
        string postData = "";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("");
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = postData.Length;
        request.Method = "POST";
        request.KeepAlive = true;
         
         
        Stream response = request.GetRequestStream();
        byte[] postBytes = Encoding.ASCII.GetBytes(postData);
        response.Write(postBytes, 0, postBytes.Length);
         
         
         
         
        response.Close();
        response.Dispose();
         
        }
         
         
         
         
         
         
        }
         
         
        }
         
         
         
         
        catch (Exception ex)
        {
         
        MessageBox.Show(ex.Message);
         
        }
        }

    (I removed my postData and the site I am requesting too to protect my code form leechers)

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,196

    Re: Cross-Thread Operation Not Valid

    Quote Originally Posted by kytro360 View Post
    (I removed my postData and the site I am requesting too to protect my code form leechers)
    Here's a head's up....

    No one cares about code that can be found in any sample and that anyone can write.

    If you don't want to post code, fine, but there isn't any need to comment about 'leechers'. It's insulting to folks on this site that are trying to help.

    Now, to solve your problem, search bing or google for "Cross-thread operation not valid".

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center