Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SynchronizationContext.Post to UI方法-更新多个标签_C#_Multithreading_Lambda_Async Await - Fatal编程技术网

C# SynchronizationContext.Post to UI方法-更新多个标签

C# SynchronizationContext.Post to UI方法-更新多个标签,c#,multithreading,lambda,async-await,C#,Multithreading,Lambda,Async Await,在下面的代码段中,如何更新多个标签? 例如:在更新方法中有多个参数,而不是一个参数 private void UIupdate(string name) { var timenow = DateTime.Now; if((DateTime.Now-dt).Milliseconds<=50) return; synchronizationcontext.Post(new

在下面的代码段中,如何更新多个标签? 例如:在更新方法中有多个参数,而不是一个参数

 private void UIupdate(string name)
        {
            var timenow = DateTime.Now;
            if((DateTime.Now-dt).Milliseconds<=50)
                return;
            synchronizationcontext.Post(new SendOrPostCallback(o =>
            {
                
                lblFirstName.Text = "name" + (string)o;
                //lblLastName.Text = ?
                //lblZipCode.Text=?
            }),name );
            dt = timenow;
        }
private void UIupdate(字符串名称)
{
var timenow=DateTime.Now;
if((DateTime.Now dt).毫秒
{
lblFirstName.Text=“name”+(字符串)o;
//lblLastName.Text=?
//lblZipCode.Text=?
}),姓名);
dt=时间现在;
}

您只需在lambda中使用方法参数即可。参数在中捕获,并可用于方法的所有内部函数

private SynchronizationContext _synchronizationContext;
private Stopwatch _stopwatch = Stopwatch.StartNew();
private TimeSpan _lastUpdateTimestamp;

private void UIUpdate(string firstName, string lastName)
{
    TimeSpan timestamp = _stopwatch.Elapsed;
    if (timestamp < _lastUpdateTimestamp.Add(TimeSpan.FromMilliseconds(50))) return;
    _lastUpdateTimestamp = timestamp;

    _synchronizationContext.Post(_ =>
    {
        lblFirstName.Text = firstName;
        lblLastName.Text = lastName;
    }, null);
}
private SynchronizationContext\u SynchronizationContext;
私人秒表_Stopwatch=Stopwatch.StartNew();
私有时间跨度_lastUpdateTimestamp;
私有void UIUpdate(字符串名、字符串名)
{
TimeSpan时间戳=_stopwatch.appeased;
if(timestamp<\u lastUpdateTimestamp.Add(TimeSpan.fromMissicles(50)))返回;
_lastUpdateTimestamp=时间戳;
_synchronizationContext.Post(=>
{
lblFirstName.Text=firstName;
lblLastName.Text=lastName;
},空);
}

我还修正了你的时间测量逻辑。仅计算当前秒的毫秒数(值介于0和999之间)。您可能打算使用。但即使这样,您的测量也将取决于系统时钟的稳定性。系统时间可以通过用户操作或自动过程进行调整,因此不能信任它来测量间隔。

然后改为字符串post多个字符串(数组/列表)或一些自定义poco类主题:为什么使用
synchronizationcontext
而不是普通的异步/等待?为什么这个问题有WCF标签?你能发送一个代码样本吗?什么例子?取而代之的是
name
post
newstring[]{name,something,somethingelse}
然后将
o
转换为
string[]
并从数组中读取值并分配给右UI元素我删除了wcf标记非常感谢。