Reputation: 24116
I am working on a small utility that will scan a FTP folder that has a folder for each printer on our network. Within each printer folder, there will be files that needs printing.
So, here's the test code I have so far:
namespace PrintTest
{
class Program
{
const string ftpDir = @"C:\Users\Latheesan\Desktop\FTP";
static void Main(string[] args)
{
StartScan();
Console.ReadKey();
}
static void StartScan()
{
// Scan ftp dir
var printerDirs = Directory.GetDirectories(ftpDir);
// Proceed if any printer dirs were found
if (printerDirs.Length > 0)
{
// Init task list
Task[] scanPrinterTasks = new Task[printerDirs.Length];
// Iterate through each found printer dir
for (int i = 0; i < printerDirs.Length; i++)
{
// Configure printer tasks
var printerName = printerDirs[i];
printerDirs[i] = Task.Factory.StartNew(() =>
ScanPrinterDir(printerName));
}
// Wait for all tasks to finish
Task.WaitAll(scanPrinterTasks);
}
}
static void ScanPrinterDir(string printerName)
{
// todo
}
}
}
When I try to compile this, I get the following error:
Severity Code Description Project File Line Error CS0029 Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'string' PrintTest C:\Users\Latheesan\Desktop\PrintTest\PrintTest\Program.cs 34
How do you pass parameters to a method invoked by Task.Factory.StartNew
?
Upvotes: 0
Views: 703
Reputation: 10236
There is nothing wrong in passing parameters here however the return type of Task.Factory.StartNew is task not a string which is why you are getting compile time error.
I think you need
scanPrinterTasks[i] = Task.Factory.StartNew(() =>
ScanPrinterDir(printerName));
instead of
printerDirs[i] = Task.Factory.StartNew(() =>
ScanPrinterDir(printerName));
Upvotes: 4