Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 后台工作人员报告忙,尽管已取消且不再存在_C#_Wpf_Multithreading_Backgroundworker - Fatal编程技术网

C# 后台工作人员报告忙,尽管已取消且不再存在

C# 后台工作人员报告忙,尽管已取消且不再存在,c#,wpf,multithreading,backgroundworker,C#,Wpf,Multithreading,Backgroundworker,当我取消并处理我的后台工作程序时,尽管它已从VS线程列表中消失,但它仍会继续向我的应用程序报告忙碌,而我的应用程序正在等待它关闭。此外,从未引发我的RunWorkerCompleted事件 调试表明代码已退出,我看不到进一步的挂起执行 如果删除.IsBusy检查,VS会报告“此BackgroundWorker当前正忙,无法同时运行多个任务” 我无法理解为什么VS中不再列出的线程在关闭后继续运行 private void UpdateTimeLine() {

当我取消并处理我的后台工作程序时,尽管它已从VS线程列表中消失,但它仍会继续向我的应用程序报告忙碌,而我的应用程序正在等待它关闭。此外,从未引发我的RunWorkerCompleted事件

调试表明代码已退出,我看不到进一步的挂起执行

如果删除.IsBusy检查,VS会报告“此BackgroundWorker当前正忙,无法同时运行多个任务”

我无法理解为什么VS中不再列出的线程在关闭后继续运行

     private void UpdateTimeLine()
        {

        txtb_timeline.Text = "Updating...";


        startTimelineUpdater.DoWork += new DoWorkEventHandler(startTimelineUpdater_DoWork);
        startTimelineUpdater.RunWorkerAsync("hometimeline");

        startTimelineUpdater.WorkerReportsProgress = true;
        startTimelineUpdater.ProgressChanged += new ProgressChangedEventHandler
                                             (startTimelineUpdater_ProgressChanged);
        startTimelineUpdater.WorkerSupportsCancellation = true;

    }


   void startTimelineUpdater_DoWork(object sender, DoWorkEventArgs e)
    {
        //begin the thread to maintain updates of SQL          
        beginRegUpdateTimeline.DoWork += new DoWorkEventHandler(beginRegUpdateTimeline_DoWork);
        beginRegUpdateTimeline.RunWorkerAsync();
        beginRegUpdateTimeline.WorkerSupportsCancellation = true;

        while (true)
        {
            List<string[]> sqlset;

            Xtweet getSQL = new Xtweet();

            if(e.Argument.ToString() == "hometimeline")
            {
                sqlset = getSQL.CollectLocalTimelineSql();
            }
            else
            {
                sqlset = getSQL.CollectLocalTimelineSql(Int64.Parse(e.Argument.ToString()));
            }

            int i = 0;
            while (i < 10)
            {
                foreach (var stringse in sqlset)
                {
                    StringBuilder sb = new StringBuilder();

                    sb.Append(stringse[0]);
                    sb.Append(": ");
                    sb.Append(stringse[1]);
                    sb.Append(" @ ");
                    sb.Append(stringse[2]);

                    sb.Append("\n");

                    BackgroundWorker bkgwk = sender as BackgroundWorker;
                    bkgwk.ReportProgress(0, sb.ToString());

                    Thread.Sleep(1000);

                    i++;

                    if(startTimelineUpdater.CancellationPending)
                    {
                        e.Cancel = true;
                        startTimelineUpdater.Dispose();
                        break;
                    }

                }
                if (e.Cancel == true)
                {
                    break;
                }

            }

            if (e.Cancel == true)
            {
                break;

            }

        }


    }

    /// <summary>
    /// Handles the DoWork event of the beginRegUpdateTimeline control.
    /// Updates the timeline sql on a regular basis
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param>
    void beginRegUpdateTimeline_DoWork(object sender, DoWorkEventArgs e)
    {

        while (true)
        {
            //update time in seconds
            int secs = 10;

            Xtweet.PreSqlDataCollection(null,null);
            Thread.Sleep(secs*1000);

            if(beginRegUpdateTimeline.CancellationPending)
            {
                e.Cancel = true;
                beginRegUpdateTimeline.Dispose();
                break;
            }


        }


    }

    private void startTimelineUpdater_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        txtb_timeline.Text = e.UserState.ToString();

    }   
private void UpdateTimeLine()
{
txtb_timeline.Text=“更新…”;
startTimelineUpdater.DoWork+=新的DoWorkEventHandler(startTimelineUpdater\u DoWork);
startTimelineUpdater.RunWorkerAsync(“hometimeline”);
startTimelineUpdater.WorkerReportsProgress=true;
startTimelineUpdater.ProgressChanged+=新的ProgressChangedEventHandler
(startTimelineUpdater_ProgressChanged);
startTimelineUpdater.WorkersSupportsScanCellation=true;
}
void startTimelineUpdater_DoWork(对象发送方,DoWorkEventArgs e)
{
//开始线程以维护SQL的更新
beginRegUpdateTimeline.DoWork+=新的DoWorkEventHandler(beginRegUpdateTimeline_DoWork);
beginRegUpdateTimeline.RunWorkerAsync();
beginRegUpdateTimeline.WorkersSupportsScanCellation=true;
while(true)
{
列出sqlset;
Xtweet getSQL=newxtweet();
if(例如,Argument.ToString()=“hometimeline”)
{
sqlset=getSQL.CollectLocalTimelineSql();
}
其他的
{
sqlset=getSQL.CollectLocalTimelineSql(Int64.Parse(e.Argument.ToString());
}
int i=0;
而(i<10)
{
foreach(sqlset中的var stringse)
{
StringBuilder sb=新的StringBuilder();
sb.Append(stringse[0]);
某人加上(“:”);
sb.追加(stringse[1]);
某人附加(“@”);
sb.追加(stringse[2]);
某人附加(“\n”);
BackgroundWorker bkgwk=发送方作为BackgroundWorker;
bkgwk.ReportProgress(0,sb.ToString());
睡眠(1000);
i++;
if(startTimelineUpdater.CancellationPending)
{
e、 取消=真;
startTimelineUpdater.Dispose();
打破
}
}
如果(e.Cancel==true)
{
打破
}
}
如果(e.Cancel==true)
{
打破
}
}
}
/// 
///处理beginRegUpdateTimeline控件的DoWork事件。
///定期更新时间线sql
/// 
///事件的来源。
///包含事件数据的实例。
void beginRegUpdateTimeline_DoWork(对象发送方,DoWorkEventArgs e)
{
while(true)
{
//更新时间(秒)
整数秒=10;
Xtweet.PreSqlDataCollection(null,null);
线程睡眠(秒*1000);
if(beginRegUpdateTimeline.CancellationPending)
{
e、 取消=真;
beginRegUpdateTimeline.Dispose();
打破
}
}
}
私有void startTimelineUpdater_ProgressChanged(对象发送方,ProgressChangedEventArgs e)
{
txtb_timeline.Text=e.UserState.ToString();
}   
线程睡眠后调用

如上所述:

BackgroundWorker只是在UI完成之前不报告它 线程获得控制权

更新:


因为这是WPF,所以您应该使用

我应该提到的,这是WPF。我不认为有一个doSt回事?没关系,我添加了一个引用Windows。窗体,它工作得很好。