Task Explanation

I need to clarify the code that I did not do, and I need to change it in the service.

Here are some parts of the service code

private Thread _thread;
private ConcurrentQueue<Task> _tasks = new ConcurrentQueue<Task>();
private Task _runningTask = null;

protected override void OnStart(string[] args)
{
    _thread = new Thread(WorkerThreadFunc);
    _thread.IsBackground = true;
    _thread.Start();
}

private void WorkerThreadFunc()
{
    InitDb();

    while (!_shutdownEvent.WaitOne(1000))
    {
        if (_runningTask == null || _runningTask.IsCompleted)
        {
            Task task;
            if (_tasks.TryDequeue(out task))
            {
                _runningTask = task;
                _runningTask.Start();
            }
        }
    }
}


private void RunReport(int reportID)
{
    var task = new Task(id =>
    {
        //Task code
    }, reportID);

    _tasks.Enqueue(task);
}

So it all works well

The fact is that I want to add other tasks to the task queue, but I do not have an identifier to give them (the task in the code launches the report and uses the reportID identifier, but other tasks are not related to one report, in particular).

Is there a way to create a task without specifying its ID (which I doubt), or is there something that I don't see completely?

+3
source share
2 answers

Is there a way to create a task without specifying its ID (which I doubt), or is there something that I don't see completely?

Yes, just use:

_tasks.Enqueue(new Task(() => {
       // Your code here
   });

, Task. , Task Task<T> - .

+3

, , , Reed, Task s. .

TPL DataFlow. .

+2

All Articles