למשל ב-Task הזה שלי הוא מפעיל מתודה שמקבלת string ומחזירה SortedDictionary
קוד: בחירת הכל
Task task1= new Task(() => rec.GetFiles(new DirectoryInfo(path)));קוד: בחירת הכל
Task task1= new Task(() => rec.GetFiles(new DirectoryInfo(path)));קוד: בחירת הכל
class Test
{
public SortedDictionary<string, string> GetFiles(DirectoryInfo dir)
{
SortedDictionary<string, string> openWith = new SortedDictionary<string, string>();
openWith.Add("txt", "notepad.exe");
return openWith;
}
}
..............................
..............................
Test rec = new Test();
Task<SortedDictionary<string, string>> task1 = new Task<SortedDictionary<string, string>>(() => rec.GetFiles(new DirectoryInfo("c:\\")));
task1.Start();
task1.Wait();
SortedDictionary<string, string> retVal = task1.Result;
............
קוד: בחירת הכל
static void Main(string[] args)
{
Console.Write("enter path: ");
string path = Console.ReadLine();
while (path!="0")
{
try
{
recursion rec = new recursion(path);
Task<SortedDictionary<string, FileInfo>> getAllFilesTask = new Task<SortedDictionary<string, FileInfo>>(() => rec.GetFiles(new DirectoryInfo(path)));
getAllFilesTask.Start();
Console.Write("enter word to search or bypass by pressing *: ");
string word = Console.ReadLine();
if (word != "*")
{
Task fineWordTask = new Task(() => rec.FindMatchingNames(word));
fineWordTask.Start();
fineWordTask.Wait();
}
Console.Write("enter path: ");
path = Console.ReadLine();
}
catch (DirectoryNotFoundException)
{
Console.WriteLine("path not found, please try again!");
break;
}
}
Console.ReadLine();
}קוד: בחירת הכל
StringCollection strOutputStringArray = new StringCollection();
while (path!="0")
{
...............
Task fineWordTask = new Task(() => rec.FindMatchingNames(word, strOutputStringArray));
................
}
// Now you can print the strOutputStringArray collection.
} close Main method scope.
.....
class Recursion{
......
public void FindMatchingNames(string word, StringCollection strOutputStringArray)
{
.....
strOutputStringArray.Add(.....);
.....
}
}