Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8231787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:36:44+00:00 2026-06-07T17:36:44+00:00

In the code below, I am trying to execute a method, which returns a

  • 0

In the code below, I am trying to execute a method, which returns a value, in another thread. However, it just DOES NOT work!!!

    public void main()
    {
       lstChapters.DataContext = await TaskEx.WhenAll(LoadChapters());
    }



   //CAN'T use  async in this function, it requires Task<> which
   //Error appears on the code inside []

   public [async Task<object>] Convert(object[] values, Type targetType,
     object parameter, System.Globalization.CultureInfo culture)
   {
      dictChapters data = await IQ_LoadQuranXML.LoadChapters(TypeIndex);
   }




    internal static async Task< IEnumerable<dictChapters>> LoadChapters()
    {
        var element = XElement.Load("xml/chapters.xml");

        Task < IEnumerable < dictChapters >>  something = (Task<IEnumerable<dictChapters>>)  await TaskEx.Run(delegate
        {

        IEnumerable<dictChapters> Chapters =  
           from var in element.Descendants("chapter")
           orderby var.Attribute("index").Value
           select new dictChapters
           {
              ChapterIndex = Convert.ToInt32(var.Attribute("index").Value),
              ChapterArabicName = var.Attribute("name").Value,
              ChapterType = var.Attribute("type").Value,
           };
            return Chapters;}
        );

        return something; //An ERROR on this line
     }

    //Overriding method which does not return IEnumerable type. And it accepts index as integer.
    internal static dictChapters LoadChapters(string chIdx = "0")
    {
        int chIdxInt = Convert.ToInt32(chIdx);
        List<dictChapters> Chapters = (List<dictChapters>)  LoadChapters(); // ERROR is on this line too
        return Chapters.ElementAt(chIdxInt - 1); //index of chapter in the element starts from 0
    }

The Error is:

Cannot implicitly convert type ‘System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<iq_main.dictChapters>>‘ to ‘System.Collections.Generic.IEnumerable<iq_main.dictChapters>‘. An explicit conversion exists (are you missing a cast?)

And the Other error is..

Cannot convert type ‘System.Threading.Tasks.Task<System.Collections.Generic.List<iq_main.dictChapters>>‘ to ‘System.Collections.Generic.List<iq_main.dictChapters>

When I cast “something” explicitly like return (IEnumerable<dictChapters>) something then at runtime, I get “InvalidCastException”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:36:47+00:00Added an answer on June 7, 2026 at 5:36 pm

    Actually, you’ll be getting a runtime cast error earlier than that. The problem is your cast of the TaskEx.Run result. When you await something, the Task wrapper is removed.

    public void main()
    {
      lstChapters.DataContext = await LoadChapters();
    }
    
    internal static Task<List<dictChapters>> LoadChapters()
    {
      return TaskEx.Run(delegate
      {
        var element = XElement.Load("xml/chapters.xml");
        IEnumerable<dictChapters> Chapters =  
            from var in element.Descendants("chapter")
            orderby var.Attribute("index").Value
            select new dictChapters
            {
              ChapterIndex = Convert.ToInt32(var.Attribute("index").Value),
              ChapterArabicName = var.Attribute("name").Value,
              ChapterType = var.Attribute("type").Value,
            };
        return Chapters.ToList();
      });
    }
    

    There are a few other problems with your code as well. Remember that enumerations like this are lazily executed. You probably want to return Chapters.ToList(); so that the XML parsing happens on the thread pool thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the code below I am trying to clone a git repository to another
I was trying to execute the below code in my javascript and got some
I'm using the javascript code below trying to extract the number after gallery-entry_ in
In the code below i`m trying to read a list of selected projects and
Afternoon all, Using the code below I'm trying to load what is render by
The code below is what I am trying to use in order to get
I am trying to modify the code below to check the input from a
I am trying to run the code below but it keeps locking up my
I am trying to port the code below to mootools 1.1 code but am
I am trying to modify the sample code below. It currently populates a View

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.