1

SynchronizationContext誰かが私のクラス内でThread.join()メソッドを設定する方法、またはクラスとthread.joinメソッドを処理するためのきちんとした方法があるかどうかを教えてもらえますか?基本的に、別のスレッド(UIスレッドではない)から2秒ごとにdatagridview(dgv)セルとプログレスバー(pb)を更新しようとしています。1つのスレッドがその仕事をするとき、機能はうまく機能します。ただし、最初のスレッド(スレッド1)がコントロールを更新するように2つのスレッドを設定したいと思います(私の場合、datagridviewを更新して10行を表示し、進行状況バーを50%に更新します)。スレッド1がジョブを完了するとすぐに、スレッド2が開始してコントロールを更新する必要があります(私の場合、データグリッドビューが更新され、さらに10行が表示され、進行状況バーが100%に更新されます)。以下のコードを参照してください。

using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;

namespace DelegatesAndCallback
{
public partial class Form1 : Form
{
    private Thread newThread1;
    private Thread newThread2;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        int id = Thread.CurrentThread.ManagedThreadId;
        Trace.WriteLine("Button thread "+id);

        SynchronizationContext uiContext = SynchronizationContext.Current;

        // thread #1
        startThread1(uiContext);

        // thread #2
        startThread2(uiContext);
    }

    public void startThread1(SynchronizationContext sc)
    {
        // thread #1
        newThread1 = new Thread(Process1) { Name = "Thread 1" };
        newThread1.Start(sc);
        //newThread1.Join();
    }

    public void startThread2(SynchronizationContext sc)
    {
        // thread #2
        newThread2 = new Thread(Process2) { Name = "Thread 2" };
        newThread2.Start(sc);
        //newThread2.Join();
    }

    public  void updateProgressBarValue(object state)
    {
        double val = Convert.ToDouble(state)/19.0;
        pb.Value = (int)(100*val);
    }

    public  void updateDataGridViewValue(object state)
    {
        dgv.Rows.Add((int) state, (int) state);
    }

    public void Process1(object state)
    {
        SynchronizationContext uiContext = state as SynchronizationContext;

        for (int i = 0; i < 10; i++)
        {
            uiContext.Send(updateDataGridViewValue, i);
            uiContext.Send(updateProgressBarValue, i);
            Thread.Sleep(2000);
        }
    }

    public void Process2(object state)
    {
        SynchronizationContext uiContext = state as SynchronizationContext;

        for (int i = 10; i < 20; i++)
        {
            if (uiContext != null) uiContext.Send(updateProgressBarValue, i);
            if (uiContext != null) uiContext.Send(updateDataGridViewValue, i);
            Thread.Sleep(2000);
        }
    }
}
}
4

2 に答える 2

2

Control.Invoke()を参照してください。これは、UI以外のスレッドがプログレスバーなどと相互作用できるように特別に設計されています。Send()この場合、Invokeを使用すると、同期コンテキストとそのメソッドの使用が置き換えられます。

少し関連したメモ:スレッドを作成するはるかに簡単な方法は次のとおりです。

new Thread(
  () => {
   /// insert code you want executed in a separate thread here...
  }
  ).Start();

更新 別のクラスのプログレスバーを更新する必要がある場合は、次のようにします。

public partial class Form1 : Form
{
    private ThreadOwner _threadOwner;

    public Form1()
    {
        InitializeComponent();
        var _threadOwner = new ThreadOwner();
        _threadOwner.StartAThread(this,progressBar1.Minimum,progressBar1.Maximum);
    }

    protected override void OnClosing(CancelEventArgs e)
    {
        _threadOwner.Exit();

        base.OnClosing(e);
    }

    internal void SetProgress(int progress)
    {
        if (progressBar1.InvokeRequired)
        {
            progressBar1.Invoke(
                new Action<Form1>(
                    (sender) => { 
                        SetProgress(progress); 
                    }
                    ),new[] { this }
                    );

        }
        else
            progressBar1.Value = progress;
    }
}

そして、ThreadOwnerクラス:

public class ThreadOwner
{
    private bool _done = false;

    public void StartAThread(Form1 form, int min, int max)
    {
        var progress = min;

        new Thread(() =>
            {
                while (!_done)
                {
                    form.SetProgress(progress);

                    if (progress++ > max)
                    {
                        progress = min;
                    }
                }

            }
        ).Start();
    }

    internal void Exit()
    {
        _done = true;
    }
}

要点は、スレッドにはフォームインスタンスへの参照が必要であり、プログレスバーを更新するメソッドを公開することです。そのメソッドは、更新が正しいスレッドで行われることを確認します。

于 2012-01-18T17:16:50.460 に答える
1

スレッドを同期するには、[手動|自動]ResetEventsを使用する必要があります。安全なコードを書くには、他のパターンを使用する必要があります。私のコードを調べてください:

public interface IProgress
{
    ManualResetEvent syncEvent { get; }
    void updateProgressBarValue(int state);
    void updateDataGridViewValue(int state);
}

public partial class Form1 : Form, IProgress
{
    // Sync object will be used to syncronize threads
    public ManualResetEvent syncEvent { get; private set; }

    public Form1()
    {
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Creeate sync object in unsignalled state
        syncEvent = new ManualResetEvent(false);

        // I like Async model to start background workers
        // That code will utilize threads from the thread pool
        ((Action<IProgress>)Process1).BeginInvoke(this, null, null);
        ((Action<IProgress>)Process2).BeginInvoke(this, null, null);
    }

    public void updateProgressBarValue(int state)
    {
        // InvokeRequired? -> Invoke pattern will prevent UI update from the non UI thread
        if (InvokeRequired)
        {
            // If current thread isn't UI method will invoke into UI thread itself
            Invoke((Action<int>)updateProgressBarValue, state);
            return;
        }

        double val = Convert.ToDouble(state) / 19.0;
        pb.Value = (int)(100 * val);
    }

    public void updateDataGridViewValue(int state)
    {
        if (InvokeRequired)
        {
            Invoke((Action<int>)updateDataGridViewValue, state);
            return;
        }

        dgv.Rows.Add((int)state, (int)state);
    }

    public void Process1(IProgress progress)
    {
        for (int i = 0; i < 10; i++)
        {
            // We have InvokeRequired in the methods and don't need any other code to invoke it in UI thread
            progress.updateDataGridViewValue(i);
            progress.updateProgressBarValue(i);
            Thread.Sleep(2000);
        }

        // When thread 1 complete its job we will set sync object to signalled state to wake up thread 2
        syncEvent.Set();
    }

    public void Process2(IProgress progress)
    {
        // Thread 2 will stop until sync object signalled
        syncEvent.WaitOne();

        for (int i = 10; i < 20; i++)
        {
            progress.updateProgressBarValue(i);
            progress.updateDataGridViewValue(i);
            Thread.Sleep(2000);
        }
    }
}

さまざまなクラスからUI更新を呼び出すようにコードが更新されました

于 2012-01-18T17:34:07.420 に答える