过去的几天我一直在解决这个问题,没有一个运气好的解决方案,我看不到代码在哪里可能出错了,在后台工作人员中,我正在使用for循环来请求来自服务器的数据,它返回的很好,在我的测试中,它循环了5次,应做的是在循环的每次迭代中将数据追加到richTextBox,但我尝试过的一切都保持将数据写入richTextBox中:
代码:
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
string action = e.Argument as string;
if (action == "wraith_create_project")
{
StringBuilder sb = new StringBuilder();
// NEEDED FOR FORMAT PURPOSES //
var separator = Environment.NewLine;
const string gsaSeparator = "\x01";
// VARS //
var articleSource = "";
var articlekeywrd = "";
var title = "";
var body = "";
var hash = "";
var gsaArticleInfo = "";
var richTextBoxText = "";
// BREAK MAIN THREAD TO GET UI VALUES //
Invoke(new MethodInvoker(() => { articleSource = comboBoxArticleSources.Text; }));
Invoke(new MethodInvoker(() => { articlekeywrd = txtBoxScrapeKeyword.Text; }));
try
{
// BREAK THREAD UPDATE UI LOG //
Invoke(new MethodInvoker(() => { listBoxMain.Items.Add("[" + DateTime.Now + "] Building project ... " + articlekeywrd); }));
if (articleSource == "Internal Article Builder")
{
for (int x = 0; x <= 5; x++ ) {
// EVERY LOOP REQUESTS AN ARTICLE WHICH IS RETURNED AT RAND() //
var requestArticles = Helpers.getArticleTitleAndBodyInternalSpinner("https://www.thesite.com/api.php?articleBuilder=1&q=" + articlekeywrd.Replace(" ", "_"));
title = Helpers.internalSpinner(requestArticles.Item1); // SEND TO INTERNAL SPINNER FOR SPINNING ...
body = Helpers.internalSpinner(requestArticles.Item2); // SEND TO INTERNAL SPINNER FOR SPINNING ...
hash = To32BitFnv1aHash(body).ToString("X8");
// NEED A SMALL 5 SECOND SLEEP TO BE GOOD TO THE SERVER //
System.Threading.Thread.Sleep(5000);
Invoke(new MethodInvoker(() =>
{
listBoxMain.Items.Add("[" + DateTime.Now + "] Returned article ... " + requestArticles.Item1);
listBoxMain.Items.Add("[" + DateTime.Now + "] Spun the article ... " + title);
listBoxMain.Items.Add("[" + DateTime.Now + "] Pausing 5 seconds ... ");
}));
// ENCODE WITH THE GSA SEPERATOR BETWEEN EACH FIELD //
gsaArticleInfo = title + gsaSeparator + "%first_paragraph-article%" + gsaSeparator + body + gsaSeparator + hash;
// ADD TO THE RICHTEXTBOX ALL FIELDS FROM ABOVE //
richTextBoxText = string.Join(separator, gsaArticleInfo);
// ADD THE RETURNED AND SPUN ARTICLES TO THE RICHTEXTBOX //
Invoke(new MethodInvoker(() => { richTxtBoxArticle.Text += richTextBoxText; }));
// TESTING //
File.WriteAllText(@"debug.txt", richTextBoxText);
} // End for loop.
} else if (articleSource == "") {
// RESERVED FOR ADDITIONAL SOURCES //
}
} catch (Exception ex) {
Helpers.returnMessage(ex.ToString());
}
}
}
更新的功能:
public static Tuple<string, string> getArticleTitleAndBodyInternalSpinner(string url)
{
// SETUP HTML VAR AND TUPLE //
string page = null;
Tuple<string, string> result = null;
// TRY/CATCH //
try
{
using (WebClient wc = new WebClient())
{
// THE WEBPAGE //
page = wc.DownloadString(url);
}
var articleT = "";
var articleB = "";
MatchCollection aTitle = Regex.Matches(page, @"<span class='articleTitle'>(.*?)</span>", RegexOptions.Singleline);
var post1 = "";
foreach (Match aTitleMatch in aTitle) {
post1 = aTitleMatch.Groups[1].Value;
}
articleT = post1;
MatchCollection aBody = Regex.Matches(page, @"<span class='articleBody'>(.*?)</span>", RegexOptions.Singleline);
var post2 = "";
foreach (Match aBodyMatch in aBody)
{
post2 = aBodyMatch.Groups[1].Value;
}
articleB = post2;
// ADD THE ARTICLE TITLE AND BODY TO THE TUBLE //
result = Tuple.Create(articleT, articleB);
} catch (Exception ex) {
returnMessage("INTERNAL ARTICLE BUILDER: \n\n" + ex.ToString());
}
return result;
}
我尝试在循环中写入.txt文件(它也不断被覆盖),只是不会追加AppendText
,+=
,字符串生成器,一切似乎都一样,数据正在通过标题,正文,哈希传递,但不会附加,有人可以看到我犯的任何明显错误吗?难道后台工作者正在删除导致此问题的每个迭代文本?当我进行搜索时,似乎没有什么提示,可以提供任何帮助。
答案 0 :(得分:0)
您尝试添加的文本可能大于MaxLength
的{{1}}属性。我建议检查此属性,并确保将其设置为足够大的值以容纳您的所有文本。这是有关如何执行此检查的示例(下面的链接中的代码段):
RichTextBox
您可以在这里阅读更多内容:
此外,由于您使用的是private void AddMyText(string textToAdd)
{
// Determine if the text to add is larger than the max length property.
if (textToAdd.Length > richTextBox1.MaxLength)
// Alert user text is too large.
MessageBox.Show("The text is too large to addo to the RichTextBox");
else
// Add the text to be added to the control.
richTextBox1.SelectedText = textToAdd;
}
而不是File.WriteAllText()
,因此debug.txt仅包含最后写入的字符串的内容。