的WinForms - 如何访问UI线程/调用方法从一个单独的线程没有通过委托? [英] WinForms - How do I access/call methods in UI thread from a separate thread without passing a delegate?

查看:118
本文介绍了的WinForms - 如何访问UI线程/调用方法从一个单独的线程没有通过委托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

:在.NET 3.5中的WinForms应用程序,我该如何访问UI线程/调用方法从一个单独的线程,而不通过委托

QUESTION: In .NET 3.5 WinForms apps, how do I access/call methods in UI thread from a separate thread, without passing a delegate?

示例:说我有一些code我要同时运行(一​​)手动当用户通过它在运行的过程中点击一个按钮,和(b)定期调用独立非mainUI线程,但没有通过委托。 [简单而言,我认为,有这种方法的类是已建成,并在主UI线程都有它的句柄,因此,如果在单独的线程中运行的进程可能只是得到一个处理它从主UI线程它可以调用它。但愿这不是一个有缺陷的概念]

EXAMPLE: Say I have some code I want to run both (a) manually when the user clicks a button, and (b) periodically called by a process which is running in a separate non-mainUI thread but without passing a delegate. [Simplistically I'm thinking that the class that has this method is already been constructed, and the main UI thread has a handle to it, therefore if the process running in the separate thread could just get a handle to it from the main-UI thread it could call it. Hopefully this is not a flawed concept]

背景:我以后没有办法做到以上的在我的单独的进程线程实际上是我计划使用quartz.net作业的情况下,我其实。的方式调度工作,我似乎无法真正传递委托。有一种方式来传递JobDetails,但它似乎只迎合像串,整型,等等。因此我后一种方法来访问MainForm类为例,来调用一个方法就可以了,从内它运行在独立的线程quartz.net作业

BACKGROUND: I'm actually after a way to do the above for the case where my separate process thread is actually a job I schedule using quartz.net. The way the scheduler works I can't seem to actually pass in a delegate. There is a way to pass JobDetails, however it only seems to caters for things like string, int, etc. Hence what I'm after is a way to access the MainForm class for example, to call a method on it, from within the quartz.net job which runs in a separate thread.

感谢

推荐答案

假设你有机会到MainForm的,你可以简单地调用任何它的方法,但是这些方法随后将承担检查的负担,如果他们需要被整理到UI线程和处理委托路过那里。

Assuming you have access to the MainForm, you can simply call any of it's methods, but those methods will then bear the burden of checking if they need to be marshalled to the UI thread and handle the delegate passing there.

所以,在你的主要形式,你可以有一个方法:

So on your main form you could have a method:

public void UpdateProgress()
{
    if( this.InvokeRequired )
    {
        this.Invoke(UpdateProgress);
        return;
    }

    // do actual update of progress here
}

这篇关于的WinForms - 如何访问UI线程/调用方法从一个单独的线程没有通过委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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