我在c#中使用多线程的方式正确吗?

我在c#中使用多线程的方式正确吗?,c#,multithreading,C#,Multithreading,我有一个程序,运行多个线程将pdf文件转换为文件夹及其子目录中的图像。它遍历该文件夹,将所有pdf文件名放在一个列表中,然后使用该列表在我创建的4个线程之间划分工作。现在一切都很完美。多个线程同时运行并将pdf文件转换到我指定的不同位置 我只是想知道我是否用正确的方式做这件事。几乎我访问的每个网站都以不同的方式执行多线程,我不知道哪一个最有效,最终是正确的 做一些有效的事情是没有好处的,如果它做错了,我想。。。只是想在将来得到我 我希望你能看看这里的代码,看看是否有什么严重的错误,我需要改变关于

我有一个程序,运行多个线程将pdf文件转换为文件夹及其子目录中的图像。它遍历该文件夹,将所有pdf文件名放在一个列表中,然后使用该列表在我创建的4个线程之间划分工作。现在一切都很完美。多个线程同时运行并将pdf文件转换到我指定的不同位置

我只是想知道我是否用正确的方式做这件事。几乎我访问的每个网站都以不同的方式执行多线程,我不知道哪一个最有效,最终是正确的

做一些有效的事情是没有好处的,如果它做错了,我想。。。只是想在将来得到我

我希望你能看看这里的代码,看看是否有什么严重的错误,我需要改变关于多线程运行

static object LockInteger = new object();
static object LockIfCheck = new object();
static object LockInteger = new object();
static object LockIfCheck = new object();

private void button1_Click(object sender, EventArgs e)
{
    IterateThrough(txtboxdirectory.Text);
}

public void IterateThrough(string sourceDir)
{
    MulThread = new Thread(delegate()
    {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        int lowerbound = 0;
        int upperbound = (fileList.Count / 4);
        for (int i = lowerbound; i < upperbound; i++)
        {
            forFunction(exceptionFileList, fileList[i], compltetedFileList,sourceDir, dir1);
            stopWatch.Stop();
            lblFileExecutionTime.BeginInvoke(((Action)(() => lblFileExecutionTime.Text = "Execution Per File " + stopWatch.Elapsed.ToString())));
            stopWatch.Reset();
            stopWatch.Start();
        }
    });
    MulThread.Start();

    MulThread1 = new Thread(delegate()
    {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        int lowerbound = fileList.Count / 4;
        int upperbound = (fileList.Count / 4) * 2;
        for (int i = lowerbound; i < upperbound; i++)
        {
            forFunction(exceptionFileList, fileList[i], compltetedFileList, sourceDir, dir2);
            stopWatch.Stop();
            lblFileExecutionTime.BeginInvoke(((Action)(() => lblFileExecutionTime.Text = "Execution Per File " + stopWatch.Elapsed.ToString())));
            stopWatch.Reset();
            stopWatch.Start();
        }
    });
    MulThread1.Start();

    MulThread2 = new Thread(delegate()
    {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        int lowerbound = (fileList.Count / 4) * 2;
        int upperbound = (fileList.Count / 4) * 3;
        for (int i = lowerbound; i < upperbound; i++)
        {
            forFunction(exceptionFileList, fileList[i], compltetedFileList, sourceDir, dir3);
            stopWatch.Stop();
            lblFileExecutionTime.BeginInvoke(((Action)(() => lblFileExecutionTime.Text = "Execution Per File " + stopWatch.Elapsed.ToString())));
            stopWatch.Reset();
            stopWatch.Start();
        }
    });
    MulThread2.Start();

    MulThread3 = new Thread(delegate()
    {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        int lowerbound = (fileList.Count / 4) * 3;
        int upperbound;

        if (fileList.Count % 4 != 0)
        {
            upperbound = ((fileList.Count / 4) * 4) + (fileList.Count % 4) + 1;
        }
        else
        {
            upperbound = ((fileList.Count / 4) * 4) + (fileList.Count % 4);
        }

        for (int i = lowerbound; i < upperbound; i++)
        {
            forFunction(exceptionFileList, fileList[i], compltetedFileList, sourceDir, dir4);
            stopWatch.Stop();
            lblFileExecutionTime.BeginInvoke(((Action)(() => lblFileExecutionTime.Text = "Execution Per File " + stopWatch.Elapsed.ToString())));

            stopWatch.Reset();
            stopWatch.Start();
        }
    });
    MulThread3.Start();
}

我将接受来自此代码的任何建设性批评。

建议的多线程阅读方法是使用

ThreadPool的好处是它可以管理线程创建、作业分配,。。。并在减少资源使用的同时提供更好的性能

MSDN中的线程池示例:

using System;
using System.Threading;
public class Example {
    public static void Main() {

        // Queue the task.
        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));

        Thread.Sleep(1000);

        Console.WriteLine("Main thread exits.");
    }

    // This thread procedure performs the task. 
    static void ThreadProc(Object stateInfo) {

        // No state object was passed to QueueUserWorkItem, so  
        // stateInfo is null.
        Console.WriteLine("Hello from the thread pool.");
    }
}

建议的多流阅读方法是使用

ThreadPool的好处是它可以管理线程创建、作业分配,。。。并在减少资源使用的同时提供更好的性能

MSDN中的线程池示例:

using System;
using System.Threading;
public class Example {
    public static void Main() {

        // Queue the task.
        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));

        Thread.Sleep(1000);

        Console.WriteLine("Main thread exits.");
    }

    // This thread procedure performs the task. 
    static void ThreadProc(Object stateInfo) {

        // No state object was passed to QueueUserWorkItem, so  
        // stateInfo is null.
        Console.WriteLine("Hello from the thread pool.");
    }
}

我想答案是这要视情况而定。是的,你在开始线程吗。看起来您是在保护共享数据吗?是的。看起来到处都有复制粘贴的代码。它可以被抽象成一个函数,上下限可以被抽象成参数。我想你可以减少这个,使用Parallel.For或Parallel.ForEach。最后,我不太记得与GUI相关的多线程问题。我看到您在这里使用一些GUI。但是您需要确保对标签的更改是由GUI线程而不是其他线程进行的

我想答案是这要视情况而定。是的,你在开始线程吗。看起来您是在保护共享数据吗?是的。看起来到处都有复制粘贴的代码。它可以被抽象成一个函数,上下限可以被抽象成参数。我想你可以减少这个,使用Parallel.For或Parallel.ForEach。最后,我不太记得与GUI相关的多线程问题。我看到您在这里使用一些GUI。但是您需要确保对标签的更改是由GUI线程而不是其他线程进行的

你的问题可能更适合于谢谢你。我甚至都不知道网站的存在你的问题可能更适合于谢谢你。从来都不知道那个网站存在。因此,如果您想要创建多个线程,您可以在线程池中声明它们?当然,您可以像已经做的那样手动执行,但首选的方法是让CLR通过线程池处理您所需的线程。这两种方法都很有效。非常好,谢谢,我现在就试试,看看性能会提高多少。即使它没有增加,那也是正确的做法。欢迎。我喜欢别人使用多线程。Idk;-)啊,好吧。因此,如果您想要创建多个线程,您可以在线程池中声明它们?当然,您可以像已经做的那样手动执行,但首选的方法是让CLR通过线程池处理您所需的线程。这两种方法都很有效。非常好,谢谢,我现在就试试,看看性能会提高多少。即使它没有增加,那也是正确的做法。欢迎。我喜欢别人使用多线程。Idk;-)谢谢是的,除了GUI线程之外,其他线程对UI所做的更改确实会导致问题,这就是为什么如果我想更改某些内容,除了标签的setText或其他内容外,我还必须执行整个启动(操作)。从未使用过Parallel.For或Parallel.ForEach之前,必须阅读有关帽子的内容。谢谢。是的,除了GUI线程之外,其他线程对UI所做的更改确实会导致问题,这就是为什么如果我想更改某些内容,除了标签的setText或其他内容外,我还必须执行整个启动(操作)。从未使用过Parallel.For或Parallel.ForEach之前,将必须仔细阅读有关帽子的内容。