vb.net 多线程调用另一窗口,假死现象,如何解决

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vb.net 多线程调用另一窗口,假死现象,如何解决相关的知识,希望对你有一定的参考价值。

'窗体1

Imports System
Imports System.Threading
Public Class Form1

Public Sub StartTimer()
Dim tcb As New TimerCallback(AddressOf Me.TimerMethod)
Dim objTimer As Timer
objTimer = New Timer(tcb, Nothing, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10))
End Sub
Public Sub TimerMethod(ByVal state As Object)
'处理其它事物的程序略
Form2.Show() '启动倒计时显示窗口
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
StartTimer()
End Sub
End Class

'窗体2(到计时器)
Public Class Form2

Dim s As Integer = 60
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = s
s -= 1
If s = 0 Then
Me.Close()
End If
End Sub
End Class

感觉问题出在

Form2.Show() '启动倒计时显示窗口
这一句上。
这一句应该只需要执行一次就行了。
把它放在timer中,多次调用,造成假死。
参考技术A 把Form2.Show()改成:
BeginInvoke(Sub() Form2.Show())追问

谢谢,不会假死了,但Form2不能每10秒弹一个倒计时器,而是这个form2自动关闭后10秒才再次弹出。能否每10秒弹一个Form2出来?

追答

建议你用System.Windows.Forms里面的Timer而不是System.Threading里面的,这个很复杂而且线程不安全

本回答被提问者采纳

QT多线程之---moveToThread用法

  在gui编程里,一个子函数的运行时间可能过长,界面就处于假死状态,原因是窗口是一个线程,子函数也在这个线程里,一些事件也要在这个线程里处理。

如果子函数运行时间过长,系统没有办法调用事件监听循环,gui就处于假死。一般有两种办法:

      子函数事件不是很长,可以在子函数中间插入一些 QCoreApplication::processEvents 

       另一种方法就是把耗时的工作放到另一个线程里,通过信号槽来传递。这里介绍Qobject的moveToThread方法。

       下面使用老板和员工的例子来讲,有两个BT老板,闲的蛋疼,安了个闹钟,每个一段时间查员工的岗。

代码如下:

  

#include <QCoreApplication>
#include <QThread>
#include <QTimer>
#include <QObject>
#include <QDebug>
#include "worker.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qDebug()<<"main thread is :"<<QThread::currentThreadId()<<endl;    //打印主线程的线程号
   
QTimer *boss1 = new QTimer(); boss1->setInterval(5000); QThread *t1=new QThread(); //来一个新的线程 t1->start(); //启动线程 Worker w1("Bob"); //创建一个对象 w1.moveToThread(t1); //把这个对象移到线程t1里 QObject::connect(boss1, SIGNAL(timeout()), &w1, SLOT(run())); //通过信号槽机制将boss1和worker1连接起来,每个一段时间查一次看看员工工作没 QTimer *boss2 =new QTimer();
boss2->setInterval(3000); QThread *t2=new QThread(); //来一个新的线程 t2->start(); //启动线程 Worker w2("Stuart"); //创建一个对象 w2.moveToThread(t2); //把这个对象移到线程t2里 QObject::connect(boss2, SIGNAL(timeout()), &w2, SLOT(run())); boss1->start(); boss2->start(); return a.exec(); }

 worker类

  注意的是worker类一定要继承Qobject

#ifndef WORKER_H
#define WORKER_H

#include <QObject>
#include <iostream>
class Worker : public QObject
{
    Q_OBJECT
public:
    explicit Worker(QString name, QObject *parent = nullptr);
    long runnum=0;
    QString name;

signals:

public slots:
    void run(void);
};

#endif // WORKER_H

 

#include "worker.h"
#include <QDebug>
#include <QThread>
Worker::Worker(QString name ,QObject *parent) : name(name),QObject(parent)
{
    qDebug()<<"Hi, I‘m worker:"<<name<<" at thread number:"<<QThread::currentThreadId()<<endl;
}

void Worker::run()
{
    runnum++;
    qDebug()<<"I‘m "<<name<<" don‘t spy me so frequently boss! I‘m busy in my work..........";
    qDebug()<<"you have spy me "<<runnum<<" times!"<< "I‘m working at thread num :"<<QThread::currentThreadId()<<endl;
    long s=1000;
    for (long i=0; i<s;i++){


    }

}

 运行后的结果:

main thread is : 0x6be0

Hi, I‘m worker: "Bob"  at thread number: 0x6be0

Hi, I‘m worker: "Stuart"  at thread number: 0x6be0

I‘m  "Stuart"  don‘t spy me so frequently boss! I‘m busy in my work..........
you have spy me  1  times! I‘m working at thread num : 0x36c0

I‘m  "Bob"  don‘t spy me so frequently boss! I‘m busy in my work..........
you have spy me  1  times! I‘m working at thread num : 0x5ff0

I‘m  "Stuart"  don‘t spy me so frequently boss! I‘m busy in my work..........
you have spy me  2  times! I‘m working at thread num : 0x36c0

I‘m  "Stuart"  don‘t spy me so frequently boss! I‘m busy in my work..........
you have spy me  3  times! I‘m working at thread num : 0x36c0

I‘m  "Bob"  don‘t spy me so frequently boss! I‘m busy in my work..........
I‘m  "Stuart"  don‘t spy me so frequently boss! I‘m busy in my work..........
you have spy me  2  times! I‘m working at thread num : 0x5ff0

you have spy me  4  times! I‘m working at thread num : 0x36c0

I‘m  "Bob"  don‘t spy me so frequently boss! I‘m busy in my work..........
I‘m  "Stuart"  don‘t spy me so frequently boss! I‘m busy in my work..........

 

不知道你们又遇到BT的老板吗。

 

以上是关于vb.net 多线程调用另一窗口,假死现象,如何解决的主要内容,如果未能解决你的问题,请参考以下文章

vb.net多线程,循环导致窗口界面假死的问题

vb.net 线程托管 窗口假死

vb.net 多线程 窗体 假死

c# 多线程运行时出现主界面卡死现象如何解决?

C#程序窗口假死

VB.net 如何设计多线程运行