简体   繁体   English

在Windows窗体中生成代码无法正常工作

[英]Generating code is not working properly in windows form

hi i made a program in windows form application to get a code which is needed for me.I wanted to set it for not blocking UI when i press submit button 您好,我在Windows窗体应用程序中编写了一个程序,以获取我所需的代码。我想将其设置为当我按下Submit按钮时不阻止UI

I used these code for that.But i get invalidoperationexception was unhandled exception.. 我为此使用了这些代码。但是我得到invalidoperationexception是未处理的异常..

Here i tried 我在这里尝试

void Generate()
{
    textBox2.Text = "";

    string[] sss = textBox1.Text.Split('\n');

    textBox2.Text = "VERSION BUILD=8820413 RECORDER=FX" + Environment.NewLine +
    "SET !ERRORIGNORE YES" + Environment.NewLine +

       "SET !TIMEOUT_TAG 3" + Environment.NewLine +
        "SET !TIMEOUT_STEP 3" + Environment.NewLine +
      "SET !TIMEOUT_PAGE 7" + Environment.NewLine +
        "SET !REPLAYSPEED FAST" + Environment.NewLine;
    string[] emails = textBox3.Text.Split('\n');

    // label2.Text = emails.Length.ToString();
    //foreach (string email in emails)
    for (int i = 0; i < Convert.ToInt32(textBox5.Text); i++)
    {
        textBox2.Text += "TAB T=1" + Environment.NewLine + "CLEAR" + Environment.NewLine +
            "URL GOTO=https://signin.ebay.com/ws/eBayISAPI.dll?SignIn&lgout=" + Environment.NewLine +
            "WAIT SECONDS=1" + Environment.NewLine +
            "TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:SIGNINFORM ATTR=ID:SUBMIT" + Environment.NewLine +
            "SET !ENCRYPTION NO" + Environment.NewLine +
            "TAG POS=1 TYPE=INPUT:PASSWORD FORM=ID:SIGNINFORM ATTR=ID:PASS CONTENT=Maths7524" + Environment.NewLine +
            "TAG POS=1 TYPE=INPUT:TEXT FORM=ID:SIGNINFORM ATTR=ID:USERID CONTENT=" + emails[i] + Environment.NewLine +
            "TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:SignInForm ATTR=ID:sgnBt" + Environment.NewLine + "WAIT SECONDS=7" + Environment.NewLine;
        foreach (string item in sss)
        {

            textBox2.Text += "URL GOTO=www.ebay.com/itm/" + item + Environment.NewLine + "WAIT SECONDS=1" + Environment.NewLine + "TAG POS=1 TYPE=SPAN ATTR=ID:watchLabel" + Environment.NewLine + "TAG POS=1 TYPE=A ATTR=TXT:Watch" + Environment.NewLine + "WAIT SECONDS=1" + Environment.NewLine + Environment.NewLine;
        }
    }
}

This is the button click event 这是按钮单击事件

private void button1_Click(object sender, EventArgs e)
{
    //Generate();

    Thread thead = new Thread(() =>
    {
        Generate();
        label6.Text = "Done.";
    });
    thead.Start();

    label6.Text = "Generating Code.. Please wait....";
}

Only the UI thread can update the UI. 只有UI线程可以更新UI。 It makes sense to run time consuming I/O operations or long running calculations in another thread; 在另一个线程中运行耗时的I / O操作或长时间运行的计算是有意义的。 however, it makes no sense to run a method that basically only updates textboxes in another thread. 但是,运行基本上只更新另一个线程中的文本框的方法没有任何意义。 This method would have to call Invoke on the textboxes, making the textbox operations run on the UI thread again. 此方法必须在文本框上调用Invoke ,使文本框操作再次在UI线程上运行。

Simply run this method normally in the click event. 只需在click事件中正常运行此方法即可。

You can speed up the method by using a StringBuilder and by assigning to the textbox only once: 您可以通过使用StringBuilder并仅分配给文本框一次来加速该方法:

string[] sss = textBox1.Text.Split('\n');
string[] emails = textBox3.Text.Split('\n');

var sb = new StringBuilder();
sb.AppendLine("VERSION BUILD=8820413 RECORDER=FX");
sb.AppendLine("SET !ERRORIGNORE YES");
sb.AppendLine("SET !TIMEOUT_TAG 3");
sb.AppendLine("SET !TIMEOUT_STEP 3");
sb.AppendLine("SET !TIMEOUT_PAGE 7");
sb.AppendLine("SET !REPLAYSPEED FAST");

for (int i = 0; i < Convert.ToInt32(textBox5.Text); i++)
{
    sb.AppendLine("TAB T=1").AppendLine("CLEAR");
    sb.AppendLine("URL GOTO=https://signin.ebay.com/ws/eBayISAPI.dll?SignIn&lgout=");
    sb.AppendLine("WAIT SECONDS=1");
    sb.AppendLine("TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:SIGNINFORM ATTR=ID:SUBMIT");
    sb.AppendLine("SET !ENCRYPTION NO");
    sb.AppendLine("TAG POS=1 TYPE=INPUT:PASSWORD FORM=ID:SIGNINFORM ATTR=ID:PASS CONTENT=Maths7524");
    sb.Append("TAG POS=1 TYPE=INPUT:TEXT FORM=ID:SIGNINFORM ATTR=ID:USERID CONTENT=").AppendLine(emails[i]);
    sb.AppendLine("TAG POS=1 TYPE=INPUT:SUBMIT FORM=ID:SignInForm ATTR=ID:sgnBt");
    sb.AppendLine("WAIT SECONDS=7");
    foreach (string item in sss)
    {
        sb.Append("URL GOTO=www.ebay.com/itm/").AppendLine(item);
        sb.AppendLine("WAIT SECONDS=1").AppendLine("TAG POS=1 TYPE=SPAN ATTR=ID:watchLabel");
        sb.AppendLine("TAG POS=1 TYPE=A ATTR=TXT:Watch").AppendLine("WAIT SECONDS=1").AppendLine();
    }
}
textBox2.Text = sb.ToString();

Strings are immutable. 字符串是不可变的。 Concatenating strings repeatedly is not efficient as a new string object is created each time and the content of the old string object is copied to the new one + the new text. 重复串联字符串效率不高,因为每次都会创建一个新的字符串对象,并将旧的字符串对象的内容复制到新的字符串对象和新的文本中。 On the other hand, a StringBuilder works with an internal string buffer that can grow until its max capacity is reached. 另一方面, StringBuilder与内部字符串缓冲区一起使用,该缓冲区可以增长直到达到其最大容量。 Then it creates a lager buffer and copies the text of the old butter to the new buffer. 然后,它创建一个更大的缓冲区,并将旧黄油的文本复制到新缓冲区。 This new buffer has plenty of free space, so that many new text pieces can be appended, before a new resize operation must be performed. 该新缓冲区具有足够的可用空间,因此在必须执行新的调整大小操作之前,可以添加许多新的文本段。

Assigning to the same textbox repeatedly is time consuming, as the textbox also triggers events. 重复分配给同一文本框非常耗时,因为文本框还会触发事件。 Only assigning the final text once, speeds up the things further. 只分配一次最终文本,可以进一步加快处理速度。

I think, like this, it will be fast enough without multithreading. 我认为像这样,没有多线程就足够快了。 If it should not be, I would make a method that does only create the string and use it to create a task that can be awaited. 如果不是这样,我将创建一个仅创建字符串并使用它来创建可以等待的任务的方法。 Note that await does not block the UI. 需要注意的是await 阻止用户界面。

private string Generate()
{
    var sb = new StringBuilder();
    //TODO: Create the text with a StringBuilder as shown above.
    return sb.ToString();
}

private async Task<string> GenerateAsync()
{
    return await Task.Run(() => Generate());
}

// Don't forgat the async keyword in button1_Click!
private async void button1_Click(object sender, EventArgs e)
{
    label6.Text = "Generating Code.. Please wait....";

    string text = await GenerateAsync();

    textBox2.Text = text;
    label6.Text = "Done.";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM