带变量的线程问题 [英] Threading question with variables

查看:90
本文介绍了带变量的线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#2010
我试图自己找出尽可能多的方法,因为我认为这是学习某些知识的最佳方法,而不是说这里是我的代码,请修复它.我想要做的(如果可能的话)是让多个线程在同一个进程上工作.但是,为了能够做到这一点,我需要将变量传递给该线程/进程.
我想做的是将一些文件写入磁盘,testfile1,testfile2,testfile3等.这在单个线程上运行正常,但是当您超过10个时,它开始变慢并变得引人注目.我的代码可以在单个线程中正常工作,当我尝试将其传播出去时,我遇到了问题.
我将继续尝试并四处搜索... ...在此先谢谢您!

I''m using C# 2010
I''m trying to figure out as much of this on my own as possible as I think that is the best way to learn something vs saying here is my code please fix it. What I''d like to be able to do (if possible) is to have multiple threads working on the same process. However in order to be able to do that I need to pass variables to that thread/process.
What I''m trying to do is write some files to disk, testfile1, testfile2, testfile3, etc... This runs ok on a single thread but as you get over 10 it starts to slow down and become noticable. I have the code working in a single thread fine, it''s when I try to spread it out that I''m running into problems.
I''ll keep trying things and searching around... thanks in advance!

推荐答案

在这里,您将找到一个完整的源代码,显示了如何执行此操作.您创建一个线程包装器,您可以在其中传递任何内容或传递任何内容并执行适当的同步:

如何将ref参数传递给线程 [ ^ ].

如果您不太清楚如何将技术应用于特定问题,请随时提出后续问题.

为了以防万一,请检查我的以往问题答案的链接集(如果有相关主题,则为以下内容):
如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].


基于或近期的讨论:请查看我过去关于线程的答案的链接集合,并查看我的建议:
如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

希望它们对您有用.

—SA
Here you will find a complete source code showing how to do it. You create a thread wrapper where you can pass anything in or out and perform proper synchronization:

How to pass ref parameter to the thread[^].

If this is not quite clear for you how to apply the techniques to your particular problem, feel free to ask a follow-up question.

Just in case, please check the collection of my links to my past answers to the question if related topics:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].


Based or recent discussion: please take a look at the collection of links to my past answers on threading and see my recommendations:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

Hope they are useful.

—SA


似乎这应该为您提供创建线程方法来写入文件的基础知识,然后为每个传递的文件启动一个单独的线程它在对象参数中需要什么.但是,我认为由于您的操作涉及到物理磁盘的IO,因此您可能会因驱动器处理写请求的能力而受到限制...?只是一个想法...祝你好运!

来自msdn ...

Seems this should give you the basics to create a thread method to write your file, then kick it off to create a seperate thread for each file passing what it needs in the object param. But, I think since your operation involved IO to physical disk, you may just run into a limitation based on the drives ability to handle write requests anyhow... ? Just a thought... good luck!

From msdn...

using System;
using System.Threading;

public class Work
{
    public static void Main()
    {
        // To start a thread using a shared thread procedure, use
        // the class name and method name when you create the
        // ParameterizedThreadStart delegate. C# infers the
        // appropriate delegate creation syntax:
        //    new ParameterizedThreadStart(Work.DoWork)
        //
        Thread newThread = new Thread(Work.DoWork);

        // Use the overload of the Start method that has a
        // parameter of type Object. You can create an object that
        // contains several pieces of data, or you can pass any
        // reference type or value type. The following code passes
        // the integer value 42.
        //
        newThread.Start(42);

        // To start a thread using an instance method for the thread
        // procedure, use the instance variable and method name when
        // you create the ParameterizedThreadStart delegate. C# infers
        // the appropriate delegate creation syntax:
        //    new ParameterizedThreadStart(w.DoMoreWork)
        //
        Work w = new Work();
        newThread = new Thread(w.DoMoreWork);

        // Pass an object containing data for the thread.
        //
        newThread.Start("The answer.");
    }

    public static void DoWork(object data)
    {
        Console.WriteLine("Static thread procedure. Data='{0}'",
            data);
    }

    public void DoMoreWork(object data)
    {
        Console.WriteLine("Instance thread procedure. Data='{0}'",
            data);
    }
}

/* This code example produces the following output (the order
   of the lines might vary):

Static thread procedure. Data='42'
Instance thread procedure. Data='The answer'
*/


很难说出您的进程是否已经在多个线程中运行.您声明它在单个线程中可以正常工作,但是在10个左右后变慢...这是否意味着您正在单个线程上连续创建10个文件,或者设法启动了10个线程,每个线程都创建了一个文件?
It''s hard to tell if you have already got your process running in multiple threads or not from what you''ve said. You state it works fine in a single thread, but slows down after 10 or so... does that mean you are creating 10 files in a row on a single thread, or have managed to kick off 10 threads, each creating a file?


这篇关于带变量的线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆