gpt4 book ai didi

c# - 跨线程交互C#

转载 作者:行者123 更新时间:2023-12-03 13:21:21 25 4
gpt4 key购买 nike

有人可以帮助我如何在我的类中设置Thread.join()方法,或者是否有一种巧妙的方法来处理SynchronizationContext类和thread.join方法。基本上,我试图从不同的线程(不是UI线程)每2秒更新一次datagridview(dgv)单元格和进度条(pb)。当一个线程完成这项工作时,该功能可以正常工作;但是,我想设置2个线程,以便第一个线程(线程1)将更新控件(在我的情况下,它将更新datagridview并显示10行,进度条将更新为50%)。线程1完成工作后,线程2应该启动并更新控件(在我的情况下,它将更新datagridview并显示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);
}
}
}
}

最佳答案

要同步线程,您应该使用[Manual | Auto] 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更新

关于c# - 跨线程交互C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8914325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com