Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared Variable between Watin ApartmentState.STA Thread and parent Thread?

Tags:

c#

watin

I'm trying to get Watin working in my SSIS script Task to do some automation by opening IE in a new thread, do something, find the final value and basically return/set that value in the parent thread.

So I have this for now:

public partial class TestWatin{

    public void Main()
    {     
        String finalValueFromWeb = "";            
        Thread runnerThread = new Thread(delegate() { getDAFValue(ref finalValueFromWeb ); });
        runnerThread.ApartmentState = ApartmentState.STA;
        runnerThread.Start();
        runnerThread.Join();
        MessageBox.Show(finalValueFromWeb);  

        //here i want to use the value of finalValueFromWeb to download a file
        //but if i try to access finalValueFromWeb the process would fail.
    }

    //do the Watin stuff here
    public  void findHiddenURL(String refObject)
    {
        //setup page controls, press search, grab the value of "hiddenURL"
        IE ie = new IE("some_webadress_to_go_to");
        ie.Visible = false;
        ie.SelectList("testID1").Option("Car").Select();
        ie.SelectList("testID2").Option("JAP").Select();
        ie.SelectList("testID3").Option("2012").Select();
        ie.Button("testSearch").Click();
        Link link = ie.Link("hiddenURL");
        ie.Close();

        //fails here?
        refObject = link.Url;            
    }
}

What I basically want to is for findHiddenURL() to find me a value which is a string containing some CSV url. I then want to use that string to download the CSV and process it.

The problem is when I try to set the value of finalValueFromWeb inside findHiddenURL() where the process fails. The Exception message says The Object Reference is not set to an instance of an object

Can someone please tell me how I should be going about this problem? What is the proper way of doing this type of thing? Thanks

like image 836
ke3pup Avatar asked May 02 '26 19:05

ke3pup


1 Answers

Make the variable a member of the class and try to lock it. You can use c# lock :

http://msdn.microsoft.com/en-us/library/c5kehkcz%28v=vs.71%29.aspx

protected string finalValueFromWeb ;

....

public void Main()
    {
        ...
        lock(finalValueFromWeb)
        {
            MessageBox.Show(finalValueFromWeb);  
        }
    }

 public  void findHiddenURL(String refObject)
    {
        ...
        lock(finalValueFromWeb)
        {
            finalValueFromWeb = link.Url;   
        }
    }
like image 146
abuseukk Avatar answered May 04 '26 09:05

abuseukk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!