Hack Forums
[C#] Get this error when using background worker - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Get this error when using background worker (/showthread.php?tid=5783135)



Get this error when using background worker - PabloEscarNark - Jan 11, 2018

ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.'

Code
string account = listBox1.Items[i].ToString();
                BackgroundWorker backgroundWorker;
                backgroundWorker = new BackgroundWorker();
                backgroundWorker.DoWork += new DoWorkEventHandler(CheckSpotify);
                backgroundWorker.RunWorkerAsync(account);
                Thread.Sleep(2000);


 private void CheckSpotify(object sender, DoWorkEventArgs e) {

           

            string account = e.Argument.ToString();

            string[] Account = account.Split(':');
            string user = Account[0].ToString();
            string pass = Account[1].ToString();
            WebBrowser w1 = new WebBrowser();

            

            w1.Navigate("https://accounts.spotify.com/en-GB/login?continue=https:%2F%2Fwww.spotify.com%2Fuk%2Faccount%2Foverview%2F");
            HtmlDocument doc = this.w1.Document;
            HtmlElement username = doc.GetElementById("login-username");
            HtmlElement password = doc.GetElementById("login-password");
            username.SetAttribute("value", user );
            password.SetAttribute("value", pass);
            username.Focus();
            SendKeys.SendWait("~");

        }



RE: Get this error when using background worker - ๖ۣۜDiViN3 - Jan 12, 2018

Example:
PHP Code

<?php 
Thread _thread 
= new Thread(()=> Foo());
_thread.SetApartmentState(ApartmentState.STA);
_thread.Start();

void Foo()
{ 
 
    // Insert code.
} 

Link: MSDN


RE: Get this error when using background worker - gigajew - Jan 12, 2018

You're approaching the account checking in a very inefficient way. I think you should forget about this problem and move onto sending POST requests manually.


RE: Get this error when using background worker - 0xDEAD10CC - Jan 12, 2018

Background threads are running on MTA threads while you need an STA thread for that component.