-2

I have a console application that runs Excel and does some stuff inside a excellarch. Now I want to make two threads that go through these working states when they take text from one excel and place it in another.

So I wrote this code:

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);
        }
    }
}

Though when I try to make a new thread it tells me that it cant convert from void to system.threading.threadstart and I don't know what I'm doing wrong? I need new threads because I have more then one thing that needs to be done I need to multitask two methods and Threadpool as I understand it just ques the work to the existing threadpool

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

This is how you'd use a Task, given your code. However, note that this code cannot compile as you posted it, because ExcelConverter is not defined.

Nevertheless, this is how it should look:

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);
        }
    }
}

If the task returns a result, you simply create it as Task<T>.Run(() => ...)), where T is the result type. Then instead of waiting for the task to complete using .Wait(), you can access Task.Result to get the result - this will cause it to wait for the task to complete before accessing the result.

Here's the example above changed so that ThreadWorker() returns a string:

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";
        }
    }
}
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276