线程控制台应用程序
Threading Console application
我有一个控制台应用程序运行 Excel 并在 excellarch 中做一些事情。现在我想创建两个线程,当它们从一个 excel 获取文本并将其放入另一个时,它们会经历这些工作状态。
所以我写了这段代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
namespace PrestaConverter
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("skriv in filvägen till Kategorifil");
string path = Console.ReadLine();
while(File.Exists(path) == false)
{
Console.WriteLine("fel filväg skriv in en ny");
path = Console.ReadLine();
}
Thread workThreadOne = new Thread(ThreadWorker(path));
}
static void ThreadWorker(string path)
{
ExcelConverter convert = new ExcelConverter();
convert.Convert(path);
}
}
}
虽然当我尝试创建一个新线程时它告诉我它不能从 void 转换为 system.threading.threadstart 而且我不知道我做错了什么?我需要新线程,因为我有不止一件事需要完成,我需要对两种方法和线程池进行多任务处理,据我所知,它只是将工作查询到现有的线程池
根据您的代码,这就是您使用任务的方式。但是,请注意,此代码无法像您发布的那样进行编译,因为未定义 ExcelConverter
。
尽管如此,它应该是这样的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
namespace PrestaConverter
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("skriv in filvägen till Kategorifil");
string path = Console.ReadLine();
while (File.Exists(path) == false)
{
Console.WriteLine("fel filväg skriv in en ny");
path = Console.ReadLine();
}
Task worker = Task.Run(() => ThreadWorker(path));
// Wait for task to complete.
Console.WriteLine("Waiting for background task to complete.");
worker.Wait();
Console.WriteLine("Background task has completed");
Console.ReadLine();
}
static void ThreadWorker(string path)
{
ExcelConverter convert = new ExcelConverter();
convert.Convert(path);
}
}
}
如果任务 returns 结果,您只需将其创建为 Task<T>.Run(() => ...))
,其中 T
是结果类型。然后,您可以访问 Task.Result
来获取结果,而不是使用 .Wait()
等待任务完成 - 这将导致它在访问结果之前等待任务完成。
上面的示例已更改,因此 ThreadWorker()
returns 一个字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
namespace PrestaConverter
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("skriv in filvägen till Kategorifil");
string path = Console.ReadLine();
while (File.Exists(path) == false)
{
Console.WriteLine("fel filväg skriv in en ny");
path = Console.ReadLine();
}
Task<string> worker = Task.Run(() => ThreadWorker(path));
// Wait for task to complete.
Console.WriteLine("Waiting for background task to complete.");
string result = worker.Result;
Console.WriteLine("Background task returned: " + result);
Console.WriteLine("Background task has completed");
Console.ReadLine();
}
static string ThreadWorker(string path)
{
ExcelConverter convert = new ExcelConverter();
convert.Convert(path);
return "This is the result";
}
}
}
我有一个控制台应用程序运行 Excel 并在 excellarch 中做一些事情。现在我想创建两个线程,当它们从一个 excel 获取文本并将其放入另一个时,它们会经历这些工作状态。
所以我写了这段代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
namespace PrestaConverter
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("skriv in filvägen till Kategorifil");
string path = Console.ReadLine();
while(File.Exists(path) == false)
{
Console.WriteLine("fel filväg skriv in en ny");
path = Console.ReadLine();
}
Thread workThreadOne = new Thread(ThreadWorker(path));
}
static void ThreadWorker(string path)
{
ExcelConverter convert = new ExcelConverter();
convert.Convert(path);
}
}
}
虽然当我尝试创建一个新线程时它告诉我它不能从 void 转换为 system.threading.threadstart 而且我不知道我做错了什么?我需要新线程,因为我有不止一件事需要完成,我需要对两种方法和线程池进行多任务处理,据我所知,它只是将工作查询到现有的线程池
根据您的代码,这就是您使用任务的方式。但是,请注意,此代码无法像您发布的那样进行编译,因为未定义 ExcelConverter
。
尽管如此,它应该是这样的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
namespace PrestaConverter
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("skriv in filvägen till Kategorifil");
string path = Console.ReadLine();
while (File.Exists(path) == false)
{
Console.WriteLine("fel filväg skriv in en ny");
path = Console.ReadLine();
}
Task worker = Task.Run(() => ThreadWorker(path));
// Wait for task to complete.
Console.WriteLine("Waiting for background task to complete.");
worker.Wait();
Console.WriteLine("Background task has completed");
Console.ReadLine();
}
static void ThreadWorker(string path)
{
ExcelConverter convert = new ExcelConverter();
convert.Convert(path);
}
}
}
如果任务 returns 结果,您只需将其创建为 Task<T>.Run(() => ...))
,其中 T
是结果类型。然后,您可以访问 Task.Result
来获取结果,而不是使用 .Wait()
等待任务完成 - 这将导致它在访问结果之前等待任务完成。
上面的示例已更改,因此 ThreadWorker()
returns 一个字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
namespace PrestaConverter
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("skriv in filvägen till Kategorifil");
string path = Console.ReadLine();
while (File.Exists(path) == false)
{
Console.WriteLine("fel filväg skriv in en ny");
path = Console.ReadLine();
}
Task<string> worker = Task.Run(() => ThreadWorker(path));
// Wait for task to complete.
Console.WriteLine("Waiting for background task to complete.");
string result = worker.Result;
Console.WriteLine("Background task returned: " + result);
Console.WriteLine("Background task has completed");
Console.ReadLine();
}
static string ThreadWorker(string path)
{
ExcelConverter convert = new ExcelConverter();
convert.Convert(path);
return "This is the result";
}
}
}