“Not Responding” in window title when running in new process

前端 未结 8 1295
清酒与你
清酒与你 2021-02-07 14:39

I have a long running method that must run on UI thread. (Devex - gridView.CopyToClipboard())

I do not need the UI to be responsive while c

相关标签:
8条回答
  • 2021-02-07 15:19

    I am afraid the simplest solution is to make your own CopyToClipboard() where you in your for loop, every now and then, do an Application.DoEvents, which keeps the ui thread responsive.

    I guess most licenses of DevExpress have the source code available, so you can probably copy paste most if it.

    Since you know the data you can probably make a much simpler procedure than the generic that DevExpress uses.

    like this:

    const int feedbackinterval = 1000;
    
    private void btnCopy_Click(object sender, EventArgs e)
    {
        StringBuilder txt2CB = new StringBuilder();
        int[] rows = gridView1.GetSelectedRows();
    
        if (rows == null) return;
    
        for (int n = 0; n < rows.Length; n++)
        {
            if ((n % feedbackinterval) == 0) Application.DoEvents();
    
            if (!gridView1.IsGroupRow(rows[n]))
            {
                var item = gridView1.GetRow(rows[n]) as vWorkOrder;
                txt2CB.AppendLine(String.Format("{0}\t{1}\t{2}",
                item.GroupCode, item.GroupDesc, item.note_no??0));
            }
         }
            Clipboard.SetText(txt2CB.ToString());
    }
    
    0 讨论(0)
  • 2021-02-07 15:22

    This is because you call a long running method synchronously in your main application thread. As your applicaton is busy it does not respond to messages from windows and is marked as (Not Responding) until finished.

    To handle this do your copying asynchronously e.g. using a Task as one simplest solution.

    Task task = new Task(() =>
            {
                gridView.Enabled = false;
                gridView.CopyToClipboard();
                gridView.Enabled = true;
            });
    
            task.Start();
    

    Disable your grid so nobody can change values in the GUI. The rest of your application remains responsive (may has side effects!).

    0 讨论(0)
提交回复
热议问题