线程开始暂停停止问题。求大佬解决!

only_youlix 2017-10-27 03:17:53
要做一个循环发送消息的功能。循环要几千次最少。 刚开始用DispatcherTimer测试 发现时间长了会卡。

后来改为Thread 使用的是.NET 4

Thread thread ;
void Window1_Loaded(object sender, RoutedEventArgs e)
{
thread = new Thread(new ThreadStart(timeTick));
}
private void btnPlay_Click(object sender, RoutedEventArgs e)
{
//播放
if (!thread.IsAlive)
{
try { thread = new Thread(new ThreadStart(timeTick)); }
catch { }
thread.Start();
}
else
{
try
{

thread.Resume();
}
catch { }
}
}

private void btnPause_Click(object sender, RoutedEventArgs e)
{

thread.Suspend();

}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
if (thread.IsAlive)
{
try
{
thread.Resume();
}
catch { }
thread.Abort();
}
}

void timeTick()
{
while (true)
{
Thread mythread = new Thread(() =>
{
if (CommonClass.lstTimeInfo != null && CommonClass.lstTimeInfo.Count != 0)
{
clsTimeInfo clsTimeIndex = null;

int index = CommonClass.currDateTime / mTimeSpan;
if (index < CommonClass.lstTimeInfo.Count)
{
clsTimeIndex = CommonClass.lstTimeInfo[index];

if (clsTimeIndex != null && clsTimeIndex.cmd != null)
{
for (int i = 0; i < CommonClass.lstIPInfos.Count; i++)
{
if (CommonClass.lstIPInfos[i].IsPlay == true)
CommonClass.SendToUdp(clsTimeIndex.cmd, CommonClass.lstIPInfos[i].Ip);
}
}
}
}
});
mythread.Start();
CommonClass.currDateTime = CommonClass.currDateTime + mTimeSpan;
Thread.Sleep(mTimeSpan);
}
}





使用的是 Resume 和 Suspend 和 Abort 控制开始 暂停 停止。 功能倒是实现了。不过这样有什么问题吗。 还有这个功能要怎么实现呢。需要能开始 暂停 停止。
...全文
693 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
only_youlix 2017-11-01
  • 打赏
  • 举报
回复
好吧。我在研究研究
exception92 2017-11-01
  • 打赏
  • 举报
回复
引用 2 楼 only_youlix 的回复:
断点调试会挂起进程中的所有线程,也就线程暂时不会工作,你能看出什么。
only_youlix 2017-10-31
  • 打赏
  • 举报
回复
引用 1 楼 duanzi_peng 的回复:
这样肯定有问题的,因为创建销毁Thread是非常耗资源的。Loaded的时候没有启动线程,所以不用那么声明,多余。 timeTick 里就不需要Thread了,由一个Thread执行就行了。 另外在线程停止时 你while循环会一直运行,并没有跳出循环,创建的mythread会不停的初始化,时间长了CPU利用率会达到100%,但是它却什么都没有做,浪费资源。
我把timetick里的new Thread去掉了。 这个是个视频播放的 播放的同时会发送消息 就是执行thread.start 或者 thread.Resume(); 暂停 就 用的thread.Suspend(); 停止是 if thread.Abort(); 然后在初始化一下Thread 。 timeTick里 方法有一个else 是执行完了就停止thread的 。我打断点看 貌似没发现啥问题。。

 void timeTick()
        {
            while (true)
            {
                if (CommonClass.lstTimeInfo != null && CommonClass.lstTimeInfo.Count != 0)
                {
                    clsTimeInfo clsTimeIndex = null;

                    int index = CommonClass.currDateTime / mTimeSpan;
                    if (index < CommonClass.lstTimeInfo.Count)
                    {
                        clsTimeIndex = CommonClass.lstTimeInfo[index];

                        if (clsTimeIndex != null && clsTimeIndex.cmd != null)
                        {
                            for (int i = 0; i < CommonClass.lstIPInfos.Count; i++)
                            {
                                if (CommonClass.lstIPInfos[i].IsPlay == true)
                                    CommonClass.SendToUdp(clsTimeIndex.cmd, CommonClass.lstIPInfos[i].Ip);
                            }
                        }
                    }
                    else
                    {
                        CommonClass.currDateTime = 0;
                        this.Dispatcher.Invoke(new Action(() =>
                        {
                            try
                            {
                                thread.Resume();
                            }
                            catch { }
                            thread.Abort();
                        }));
                    }
                }
                CommonClass.currDateTime = CommonClass.currDateTime + mTimeSpan;
                Thread.Sleep(mTimeSpan);
            }
        }
exception92 2017-10-27
  • 打赏
  • 举报
回复
这样肯定有问题的,因为创建销毁Thread是非常耗资源的。Loaded的时候没有启动线程,所以不用那么声明,多余。 timeTick 里就不需要Thread了,由一个Thread执行就行了。 另外在线程停止时 你while循环会一直运行,并没有跳出循环,创建的mythread会不停的初始化,时间长了CPU利用率会达到100%,但是它却什么都没有做,浪费资源。

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧