在一个类中多次运行一个方法? [英] Run a method in a class multiple times ?

查看:74
本文介绍了在一个类中多次运行一个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对音频进行代码转换的程序将任务提交给多线程服务,但是我的程序未执行任何繁重的任务,我有一个类和一种方法来提交转换任务.提交工作>等到它完成

我想多次运行此方法,而类值不会冲突

香港专业教育学院试图声明一个类的新实例,并使用这样的背景工作程序来运行它

My program which transcodes audio submits tasks to a multithreaded service but no intensive tasks are proformed by my program, i have a class and a method which submits the conversion task Extract the audio > Submit the job > Wait until its finished

I want to run this method multiple times without the class values conflicting

Ive tried declaring a new instance of the class and running it with a backgroundworker like this

new ConversionProcess(Files);
                    var Slot1 = new BackgroundWorker();
                    Slot1.DoWork += ConversionProcess.Convert;
                    Slot1.RunWorkerAsync();  



这适用于一个实例,但是如果我创建一个新实例并在另一个backgroundworker中运行它,则仅第一个实例运行.
有什么办法可以在c#
中执行此操作吗?
--class



This works for one instance but if i create a new instance and run it in another backgroundworker only the first instance runs.
Is there any way of doing this in c#

--class

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;

namespace AudioThreadingTest
{
    public class ConversionProcess
    {
        private static List<string> FileList;
        public static string AudioChannels;

        public ConversionProcess(List<string> ConstructorFileList)
        {
            FileList = ConstructorFileList;
        }

        public static void Convert(object Sender, DoWorkEventArgs DoWorkEventArgs)
        {
            foreach (var FileName in FileList)
            {
            new SourceHandler();
            new MP4Multiplexer();
            new ConversionLog();
            new JobEvaluate();
            new AACTranscoder();
            SourceHandler.GetStreamNames(FileName);
            SourceHandler.ExtractStreams(FileName);
            ConversionLog.AddToLog("Finished Extracting Source File.");
            AACTranscoder.CompressAudio(Stream);
            MP4Multiplexer.PackageMp4();
            }
        }
    }
}

推荐答案

你好

除了以下以外,我没有任何问题:
class不是"Threadsafe",因为这样是静态文件.您应该锁定FileList
请考虑有关Threadsafe的更多信息的链接: MSDN

而这个:
http://msdn.microsoft.com/en-us/library/dd997305.aspx [ ^ ]


并且:
什么是线程安全性"?

如果我不了解您的意图,请让我们进一步了解(对象Sender,DoWorkEventArgs DoWorkEventArgs)
Slot1.DoWork += ConversionProcess.Convert的处理程序方法是什么?
Hello

I haven''t gotten any problem except that:
The class is not "Threadsafe" since being static fileds like that. You shoud lock FileList
Please consider the link for more information about Threadsafe: MSDN

And this:
http://msdn.microsoft.com/en-us/library/dd997305.aspx[^]


And:
What is ''thread safty''?

If I haven''t understood your intention, then let''s know more about (object Sender, DoWorkEventArgs DoWorkEventArgs)
What is the handler method of Slot1.DoWork += ConversionProcess.Convert ?


如果我正确地理解了您的问题,我想您一次只想运行一个.我建议您从BackgroundWorker完成事件中运行下一个实例.您也可以在当前BackgroundWorker的末尾运行它,但这需要将运行BackgroundWorker的信息包括在传递给BackgroundWorkder的参数中.

另一个注意事项:在执行run方法时,请将BackgroundWorking所需的任何信息包含在要传递的对象中.
If I understand your question right, I think you only want to run one at time. What I would recommend is that you run the next instance from the BackgroundWorker completed event. You can also run it at the end of the current BackgroundWorker, but that would require including the information for running the BackgroundWorker to be included in the arguments passed to the BackgroundWorkder.

Another note: include any information needed by the BackgroundWorking in the object being passed when doing the run method.


这篇关于在一个类中多次运行一个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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