如何在线程中运行方法并在线程退出时调用方法

发布于 2024-09-29 21:18:50 字数 579 浏览 3 评论 0原文

我一直在寻找这个问题的答案,但找不到一个简单的例子来帮助我理解。我正在用 C# 编写 win 表单应用程序,其中包含一堆用于视频编码的命令行工具。我有一个名为 encodefiles 的方法,我想在线程中运行此方法,以便 UI 仍然可以运行。一旦线程完成,我希望它调用另一个方法,这样我就知道它已经完成。然后我可以编码下一个文件或完成。一切都取决于是否还有更多文件需要编码。

当我只运行一个命令行工具时,我得到了我想要的操作,因为我正在使用可以将完成的事件附加到的进程对象。我想对一个线程做同样的事情,但不知道如何做。

简而言之,我希望能够使用线程而不是进程执行以下操作

Process myProcess = Process.Start(commandLineExe, commandlineSwitches);
myprocess.EnableRaisingEvents = true;
myprocess.Exited += new EventHandler(myprocess_exited);

希望这是有意义的

我使用 c# .NET 3.5 和 Visual Studio 2010

I have been searching for an answer to this but cannot find a simple example to help me understand. I am writing win form app in c# that wraps up a bunch of command line tools used for video encoding. I have a method called encodefiles i want to run this method in a thread so that the UI can still operate. Once the thread has finished I want it to call another method so i know it has finished. i can then encode the next file or finish. all depends if there are any more files to encode.

I was getting the action i wanted when i was only running a single command line tool as i was using the process object which you can attach a finished event to. I want to the same for a thread but have no idea how.

In short i want to be able to do the following with a thread instead of a process

Process myProcess = Process.Start(commandLineExe, commandlineSwitches);
myprocess.EnableRaisingEvents = true;
myprocess.Exited += new EventHandler(myprocess_exited);

Hope that makes sense

Am using c# .NET 3.5 with visual studio 2010

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最丧也最甜 2024-10-06 21:18:50

好吧,如果您使用 winforms 并且希望在后台运行任务并在其完成时发出通知,请使用 backgroundworker

并处理 DoWork()、WorkerComplete()、progressChanged() 事件以完成工作。

well if you are using winforms and you want to run a task in background and notify when its completed use backgroundworker

and handle the DoWork(), WorkerComplete(), progressChanged() events to get your work done.

鹤仙姿 2024-10-06 21:18:50

您是否查看过 BackgroundWorker 类?
它使您可以选择触发“进度更改”事件。

Did you take a look at the BackgroundWorker class?
It gives you the option to fire 'progress changed' events.

江心雾 2024-10-06 21:18:50

再创建一个线程。在它的线程过程中,执行以下操作:

yourMonitoredThread.Join();
CallYourProcedure(); //  can be event

编辑:

您可以从 Thread 派生并实现所需的事件并封装使用它。

Create one more thread. In the thread procedure for it, do:

yourMonitoredThread.Join();
CallYourProcedure(); //  can be event

EDIT:

You can derive from Thread and implement your needed event and use it encapsulated.

梦回梦里 2024-10-06 21:18:50

也许是这样的:

public class MyThread
{
    Thread _thread;

    public MyThread()
    { 
        _thread = new Thread(WorkerMethod);
    }

    public void Start()
    {
        _thread.Start();
    }

    private void WorkerMethod()
    {
        // do something useful
        // [...]

        //Exiting this method = exit thread => trigger event
        Exited(this, EventArgs.Empty);
    }

    public event EventHandler Exited = delegate{};
}

Maybe something like this:

public class MyThread
{
    Thread _thread;

    public MyThread()
    { 
        _thread = new Thread(WorkerMethod);
    }

    public void Start()
    {
        _thread.Start();
    }

    private void WorkerMethod()
    {
        // do something useful
        // [...]

        //Exiting this method = exit thread => trigger event
        Exited(this, EventArgs.Empty);
    }

    public event EventHandler Exited = delegate{};
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文