在C#中没有响应时显示gif [英] Display gif when not responding in C#

查看:84
本文介绍了在C#中没有响应时显示gif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我在我的项目中有2个表格

在表格1中运行此代码

Hello
I Have 2 Form In My Project
In Form 1 Run This Code

int i = 0;
private void Form1_Load(object sender, EventArgs e)
{
    Form2 F = new Form2();
    F.Show();
    while (true)
    {
        i++;
        textbox1.text = i.toString();
    }
}



在表格2中有一个动画Gif由PictureBox

但我的问题什么时候没有回复我的应用程序例如In(true)

第二种形式除了工具(pictureBox)和Show One Gif的form2中没有显示任何内容



我尝试了什么:



而(true)是示例代码无回复

我想要显示True PictureBox时没有响应


In Form 2 Have One Animated Gif By PictureBox
But My Question When Not Responding My Application For Example In While(true)
The second form does not display anything but there in the form2 of a tool(pictureBox) and Show One Gif

What I have tried:

while(true) is sample code For Not Responding
I Want Show True PictureBox When Not Responding

推荐答案

这是一种糟糕的做事方式,但是如果你必须尝试使用​​ Application.DoEvents()

This is a bad way to do things, but if you must try using Application.DoEvents() :
int i = 0;
private void Form1_Load(object sender, EventArgs e)
{
    Form2 F = new Form2();
    F.Show();
    while (true)
    {
        i++;
        textbox1.text = i.ToString();
        Application.DoEvents();
    }
}



或者使用任务


Alternatively use Task :

int i = 0;
private void Form1_Load(object sender, EventArgs e)
{
    Form2 F = new Form2();
    F.Show();
    Task.Factory.StartNew( () =>
    {
        i++;
        textbox1.text = i.ToString();
    });
}


不,它不会。

实际上该代码的作用是锁定UI线程完全,因为它无法查看正在发生的事件,因为它在中完全忙碌而循环 - 并且因为它永远不会结束,所以没有更多事件发生Windows $是基于消息的操作系统 - 您(或用户)所做的一切都被转换成一条消息,该消息在系统中传递并由代码的隐藏部分处理称为消息循环。如果你从事件处理程序中不存在,那么控件永远不会返回到消息循环,它永远不会查看更多消息 - 所以你的UI冻结并且永远不会恢复。

你在里面做什么出于完全相同的原因,即使没有附加表格,该循环也不会显示任何内容。



或许,您需要考虑开始第二个线程来做某事(如果该代码不是实际操作的占位符) - 但是如果你这样做,你需要非常小心,因为你不能访问控件,除非它在主线程上。它变得复杂了,我不会尝试向你解释它的代码!



基本上,你在那里尝试做什么都行不通:我们可以告诉你实际要做什么,除非我们对你在现实中想要做的事情有更好的了解。
No, it won't.
In fact what that code does is lock up the UI thread completely, because it can't do anything to look at the event that are occurring because it is completely busy inside your while loop - and since that never ends, no more events are ever actioned.
Windows is a message based operating system - and everything you (or the user) does is translated into a message which is passed around the system and processed by a "hidden" part of your code called a "Message Loop". If you do no exist from an Event Handler then control never gets back to the message loop and it never gets to look at further messages - so your UI freezes up and never recovers.
What you are doing inside that loop wouldn't display anything anyway, even without the additional Form for exactly the same reasons.

Probably, you need to look at starting a second thread to do something (if that code isn't a placeholder for your real actions) - but if you do that, you need to be pretty careful as you can't access controls unless it's on the main thread. It gets complicated, and I'm not going to try and explain it to you for that code!

Basically, what you are trying to do there won't work: and we can;t tell you what to actually do unless we have a much, much better idea what you are trying to do in reality.


如果你在一个线程中做了大量的工作,也希望显示其他东西,除非你的代码执行完成,否则它是不可能的



这样做你可以使用以下的想法。

您可以使用后台工作人员进行无响应工作

,而您的应用程序线程将显示gif图片框

就是它
if you do huge work in one thread and also want to display others thing it will not possible until and unless your code execution finished

for doing this you can use the below idea.
You can Use background worker for doing your not responding work
while your application thread will display the gif Picture box
that's it


这篇关于在C#中没有响应时显示gif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆