Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i am new in threading concept i applied threading concept in my code when i run logic i have two beigninvoke concept when i executed it pass two beigninvoke then it will get output or else it executed only one beigninvoke means it will not get ouput it will get error in WaitHandle[] because of another thread will be pass in null how can i solve this problem

C#
[WebMethod]
  public XmlDocument GIHotelInventory(string GIMonth, string GIYear, string username)
    {
        XmlDocument Ctxml = new XmlDocument();
        XmlDocument Mmtxml = new XmlDocument();
        XmlDocument xml = new XmlDocument();

        var dt = ProviderDetails(username);

        CtCaller ct = new CtCaller(*****Inventory);
        MmtCaller mmt = new MmtCaller(*****Inventory);
        IAsyncResult ctreslt = null;
        IAsyncResult mmtreslt = null;
        string ProviderUserName = string.Empty;
        string ProviderPassword = string.Empty;
        foreach (DataRow a in dt.Rows)
        {
            CollectionProviderDetails prd = new CollectionProviderDetails();
            prd.GiUserName = Convert.ToString(a["GiUserName"]);
            prd.ProviderName = Convert.ToString(a["ProviderName"]);
            ProviderUserName = Convert.ToString(a["PrUserName"]);
            ProviderPassword = Convert.ToString(a["PrPassword"]);

            if (prd.ProviderName == Portal.*****.ToString())
            {
                ctreslt = ct.BeginInvoke(GIMonth, GIYear, ProviderUserName, ProviderPassword, null, null);

            }

            else if (prd.ProviderName == Portal.*****.ToString())
            {
                mmtreslt = mmt.BeginInvoke(GIMonth, GIYear, null, null);
            }
        }
        WaitHandle[] handleArray = { ctreslt.AsyncWaitHandle, mmtreslt.AsyncWaitHandle };
        WaitHandle.WaitAll(handleArray);
        Ctxml = ct.EndInvoke(ctreslt);
        Mmtxml = mmt.EndInvoke(mmtreslt);
        String Allxml = "<inventorydetails>\n";
        string GIxml = "<inventorydetails>\n" + Ctxml.InnerXml + " " + Mmtxml.InnerXml + "</inventorydetails>";
        Allxml += "</inventorydetails>";
        xml.LoadXml(GIxml);

        return xml;
    }
Posted
Updated 29-Oct-15 1:40am
v2

1 solution

I think I understand, but please correct me if i'm wrong.

So the series of events happen like this:
ctreslt = null
mmtreslt = null

ct.begininvoke
[ does some stuff and eventually returns ctreslt ]

mmt.begininvoke
[ does some stuff and eventually returns mmtreslt ]

Unless they finish, there won't be an async result, so it is still null when you check them.

Here's what you need to do:

Create a couple of ManualResetEvents. Instantiate them as new ManualResetEvent(false) which is the default stopper.

pass these ManualResetEvents into the async methods

At the end of the async methods "set" the ManualResetEvent: mrw.set(). This will release the stoppers

in your main thread, have WaitHandler.WailAll({mre1,mre2}). This will wait for the sets to be set.

The AsyncResult types will still not be quite ready yes as the async methods need to return before they are instantiated.

I hope that helps ^_^
Andy
 
Share this answer
 
Comments
Member 12021941 29-Oct-15 8:16am    
sorry to say this i am new in threading concept so plz implement that in code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900