using System;
using System.Threading;
using System.Windows.Forms;
namespace CancellationSource
{
internal delegate void DisplayMessage(string message);
internal static class Program
{
static void Main(string[] args)
{
CancellationTokenSource cts = new CancellationTokenSource();
ThreadPool.QueueUserWorkItem(o => Count(cts.Token, 50));
//ThreadPool.QueueUserWorkItem(o => Count(CancellationToken.None, 50));
Console.WriteLine("Press <enter> to cancel the operation.");
Console.ReadLine();
Action<Object> messageTarget;
messageTarget = Console.WriteLine;
cts.Token.Register(Console.WriteLine, null, true);
messageTarget = ShowWindowsMessage;
messageTarget ("This should print in box");
//This code opens a
//Message box only when the called thread operation is cancelled. This code calls two different methods
//of type Action<Object> when the spawned / requested thread is canceled.
//cts.Token.Register(messageTarget, null, true);
cts.Cancel();
Console.ReadLine();
}
//Notice in order to get the method below to execute upon thread cancelation, I had to make sure its
//its signature is one of Action<Object>
private static void ShowWindowsMessage(object value)
{
var message = (string)value;
MessageBox.Show(message);
}
private static void Count(CancellationToken token, Int32 countTo) {
for (int count = 0; count < countTo; count++)
{
if (token.IsCancellationRequested)
{
Console.WriteLine("Count is cancelled");
break;
}
Console.WriteLine(count);
Thread.Sleep(200);
}
Console.WriteLine("Count is done");
}
}
}
According to the content I am reading, I should be able to register a delegate of type Action to be executed upon cancelation, but the complier gives me and error stating: 'DisplayMessage' delegate can not be used in the current context. I can not tell what I am doing wrong. Here is the code commented in the area, which I get the error...
I updated the code, and while Console.Writeline works, calling a message box method gives me an error stating can not convert from method group to Action delegate. Here is the updated code:
edit: The last functionality I was after was to call an actual method, one that perhaps has slightly different signature from Action. The following modification kind of works, except it prints the Window twice. I do not get why it is doing that since I am copying every line from the relevant Console.WriteLine lines in my code. I realized that I had a type mismatch between the method I wrote and what Register method accepts which is Action. Now I am basically trying to figure out what I get two MessageBoxs when the method signatures are the same, and the code is the same.
Thank you for everyone's feedback. The following code works the way I wanted. It is commented, and I am posting it in case someone else likes to do something similar.
using System;
using System.Threading;
using System.Windows.Forms;
namespace CancellationSource
{
internal delegate void DisplayMessage(string message);
internal static class Program
{
static void Main(string[] args)
{
CancellationTokenSource cts = new CancellationTokenSource();
ThreadPool.QueueUserWorkItem(o => Count(cts.Token, 50));
//ThreadPool.QueueUserWorkItem(o => Count(CancellationToken.None, 50));
Console.WriteLine("Press <enter> to cancel the operation.");
Console.ReadLine();
Action<Object> messageTarget;
messageTarget = Console.WriteLine;
cts.Token.Register(Console.WriteLine, null, true);
messageTarget = ShowWindowsMessage;
messageTarget ("This should print in box");
//This code opens a
//Message box only when the called thread operation is cancelled. This code calls two different methods
//of type Action<Object> when the spawned / requested thread is canceled.
//cts.Token.Register(messageTarget, null, true);
cts.Cancel();
Console.ReadLine();
}
//Notice in order to get the method below to execute upon thread cancelation, I had to make sure its
//its signature is one of Action<Object>
private static void ShowWindowsMessage(object value)
{
var message = (string)value;
MessageBox.Show(message);
}
private static void Count(CancellationToken token, Int32 countTo) {
for (int count = 0; count < countTo; count++)
{
if (token.IsCancellationRequested)
{
Console.WriteLine("Count is cancelled");
break;
}
Console.WriteLine(count);
Thread.Sleep(200);
}
Console.WriteLine("Count is done");
}
}
}
...I just changed so that only the first column is read, and simplified the NA padding by observing that selecting a sequence that extends outside a character vector pads with NA automatically...
If you kept the old way of padding, you should at least pad with NA_character_ instead of NA to avoid unnecessary coercion.
I also index the KEYTERMS column by number instead of name (since there should be only one). I also changed sapply to vapply because I like it better :) - it actually is faster too.
Finally you said you wanted a data.frame. The last line produces that instead of a matrix.
假设file3是你想要的最终版本: file3 = merge(file1, file2, by = "ID")
file3 = file3[file3$END < file3$START, c("ID","END")]
Assuming that file3 is the final one you want: file3 = merge(file1, file2, by = "ID")
file3 = file3[file3$END < file3$START, c("ID","END")]
转换每个csv文件并重新保存到磁盘,然后使用命令行连接它们[...] 听起来像Transpose-Cat-Transpose。 使用粘贴来水平连接文件。 paste -d ',' a.csv b.csv c.csv ... > result.csv
[...] transposing each csv file and re-saving to disk, and then using the command line to concatenate them [...] Sounds like Trans ...
以下是我将如何使用dplyr来做到这dplyr library(dplyr)
newfoo <- foo %>%
group_by(A) %>%
mutate(D = sum(C[B != 4]),
E = C/D)
#newfoo # the resulting data.frame
#Source: local data frame [6 x 5]
#Groups: A
#
# A B C D E
#1 1 1 1 ...
将来请提供一个最小的工作示例。 这不是完全使用tm而是qdap,因为它更适合您的数据类型: library(qdap)
#create a fake data set (please do this in the future yourself)
dat <- data.frame(year=1945:(1945+10), summary=DATA$state)
## year summary
## 1 1945 Co ...