Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Creating an image control in a seperated thread

Creating an image control in a seperated thread

Scheduled Pinned Locked Moved WPF
csharpwpfdesigndata-structures
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Haim Nachum
    wrote on last edited by
    #1

    Hi. I have a wpf app that have a stack panel that i would like to populate with images that i'm downloading from a website. to avoid getting the UI from freezing while downloading the images i created another thread and in that thread downloaded the images and tried to create the image controls. but i'm getting this exception: "The calling thread must be STA, because many UI components require this." Here is the thread code: new Thread(delegate() { string searchUrl = siteAlgorithm.BuildSearchExpression(searchKeyWords); searcherObj.Clear(); searcherObj.LoadNewPage(siteAlgorithm.ExtractSearchResults(myWebClient.DownloadSiteToString(searchUrl))); MemoryStream stream; Image img = new Image(); // ---> Exception Thrown System.Windows.Media.Imaging.BitmapImage bitImg; foreach (SearchResult item in searcherObj.FirstPage.resultItems) { stream = new MemoryStream(myWebClient.DownloadData(item.imageUrl)); bitImg = new System.Windows.Media.Imaging.BitmapImage(); bitImg.BeginInit(); bitImg.StreamSource = stream; bitImg.EndInit(); img.Source = bitImg; Gui.AddSearchResultItem(img, item.info, item.uri); } } ).Start();

    C 1 Reply Last reply
    0
    • H Haim Nachum

      Hi. I have a wpf app that have a stack panel that i would like to populate with images that i'm downloading from a website. to avoid getting the UI from freezing while downloading the images i created another thread and in that thread downloaded the images and tried to create the image controls. but i'm getting this exception: "The calling thread must be STA, because many UI components require this." Here is the thread code: new Thread(delegate() { string searchUrl = siteAlgorithm.BuildSearchExpression(searchKeyWords); searcherObj.Clear(); searcherObj.LoadNewPage(siteAlgorithm.ExtractSearchResults(myWebClient.DownloadSiteToString(searchUrl))); MemoryStream stream; Image img = new Image(); // ---> Exception Thrown System.Windows.Media.Imaging.BitmapImage bitImg; foreach (SearchResult item in searcherObj.FirstPage.resultItems) { stream = new MemoryStream(myWebClient.DownloadData(item.imageUrl)); bitImg = new System.Windows.Media.Imaging.BitmapImage(); bitImg.BeginInit(); bitImg.StreamSource = stream; bitImg.EndInit(); img.Source = bitImg; Gui.AddSearchResultItem(img, item.info, item.uri); } } ).Start();

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You can't create controls from another thread. You CAN use the background worker class to download them, and it's work finished delegate runs on the main thread, as does the progress event, so you can use those to put your image into your page.

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups