Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

i have an Async methode and i want to call it to make same traitment i have added in my action, my issue is whene i return view the methode stay runnig in background and the view is empty

What I have tried:

i have try,

public ActionResult loaddata()
{
Task task = new Task(WsdlCompareAsync);
task.Start();


return Json(new { data = myListDisp }, JsonRequestBehavior.AllowGet);
}
static List<tracking> myListDisp = new List<tracking>();

private async void WsdlCompareAsync()
{


string userName, token, scopePath, apiVersion, defaultCollectionUrl, firstEnvironmentToCompare, secondEnvironmentToCompare;
GetConfiguration(out userName, out token, out scopePath, out apiVersion, out defaultCollectionUrl, out firstEnvironmentToCompare, out secondEnvironmentToCompare);

WsdlDiff diff = new WsdlDiff(defaultCollectionUrl, userName, token, scopePath, apiVersion);
myListDisp= await diff.Compare(firstEnvironmentToCompare, secondEnvironmentToCompare);

}


the WsdlCompareAsync stay runing in backgroude i have add static liste to see if the methode work, whene i refresh the view cantain data
Posted
Updated 2-Jun-16 5:26am
Comments
John C Rayan 2-Jun-16 5:38am    
You have to use task.Wait() if you want to wait until it is finished. You could use async/await in this situation to make your application more responsive.
I can't see why you want to do in Task.

Alternatively you can use Ajax call from Jquery in View
Member 11573837 2-Jun-16 6:19am    
hi John,
thank you for answering me i have added task.Wait() and i have this exception (An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.)
and for the second solution are you proposing to me can you explain more plz
this is my code view

<script src="~/Scripts/jquery.dataTables.js"></script>
<script>
$(document).ready(function () {

$('#myTable').DataTable({
//"serverside":true,
"ajax": {
"url": "/DiffChecker/loaddata",
"type": "GET",
"datatype": "json"
//,
//"timeout": 10000

},
"columns": [
{ "data": "Name", "autoWidth": true },
{ "data": "HasChanged", "autoWidth": true },
{ "data": "ErrorMessage", "autoWidth": true },
{ "data": "DiffGram", "autoWidth": true }
]
});
});
</script>

<table class="table" id="myTable">
<thead>
<tr>
<th>Name</th>
<th>Etat</th>
<th>Error Message</th>
<th>DiffGram</th>

</tr>
</thead>
</table>
John C Rayan 2-Jun-16 9:10am    
Well! You don't need Task there. Remove the Task there first do everything synchronously. Because you are calling from Ajax you don't need it in server side too.
Member 11573837 2-Jun-16 9:21am    
but the CompareAsync methode it was async methode, i have change the CompareAsync to sync methode and still don't return data

public async Task<List<tracking>> CompareAsync(string prevEnv, string curEnv)
{
var extensionToFind = ".svc";

IEnumerable<value> listOFSvc = await TfsHelper.GetCurrentValuesFromExtensionAsync(_defaultCollectionUrl, _userName, _token, _scopePath, extensionToFind, _apiVersion);

foreach (var svc in listOFSvc)
{
ICompareResult result = InternalCompare(svc, prevEnv, curEnv);
if (result is SucceededCompare)
{
_rsltList.Add(new Tracking()
{
Name = svc.path,
DiffGram = (result as SucceededCompare).DiffGram,
Status = CompareStatus.Success,
HasChanged = !(result as SucceededCompare).IsIdentical
});
}
else
{
_rsltList.Add(new Tracking()
{
Name = svc.path,
DiffGram = "",
Status = CompareStatus.Fail,
ErrorMessage = (result as FailedCompare).Message
});
}
}

return _rsltList;
}
Philippe Mori 2-Jun-16 17:10pm    
Put your code in code block and properly indent to make it readable.

1 solution

You have to use in the following way. Let me know

C#
public async Task<ActionResult> loaddata()
{
List myListDisp = await WsdlCompareAsync();
return Json(new { data = myListDisp }, JsonRequestBehavior.AllowGet);
}


private async Task<List> WsdlCompareAsync()
{
string userName, token, scopePath, apiVersion, defaultCollectionUrl, firstEnvironmentToCompare, secondEnvironmentToCompare;
GetConfiguration(out userName, out token, out scopePath, out apiVersion, out defaultCollectionUrl, out firstEnvironmentToCompare, out secondEnvironmentToCompare);

WsdlDiff diff = new WsdlDiff(defaultCollectionUrl, userName, token, scopePath, apiVersion);
return await diff.Compare(firstEnvironmentToCompare, secondEnvironmentToCompare);

}
 
Share this answer
 
v4

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