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 7889123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:00:55+00:00 2026-06-03T06:00:55+00:00

I have a class who – upon instantiation – creates a background thread that

  • 0

I have a class who – upon instantiation – creates a background thread that waits for work to do via System.Threading.AutoResetEvent. When work is available, the thread would pull an object from a generic Queue to work on and then call a Callback delegate from the object when finished.

The devil’s in the details, though: To do its work, the thread calls a generic function, so a Type needs to be known by the thread. I can define the objects in the queue to accept a generic, but how can I define a queue to accept an object that is truly generic, that is, its type could be anything? How could the background thread infer the type needed for its generic function call?

Example Code:

class GenericItem<T> 
{
  // ... some code ...
}

// somewhere else in code
Queue<GenericItem> myGenericListofItemsToWorkOn; 

Obviously C# doesn’t like the last line since I am trying to NOT specify a concrete type so I can do the following:

myGenericListofItemsToWorkOn.Enqueue( new GenericItem<string>() );
myGenericListofItemsToWorkOn.Enqueue( new GenericItem<int>() ); 

My thread then needs to do the following:

GenericItem obj = myGenericListofItemsToWorkOn.Dequeue(); // How can I reference a generic item here?
Library.Call<...>(...);  // How can I infer the generic type of the item dequeued here?
  • 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-03T06:00:56+00:00Added an answer on June 3, 2026 at 6:00 am

    You cannot do this. What you can do is have class GenericItem<T> implement a non-generic interface (or derive from a non-generic base class) that exposes enough functionality for you to work with. You would then be able to have

    Queue<ISomething> myGenericListofItemsToWorkOn;
    

    and when you pull items out of the queue ISomething should expose enough functionality for you to work on each item.

    Update: This is what would never work:

    Library.Call<X>(...);
    

    To make such a call the compiler needs to know what X is, either because you specify it explicitly or because it can infer it from the type of .... But since the list contains items of different types you can only tell what you pulled out of the bag at runtime, with GetType(). You can’t have your cake and eat it too; either the Queue must contain objects of only one type, or Library.Call must not be “strongly typed”.

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

Sidebar

Related Questions

i have bug that i cannot find, i have Class Point with method who
I have a class and subclasses who extends that. Like this: @Table @Entity class
i have Segment class who contain 2 sections , each section contain 2 point
I have a bunch of class who implement a common interface : Command. And
Here is my problem, I have a class which have a object who throw
Let's say I have an object who's class definition looks like: class Command: foo
I have class method that returns a list of employees that I can iterate
It is said that when we have a class Point and knows how to
.Net 3.5, using C# I have an instance of a class (A) that has
Let's say I have class A who inherits from class B and C (multiple

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.