读书人

C#winform未响应有关问题

发布时间: 2012-12-26 14:39:28 作者: rapoo

C#winform未响应问题



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Test_Eight.Properties;

namespace Test_Eight
{
public partial class AutoPlay : Form
{
public AutoPlay()
{
InitializeComponent();
}

private Bitmap MyBitmap; //事件公用变量

//初始化
private void AutoPlay_Load(object sender, EventArgs e)
{
Bitmap SrcBitmap = new Bitmap(Resources._1); //把打开的图像赋给Bitmap变量
MyBitmap = new Bitmap(SrcBitmap, this.pictureBox_Show.Width, this.pictureBox_Show.Height);
this.pictureBox_Show.Image = MyBitmap; //在控件上显示图像
timer_Change.Enabled = true;
}

//对接
private void Joint()
{
try
{
int width = this.pictureBox_Show.Width;//图像宽度
int height = this.pictureBox_Show.Height;//图像高度
Graphics g = this.pictureBox_Show.CreateGraphics();
g.Clear(Color.Gray);
Bitmap bitmap = new Bitmap(width, height);
int x = 0;
while (x <= height / 2)
{
for (int i = 0; i <= width - 1; i++)


{
bitmap.SetPixel(i, x, MyBitmap.GetPixel(i, x));
}
for (int i = 0; i <= width - 1; i++)
{
bitmap.SetPixel(i, height - x - 1, MyBitmap.GetPixel(i, height - x - 1));
}
x++;
this.pictureBox_Show.Invalidate();
g.DrawImage(bitmap, 0, 0);
System.Threading.Thread.Sleep(1);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

//中间拉伸
private void Middle_Stretch()
{
int width = this.pictureBox_Show.Width; //图像宽度
int height = this.pictureBox_Show.Height; //图像高度
Graphics g = this.pictureBox_Show.CreateGraphics(); //创建Graphics对象实例


g.Clear(Color.Gray); //初始为全灰色
for (int y = 0; y <= width / 2; y++) //两边拉伸显示
{
Rectangle DestRect = new Rectangle(width / 2 - y, 0, 2 * y, height);
Rectangle SrcRect = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
g.DrawImage(MyBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
System.Threading.Thread.Sleep(1);
}
}

int Count = 0;
//切换图片
private void timer_Change_Tick(object sender, EventArgs e)
{
Count++;
switch (Count)
{
case 1: Joint(); break;
case 2: Middle_Stretch(); break;
default: break;
}
if (Count == 3)
{
Count = 0;
pictureBox_Show.Image = Resources._1;
}
}
}
}


[解决办法]
你的所有的g都没有释放啊
[解决办法]
问题出在你的Joint和 Middle_Stretch的循环中,尤其是SetPixel的效率非常低,导致才Form退出时,Timer的线程还需要很长时间才能结束退出。

解决办法可以设定一个Bool变量用于控制上面2个函数的退出,在Form的Closing事件中改变Bool变量使得Timer中的循环迅速退出,另外还需要先停止Timer。
[解决办法]
要是这个就是你全部的代码的话,我想是不会造成未响应的,你先自己试试能不能将错误抓出来,知道是哪里有问题了才好解决,否则别人也只能靠猜的,不一定对

protected override void OnClosing(CancelEventArgs e)


{
try
{
timer_Change.Enabled = false;
base.OnClosing(e);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}


[解决办法]
在循环末端加上 Application.DoEvents();
或者另开一个线程来实现播放管理。
[解决办法]
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
winform的UI线程是单的,你的这个 Joint()Middle_Stretch()方法里面System.Threading.Thread.Sleep(1); 语句会导致主线程停止,当你将sleep时间加大的时候,任何对界面的操作都会产生大的延迟,也就是未响应了, 将这两句代码注掉
[解决办法]
引用:
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
winform的UI线程是单的,你的这个 Joint()Middle_Stretch()方法里面System.Threading.Thread.Sl……


这是Timer的事件里面的,怎么会造成UI堵塞??

调试过了,解决办法我在前面说过了。就是Joint方法里面的SetPixel在图片大的时候,那循环要执行很多次,而退出的时候Timer还在跑,而且还远远因为SetPixel而没结束掉。。。从而导致UI卡死

读书人网 >C#

热点推荐