1

I am working on a WPF application. I have used two “tasks” inside a button click event for parallel execution.

One task calls “Method1” which downloads files from a server. The second task calls “Method2”.

Method2 contains the following operations:

  1. Download 2 files from a server
  2. Parse & read the files' contents
  3. Scrape a site to fetch site details

In the scraping operation, we are creating a WebBrowser object to load the site's contents.

The issue is that - while executing the button_click event - I am getting the following error:

enter image description here

How can I solve this issue? Any help would be appreciated.

Thanks,

Ranish

Guillaume LaHaye
  • 339
  • 1
  • 16
Ranish
  • 877
  • 2
  • 10
  • 30
  • Are you doing method2 async? or in another thread? If you own the thread, you can change it's apartment state to STA. If you don't, then you may want to create your own for this reason. See this for a similar issue: http://stackoverflow.com/questions/4685237/how-can-i-make-a-background-worker-thread-set-to-single-thread-apartment – Simon Mourier Apr 01 '13 at 10:13
  • Yes, I am calling method 2 as Async, Task.Factory.StartNew(() => Method2()) – Ranish Apr 01 '13 at 10:27
  • possible duplicate of [How to create a task (TPL) running a STA thread?](http://stackoverflow.com/questions/5971686/how-to-create-a-task-tpl-running-a-sta-thread) – Simon Mourier Apr 01 '13 at 10:29

1 Answers1

0

WebBrowser is a control and can't be used in cross threading. My advice is using HttpWebRequest, HttpWebResponse and HTML Agility Pack library to scrap HTML instead

Doan Cuong
  • 2,594
  • 4
  • 22
  • 39