从 C# 中的不同类实例化线程方法

发布于 2024-12-27 18:32:02 字数 2418 浏览 0 评论 0原文

有人能指出我如何处理以下问题吗?基本上,我尝试重用以下示例中的代码: http://www.codeproject.com/KB/threads/SynchronizationContext.aspx 我唯一不明白的问题是,如果在不同的类中找到 RUN 方法,我如何实例化它。请看下面的代码。

public partial class Form1 : Form
{
public Form1()
{
    InitializeComponent();
}

private void mToolStripButtonThreads_Click(object sender, EventArgs e)
{
    // let's see the thread id
    int id = Thread.CurrentThread.ManagedThreadId;
    Trace.WriteLine("mToolStripButtonThreads_Click thread: " + id);

    // grab the sync context associated to this
    // thread (the UI thread), and save it in uiContext
    // note that this context is set by the UI thread
    // during Form creation (outside of your control)
    // also note, that not every thread has a sync context attached to it.
    SynchronizationContext uiContext = SynchronizationContext.Current;

    // create a thread and associate it to the run method
    Thread thread = new Thread(Run);

    // start the thread, and pass it the UI context,
    // so this thread will be able to update the UI
    // from within the thread
    thread.Start(uiContext);
}


// THIS METHOD SHOULD GO IN A DIFFERENT CLASS (CLASS2) SO HOW TO CALL METHOD UpdateUI()
private void Run(object state)
{
    // lets see the thread id
    int id = Thread.CurrentThread.ManagedThreadId;
    Trace.WriteLine("Run thread: " + id);

    // grab the context from the state
    SynchronizationContext uiContext = state as SynchronizationContext;

    for (int i = 0; i < 1000; i++)
    {
        // normally you would do some code here
        // to grab items from the database. or some long
        // computation
        Thread.Sleep(10);

        // use the ui context to execute the UpdateUI method,
        // this insure that the UpdateUI method will run on the UI thread.

        uiContext.Post(UpdateUI, "line " + i.ToString());
    }
}

/// <summary>
/// This method is executed on the main UI thread.
/// </summary>
private void UpdateUI(object state)
{
    int id = Thread.CurrentThread.ManagedThreadId;
    Trace.WriteLine("UpdateUI thread:" + id);
    string text = state as string;
    mListBox.Items.Add(text);
}
}

编辑:

例如,run 方法是由其他人(另一个开发人员)给我的,我需要在我的 UI 线程(主线程或类 Form1)中作为不同线程运行此方法,但是,每当我运行该线程(运行方法)我还需要使用 UpdateUI 方法更新 ListBox mListBox

Can someone point me how to deal with the following issue? Basically, im trying to reuse code from the following example found at:
http://www.codeproject.com/KB/threads/SynchronizationContext.aspx
the only issue i dont understand is how i can instantiate the RUN method if it is found in a different class. Please see following code.

public partial class Form1 : Form
{
public Form1()
{
    InitializeComponent();
}

private void mToolStripButtonThreads_Click(object sender, EventArgs e)
{
    // let's see the thread id
    int id = Thread.CurrentThread.ManagedThreadId;
    Trace.WriteLine("mToolStripButtonThreads_Click thread: " + id);

    // grab the sync context associated to this
    // thread (the UI thread), and save it in uiContext
    // note that this context is set by the UI thread
    // during Form creation (outside of your control)
    // also note, that not every thread has a sync context attached to it.
    SynchronizationContext uiContext = SynchronizationContext.Current;

    // create a thread and associate it to the run method
    Thread thread = new Thread(Run);

    // start the thread, and pass it the UI context,
    // so this thread will be able to update the UI
    // from within the thread
    thread.Start(uiContext);
}


// THIS METHOD SHOULD GO IN A DIFFERENT CLASS (CLASS2) SO HOW TO CALL METHOD UpdateUI()
private void Run(object state)
{
    // lets see the thread id
    int id = Thread.CurrentThread.ManagedThreadId;
    Trace.WriteLine("Run thread: " + id);

    // grab the context from the state
    SynchronizationContext uiContext = state as SynchronizationContext;

    for (int i = 0; i < 1000; i++)
    {
        // normally you would do some code here
        // to grab items from the database. or some long
        // computation
        Thread.Sleep(10);

        // use the ui context to execute the UpdateUI method,
        // this insure that the UpdateUI method will run on the UI thread.

        uiContext.Post(UpdateUI, "line " + i.ToString());
    }
}

/// <summary>
/// This method is executed on the main UI thread.
/// </summary>
private void UpdateUI(object state)
{
    int id = Thread.CurrentThread.ManagedThreadId;
    Trace.WriteLine("UpdateUI thread:" + id);
    string text = state as string;
    mListBox.Items.Add(text);
}
}

EDIT:

for instance, the run method is given me by someone else (another developer) and i need to run this method as a different thread in my UI thread (Main Thread or class Form1), however, whenever i run the thread (run method) i also need to update a ListBox mListBox using the UpdateUI method.

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

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

发布评论

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

评论(2

转瞬即逝 2025-01-03 18:32:02

如果您想在不实例化其父类的情况下执行它,则 Run 应该是静态的。此外,Run 应公开为公共,具体取决于您计划使用它的范围。例如

public static void Run(object state) { ... }

Run should be static if you want to execute it without instantiating it's parent class. Also, Run should be exposed as public, depending on the scope of how you plan on using it. e.g.

public static void Run(object state) { ... }
断肠人 2025-01-03 18:32:02

你想做这样的事情吗?

Foo foo = new Foo();
var thread = new Thread(foo.Run);

(这是假设您将 Run 更改为 public)

Are you wanting to do something like this?

Foo foo = new Foo();
var thread = new Thread(foo.Run);

(This is assuming you change Run to public)

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