English 中文(简体)
Winforms C# - show Cancel dialog, when main UI thread isbus
原标题:Winforms C# - Show Cancel dialog when main UI thread is busy

我有一个主要形式,它包含着一种全貌的控制。 还有一名工人的透镜经常向该电线控制发送标志。 现在,我想只用一个取消的纽芬兰语显示方言,而统一党的主要控制正在显示出 st。 问题在于,取消的方言没有反应,而最新消息却落在后面,我无法点击取消的纽顿。 任何关于如何解决这一问题的想法。 我正想再造一个国库,并显示它取消纽芬兰。 任何其他替代品?

EDIT#1

  1. I should clarify that I already use a worker thread to do the work.
  2. DisplayLogs() is in a seperate thread.
  3. DisplayLogs() is called from other threads.
  4. LogMessage is the delegate that points to the method UpdateMessage in main UI.
  5. The control used is a TextBox. I have tried other controls like listview, richtextboxsand, etc. still the same result.

Worker Thread

void DisplayLogs()
{
    lock (this)
    {
        while (logQueue.Count > 0)
        {
            string logMessagemessage = logQueue.Dequeue();
            LogMessage(string.Concat(logMessagemessage, Environment.NewLine));
        }
    }
}

Main UI

public void UpdateMessage( string message)
{
    if (!txtLog.IsHandleCreated)
    {
        return;
    }

    if (txtLog.InvokeRequired)
        txtLog.BeginInvoke( new UpdateLogDelegate( UpdateLog), message);
    else
        txtLog.AppendText(message);
}
问题回答

The main solution is to offload the expensive code onto a background worker and leave your UI thread responsive for UI actions. Your form can then simply show a modal dialog or something.

MSDN - How of use a Background Worker

在这种情况下,必须把大部分工作转向新的线索,并澄清废除电文的星座。

你们正在经历这种倒退。 从理论上讲,主要线应始终可供接受用户的投入。 任何可能长时间阻碍的工作(重算、数据库接入、网络接入)都应在背景线上进行。 想法是使电离层控制数据由背景线(<条码>BackgroundWorker)计算和人口构成。 如果用户点击取消的纽芬兰语,就能够提供主线。

你们的问题是,贵部的read子是ALWAYS的汽车。 我在此表示,假定<代码>log Queue中的项目数目相当多。 而 lo到 que为止是空的。 因此,它不断以更新要求来打断国际调查组的透镜。

另外,<代码>(txt Carlo.InvokeRequired)是毫无意义的,因为你总是把这种方法从工人的表面上说出来。

So, since a .net WinForm application has only a single UI, which in your case is too busy to process other notifications, the new window appears stuck (because the paint messages are stuck in the message queue and cannot be processed as it is already flooded with the text box update messages)

您可在座右栏上贴上<代码>Application.DoEvents,使电文有一段时间处理待发通知。 然而,我认为,这是一种黑板,因为“团结”行为有时不可预测。 这种做法可能会导致诸如在移动方言时utter忙、延迟对点击事件的答复等。

另一个要点,MessageBox.ShowForm.ShowDialog (如果这是你在取消纽芬兰语时使用的话)是一种阻塞。 你们显示的read,直到你放弃log。 Try Form.Show,并将母性财产归为主要形式。

另一种选择是增加一个时间,每个Y秒只处理X项通知。 这将为国际不动产业联合会提供开展其他活动所需的一些呼吸空间。





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签