none
关于System::Threading::Timer的问题 RRS feed

  • 常规讨论

  • using namespace System::Windows::Forms;
    using namespace System::Drawing;
    using namespace System::Drawing::Imaging;
    using namespace System;
    using namespace System::Threading;
    public ref class Flash
    {
    private:
    	Bitmap^ TargetImage,^OriginImage;
    	Form^ form;
    	bool GradientModel;
    	Int32 Alpha;
    	Color FlashColor;
    	System::Threading::Timer^ timer;
    public:
    	void flash(Object^ object)
    	{
    		System::Threading::Timer^ timer=(System::Threading::Timer^)object;
    		Bitmap^ Temp;
    		Graphics^ Drawer;
    		SolidBrush^ brush;
    		Rectangle Rect=Rectangle(Point(0,0),form->BackgroundImage->Size);
    		if(!GradientModel)
    		{
    			Alpha+=50;
    			Temp=OriginImage->Clone(Rectangle(Point(0,0),OriginImage->Size),PixelFormat::Format32bppArgb);
    		}
    		else
    		{
    			Alpha-=50;
    			Temp=TargetImage->Clone(Rect,PixelFormat::Format32bppArgb);
    		}
    		Drawer=Graphics::FromImage(Temp);
    		brush=gcnew SolidBrush(Color::FromArgb(Alpha,FlashColor.R,FlashColor.G,FlashColor.B));
    		Drawer->FillRectangle(brush,Rect);
    		form->BackgroundImage=Temp;
    		brush->~SolidBrush();
    		Drawer->~Graphics();
    		Temp->~Bitmap();
    		form->Refresh();
    		if(Alpha==250)
    		{
    			GradientModel=true;
    		}
    		if(Alpha==0)
    		{
    			timer->Change(Timeout::Infinite,Timeout::Infinite);
    		}
    	}
    	Flash(Bitmap^ _TargetImage,Form^ _form,Color _FlashColor)
    	{
    		TargetImage=_TargetImage;
    		form=_form;
    		FlashColor=_FlashColor;
    		GradientModel=false;
    		Alpha=0;
    		OriginImage=(Bitmap^)form->BackgroundImage;
    		timer=gcnew System::Threading::Timer(gcnew TimerCallback(this,&Flash::flash));
    	}
    	Flash(Bitmap^ _TargetImage,Form^ _form)
    	{
    		TargetImage=_TargetImage;
    		FlashColor=Color::White;
    		form=_form;
    		GradientModel=false;
    		Alpha=0;
    		OriginImage=(Bitmap^)form->BackgroundImage;
    		timer=gcnew System::Threading::Timer(gcnew TimerCallback(this,&Flash::flash));
    	}
    	Flash(Form^ _form)
    	{
    		form=_form;
    		TargetImage=(Bitmap^)form->BackgroundImage;
    		FlashColor=Color::White;
    		GradientModel=false;
    		Alpha=0;
    		OriginImage=(Bitmap^)form->BackgroundImage;
    		timer=gcnew System::Threading::Timer(gcnew TimerCallback(this,&Flash::flash));
    	}
    	void Start(Int32 Period)
    	{
    		timer->Change(0,Period);
    	}
    };

    以上是我写的一个类,我在其他地方调用Start。例如Flash^ a=gcnew Flash(this); a->Start(1000);

    但是调试的时候没有弹出任何异常窗口就直接关闭了我正在调试的form,只有ouput那里有写:

    A first chance exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll
    A first chance exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll

    A first chance exception of type 'System.InvalidOperationException' occurred in System.Drawing.dll
    A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll
    The program '[4288] TBS-03.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

    搞得我一头雾水。请问倒底哪里出错了呢?该怎么改才可以?

    2010年8月28日 11:17

全部回复

  • 搞点stacktrace出来看看呢?
    2010年8月28日 21:38
  • 感觉像是拆箱有问题,Flash(this)。这个this 是什么对象?
    麻烦把正确答案设为解答。
    2010年8月29日 2:04
    版主
  • 谢谢楼上2位回答,this是我的form的this指针,能不能说详细点呢?
    2010年8月29日 3:54
  • 在调试选项里面设置在System.ArgumentException抛出的时候中断,然后调试,在中断的时候看call stack

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2010年8月30日 16:39
    版主
  • if(Alpha==0)
    {
    timer->Change(Timeout::Infinite,Timeout::Infinite);
    }
    好像是这句调用tmer的Change函数时出错了,提示“Object is currently in use elsewhere”。是否timer不能在timercallback委托的函数里调用Change?如果是的话,那要怎么在委托的函数里让timer停下呢?

    2010年8月31日 5:18