Da quello che posso dire, ho delle informazioni fuorvianti. Ho bisogno di avere un thread separato in esecuzione in background.Come garantire che venga creato un nuovo thread quando si utilizza il metodo Task.StartNew
Al momento lo faccio in questo modo:
var task = Task.Factory.StartNew
(CheckFiles
, cancelCheckFile.Token
, TaskCreationOptions.LongRunning
, TaskScheduler.Default);//Check for files on another thread
private void CheckFiles()
{
while (!cancelCheckFile.Token.IsCancellationRequested)
{
//do stuff
}
}
Questo crea sempre un nuovo thread per me. Tuttavia, dopo diverse discussioni, anche se è contrassegnato come LongRunning non garantisce che verrà creato un nuovo thread.
In passato ho fatto qualcosa di simile:
thQueueChecker = new Thread(new ThreadStart(CheckQueue));
thQueueChecker.IsBackground = true;
thQueueChecker.Name = "CheckQueues" + DateTime.Now.Ticks.ToString();
thQueueChecker.Start();
private void CheckQueue()
{
while (!ProgramEnding)
{
//do stuff
}
}
Consiglieresti che torno a questo approccio per garantire un nuovo thread viene utilizzato?
'LongRunning' è solo un suggerimento per lo scheduler - se devi assolutamente avere sempre un nuovo Thread, dovrai crearne uno. –
Fancy aggiungendo che come risposta? – Jon
Grazie Jon :) –