gpt4 book ai didi

c# - 向后台 worker 发送参数?

转载 作者:IT王子 更新时间:2023-10-29 03:32:52 25 4
gpt4 key购买 nike

假设我想向后台工作人员发送一个 int 参数,如何实现?

private void worker_DoWork(object sender, DoWorkEventArgs e) {

}

我知道这是什么时候 worker.RunWorkerAsync();,我不明白如何在 worker_DoWork 中定义它应该采用一个 int 参数。

最佳答案

你可以这样开始:

int value = 123;
bgw1.RunWorkerAsync(argument: value); // the int will be boxed

然后

private void worker_DoWork(object sender, DoWorkEventArgs e) 
{
int value = (int) e.Argument; // the 'argument' parameter resurfaces here

...

// and to transport a result back to the main thread
double result = 0.1 * value;
e.Result = result;
}


// the Completed handler should follow this pattern
// for Error and (optionally) Cancellation handling
private void worker_Completed(object sender, RunWorkerCompletedEventArgs e)
{
// check error, check cancel, then use result
if (e.Error != null)
{
// handle the error
}
else if (e.Cancelled)
{
// handle cancellation
}
else
{
double result = (double) e.Result;
// use it on the UI thread
}
// general cleanup code, runs when there was an error or not.
}

关于c# - 向后台 worker 发送参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4807152/

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