Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 线程控制台应用程序_C#_Multithreading_Console Application - Fatal编程技术网

C# 线程控制台应用程序

C# 线程控制台应用程序,c#,multithreading,console-application,C#,Multithreading,Console Application,我有一个控制台应用程序,它运行Excel并在excellarch中执行一些操作。现在我想创建两个线程,当它们从一个excel中获取文本并将其放入另一个excel中时,它们将经历这些工作状态 所以我写了这段代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using Syste

我有一个控制台应用程序,它运行Excel并在excellarch中执行一些操作。现在我想创建两个线程,当它们从一个excel中获取文本并将其放入另一个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);
        }
    }
}

如果任务返回结果,只需将其创建为
task.Run(()=>…)
,其中
T
是结果类型。然后,您可以访问
task.Result
,而不是使用
.Wait()
等待任务完成以获取结果-这将导致它在访问结果之前等待任务完成

下面更改了上面的示例,以便
ThreadWorker()
返回字符串:

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";
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.IO;
使用系统线程;
名称空间前置转换器
{
班级计划
{
静态void Main(字符串[]参数)
{
Console.WriteLine(“在filvägen直到Kategorifil的skriv”);
字符串路径=Console.ReadLine();
while(File.Exists(path)==false)
{
Console.WriteLine(“纽约的fel filväg skriv”);
path=Console.ReadLine();
}
Task worker=Task.Run(()=>ThreadWorker(路径));
//等待任务完成。
WriteLine(“等待后台任务完成”);
字符串结果=worker.result;
Console.WriteLine(“后台任务返回:+结果”);
Console.WriteLine(“后台任务已完成”);
Console.ReadLine();
}
静态字符串ThreadWorker(字符串路径)
{
ExcelConverter convert=新的ExcelConverter();
convert.convert(路径);
返回“这是结果”;
}
}
}

为什么不使用
Task.Run()
??我用它作为参考,这基本上就是它告诉我要做的,我已经有四年没有用c语言编程了,但我一直在努力找回我的知识,所以请告诉我在这种情况下该怎么做这能回答你的问题吗?如果我使用它,它将不仅仅使用一个已经创建的线程吗?在这种情况下,我还有另一部分工作需要完成,以找到从任务中获取返回值的方法吗?@Jens是的,一秒钟后我将添加一个示例。