我有如下的联系人列表。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
此列表中可以包含任意长度的数字。我想一一向这些号码发送消息,并在WinForm上向用户报告进度,所以我有一个这样的方法。
List<string> Numbers = new List<string>();
由于发送消息需要时间,因此我们选择使用BackgroundWorker来发送消息。现在这里的问题是,我们只希望在后台工作程序完成后才继续将消息发送到foreach循环中的下一个数字。
我已经实现
messageWorker_DoWork
&
messageWorker_RunWorkerCompleted
,我可以在messageWorker_RunWorkerCompleted方法中获取Number。我已经完成了研究,但找不到与此相关的任何内容。
答案 0 :(得分:1)
library(data.table)
library(magrittr)
# pattern to find matches
tmp <- data.table(1L, 0L, 0L, 0L, month = "")
# column 1 is the ID column
lapply(2:10, function(x)
{ # rename col names for join of subsequent columns
setnames(tmp, 1:4, names(yearly)[x:(x+3)])
# append starting month of sequence
tmp[, month := names(yearly)[x]]
# inner join
yearly[tmp, on = head(names(tmp), -1L), .(ID, month), nomatch = 0L]
}) %>%
# convert list to data.table
rbindlist() %>%
# reshape to wide format and append missing ID rows
dcast(ID ~ rowid(ID, prefix = "col")) %>%
.[yearly[, ID], on = "ID"]
DoWork
ID col1 col2
1: ABC April Sept
2: DEF May Sept
3: GHI Jun <NA>
4: MNO Sept <NA>
5: QAL <NA> <NA>
您的Progress和Completed事件处理程序将在主线程上执行。
(所有这些都没有被编译器检查)
答案 1 :(得分:-2)
使用类似以下代码的WaitHandle:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Threading;
namespace ConsoleApplication51
{
class Program
{
static AutoResetEvent block = new AutoResetEvent(false);
static void Main(string[] args)
{
block.Reset();
Test test = new Test();
block.WaitOne();
}
}
public class State
{
public string messsage = "";
}
public class Test
{
BackgroundWorker messageWorker = new BackgroundWorker();
static AutoResetEvent autoEvent = new AutoResetEvent(false);
List<int> Numbers = new List<int>() { 1,2,3,4,5};
public Test()
{
messageWorker.DoWork += new DoWorkEventHandler(messageWorker_DoWork);
messageWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(messageWorker_RunWorkerCompleted);
ProcessBroadcast();
}
public void ProcessBroadcast()
{
foreach (int number in Numbers)
{
//Send a Message here
autoEvent.Reset();
messageWorker.RunWorkerAsync(number);
autoEvent.WaitOne();
}
}
private void messageWorker_DoWork(object sender, DoWorkEventArgs e)
{
int? number = e.Argument as int?;
BackgroundWorker worker = sender as BackgroundWorker;
State state = new State() { messsage = "Done"};
}
private void messageWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
autoEvent.Set();
}
}
}