Domanda

Ho una pagina web che MyWebPage.aspx mentre il carico ha per visualizzare i dati provenienti da due webservices con il proprio algoritmo.

1) WebServiceI.SomeMethod() -> Takes 10 seconds aprx. to respond.
2) WebServiceII.SomeMethod() -> Takes 10 seconds aprx. to respond.
3) My Algorithm -> Takes 5 second aprx to respond.

Ora, quando chiamo questo modo sincrono, questo richiederà 10 + 10 + 5 = 25 secondi per il carico.

Quindi, mi è stato suggerito "Asynchronous metodo di chiamata", vale a dire. utilizzando IAsyncResult / AsyncCallback. Ora, quello che sarà (dovrebbe) accadere è che tutti saranno chiamati contemporaneamente e la pagina si caricherà in massimo 10 secondi.

Così chiamo loro ora nel modo "Begin / End" ...

public partial class MyWebPage : System.Web.UI.Page
{
    WebServiceI WebServiceIObject = new WebServiceI();
    WebServiceII WebServiceIIObject = new WebServiceII();

protected void Page_Load(object sender, EventArgs e)
{
    //BeginSomeMethod(AsyncCallback callback, object asyncState)[<- Method Signature]
    WebServiceIObject.BeginSomeMethod(OnEndGetWebServiceISomeMethodResult, null);


    //BeginSomeMethod(AsyncCallback callback, object asyncState)[<- Method Signature]
    WebServiceIIObject.BeginSomeMethod(OnEndGetWebServiceIISomeMethodResult, null);


/* My Algorithm 5 seconds*/
DataSet DS = GetDataSetFromSomeWhere();
MyGataGrid.DataSource = DS.tables[0];
MyGataGrid.DataBind();
/* My Algorithm 5 seconds*/


//System.Threading.Thread.Sleep(6000);
}

//Will be called after 10 seconds
void OnEndGetWebServiceISomeMethodResult(IAsyncResult asyncResult)
{
string WebServiceISomeMethodResult = WebServiceIObject.EndSomeMethod(asyncResult);
MyLabelI.Text = WebServiceISomeMethodResult;
//EventLog MyLog = new EventLog("Application"); MyLog.Source = "MySourceI";
//MyLog.WriteEntry(DateTime.Now.ToString());
}

//Will be called after 10 seconds
void OnEndGetWebServiceIISomeMethodResult(IAsyncResult asyncResult)
{
string WebServiceIISomeMethodResult = WebServiceIIObject.EndSomeMethod(asyncResult);
MyLabelII.Text = WebServiceIISomeMethodResult;
//EventLog MyLog = new EventLog("Application"); MyLog.Source = "MySourceII";
//MyLog.WriteEntry(DateTime.Now.ToString());
}
}

Ora il problema con l'esempio di cui sopra è che MyLabelI & Text MyLabelII non sono mai impostate perché la pagina viene caricata dopo 5 secondi

e filo è released.Both End metodi vengono chiamati correttamente come controllato scrivendo a EventLog. Come posso risolvere questo ... qualcosa come "Tutto inizia in una sola volta e poi tutti di aspettare fino a che tutti sono completi ..." Capisco che se la mia esecuzione di thread attende per 5 secondi più quindi viene eseguito il codice come richiesto ..

Come si usa AsyncWaitHandle ...

È stato utile?

Soluzione

Bene, la risposta a questo problema è "System.Web.UI.PageAsyncTask" class.It consente di effettuare chiamate asincrone a compiti e attende

completamento sugli stessi thread.Also più attività può essere creato e fatto funzionare parallel.Please passare attraverso la documentazione

Per ulteriori informazioni ... I lavori in Asp.Net 2.0 e Sopra Solo.

Per il nostro problema di cui sopra ... sto mettendo "il mio algoritmo", come la sincronizzazione ed entrambe le altre attività come asincrona parallel.So mia pagina avrà 10 + 5

= 15 secondi per caricare.

public partial class MyWebPage : System.Web.UI.Page
{
WebServiceI WebServiceIObject = new WebServiceI();
WebServiceII WebServiceIIObject = new WebServiceII();

protected void Page_Load(object sender, EventArgs e)
{
PageAsyncTask PAT_I = new PageAsyncTask
(BeginGetWebServiceISomeMethodResult, OnEndGetWebServiceISomeMethodResult, null, null, true);
Page.RegisterAsyncTask(PAT_I);

PageAsyncTask PAT_II = new PageAsyncTask
(BeginGetWebServiceIISomeMethodResult, OnEndGetWebServiceIISomeMethodResult, null, null, true);
Page.RegisterAsyncTask(PAT_II);

Page.ExecuteRegisteredAsyncTasks();

/* My Algorithm 5 seconds*/
DataSet DS = GetDataSetFromSomeWhere();
MyGataGrid.DataSource = DS.tables[0];
MyGataGrid.DataBind();
/* My Algorithm 5 seconds*/
}

IAsyncResult BeginGetWebServiceISomeMethodResult
    (object Sender, EventArgs EventArgsObject, 
    AsyncCallback AsyncCallbackObject, object PassAnythingExtraIfRequired)
{
    return WebServiceIObject.BeginSomeMethod(AsyncCallbackObject, PassAnythingExtraIfRequired);
}

 IAsyncResult BeginGetWebServiceIISomeMethodResult
    (object Sender, EventArgs EventArgsObject, 
    AsyncCallback AsyncCallbackObject, object PassAnythingExtraIfRequired)
{
    return WebServiceIIObject.BeginSomeMethod(AsyncCallbackObject, PassAnythingExtraIfRequired);
}

void OnEndGetWebServiceISomeMethodResult(IAsyncResult asyncResult)
{
string WebServiceISomeMethodResult = WebServiceIObject.EndSomeMethod(asyncResult);
MyLabelI.Text = WebServiceISomeMethodResult;
}

void OnEndGetWebServiceIISomeMethodResult(IAsyncResult asyncResult)
{
string WebServiceIISomeMethodResult = WebServiceIIObject.EndSomeMethod(asyncResult);
MyLabelII.Text = WebServiceIISomeMethodResult;
}
}

Opere :) Il codice può essere fatta generico con il refactoring comune ...

Si prega di essere un'attenta pianificazione mentre per questo tipo di design se ... Internamente questo deve essere anche utilizzando thread dal pool di thread

solo ... non ho scavato nel deep..but mosto be..So se i compiti capita di prendere moretime del previsto e se molti di questi compiti

get sollevato al tempo stesso, quindi del server web potrebbe soffrire e gli utenti potrebbero ottenere il tempo fuori ...

Sono ancora portare questo nonostante l'ora di sopra falla come i miei utenti saranno comunque in timeout se 25 secondi risulta essere 55

secondi ... meglio avere una situazione in cui alcuni utenti sono in grado di lavoro, piuttosto che nessuno ..

Se c'è qualche alternativa migliore, si prega di inviare.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top