如何在特定的空闲时间关闭WPF窗口? [英] How to Close a wpf window in particular idletime?

查看:95
本文介绍了如何在特定的空闲时间关闭WPF窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

在我的程序中,已经有一个线程在运行marque函数.现在,我希望该窗口在特定的空闲时间关闭.我放了另一个计时器线程.但是会引发异常.

我的代码-

Hi friends,

In my Program Already one thread is running for marque function. And now i want that window close in particular idle time. I put another timer thread. but it throw an exception.

my code -

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    playSwf();
    //System.Windows.Forms.Application.Idle += new EventHandler(Application_Idle);
    string todayDate = DateTime.Now.ToShortDateString();

    try
    {
        var functionRecs = from recs in Window1.mcsCtx.mcs_function
                           orderby recs.mcs_Function_FromTime
                           select recs;
        lbl_head1.Content = functionRecs.First().mcs_Function_PartyName.ToString();
        lbl_sub1.Content = functionRecs.First().mcs_Function_HallName.ToString();
        lbl_Time1.Content = functionRecs.First().mcs_Function_FromTime.ToString();
        lFunctions = functionRecs.ToList();

        // display restaurant names
        updateFunctionList(nBlock);

        var scrollrecs = from recs in Window1.mcsCtx.mcs_scroll select recs;
        string tit = scrollrecs.First().mcs_Scroll_Title.ToString();
        string descrip = scrollrecs.First().mcs_Scroll_Description.ToString();
        AddTextBlock(tit + "   " + descrip);
    }
    catch
    {
        MessageBox.Show("Check the Connection");
    }

    canvas1.Dispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate(Object state)
    {
        var node = textBlocks.First;

        while (node != null)
        {
            //double left = 0;

            if (node.Previous != null)
            {
                Canvas.SetLeft(node.Value, Canvas.GetLeft(node.Previous.Value) + node.Previous.Value.ActualWidth + gap);
            }
            else
            {
                Canvas.SetLeft(node.Value, canvas1.Width + gap);
            }

            node = node.Next;
        }

        return null;

    }), null);
    timer.Interval = timer_interval;
    timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
    //timer.Start();
    timer.Enabled = true;

    IdleTimer.Interval = 1;
    IdleTimer.Elapsed += new ElapsedEventHandler(IdleTimer_Elapsed);
    //IdleTimer.Start();
    IdleTimer.Enabled = true;
}

void IdleTimer_Elapsed(object sender, ElapsedEventArgs e)
{
    try
    {
        var idletimerecs = from recs in Window1.mcsCtx.mcs_idletimer
                           select recs;
        int idletimedb = idletimerecs.First().mcs_IdleTimer_Time;
        int itime = idletimedb * 100;
        idleCounter++;

        if (idleCounter == itime)
        {
            //MessageBox.Show(""+itime);
            //Thread.CurrentThread.Abort();
            //IdleTimer.Enabled = false;
            fn();
            //Window1 win1 = new Window1();
            //win1.Show();
        }
    }
    catch//(Exception er)
    {
       // MessageBox.Show("ERROR"+er);
    }
}

void fn()
{
    try
    {
        //timer.Enabled = false;
        //IdleTimer.Enabled = false;
        this.Close();
    }
    catch (Exception exa)
    {
        MessageBox.Show("" + exa);
        //MessageBox.Show(""+exa.InnerException);
    }
    //ButtonAutomationPeer peer = new ButtonAutomationPeer(btn_openMain);
    //IInvokeProvider invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
    //invokeProv.Invoke();
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    canvas1.Dispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate(Object state)
    {
        var node = textBlocks.First;
        var lastNode = textBlocks.Last;

        while (node != null)
        {
            double newLeft = Canvas.GetLeft(node.Value) - move_amount;

            if (newLeft < (0 - (node.Value.ActualWidth + gap)))
            {
                textBlocks.Remove(node);

                var lastNodeLeftPos = Canvas.GetLeft(lastNode.Value);

                textBlocks.AddLast(node);

                if ((lastNodeLeftPos + lastNode.Value.ActualWidth + gap) > canvas1.Width) // Last element is offscreen
                {
                    newLeft = lastNodeLeftPos + lastNode.Value.ActualWidth + gap;
                }
                else
                {
                    newLeft = canvas1.Width + gap;
                }
            }

            Canvas.SetLeft(node.Value, newLeft);

            node = node == lastNode ? null : node.Next;
        }

        return null;

    }), null);
}

void AddTextBlock(string Text)
{
    TextBlock tb = new TextBlock();
    tb.Text = Text;
    tb.FontSize = 35;
    tb.FontWeight = FontWeights.Medium;
    tb.Foreground = Brushes.Gold;

    canvas1.Children.Add(tb);

    Canvas.SetTop(tb, -3);
    Canvas.SetLeft(tb, -999);
    Canvas.SetBottom(tb, 3);

    textBlocks.AddLast(tb);
}



异常-

System.InvaliadOperationException

调用read的对象无法访问该对象,因为其他线程拥有该对象.
在this.Close();中;代码.

帮助我!..



Exception -

System.InvaliadOperationException

the calling theread cannot access this object because a different thread owns it
in this.Close(); code.

help me!..

推荐答案

您正在尝试以交叉线程方式访问对象.

在尝试访问该对象之前,请尝试返回主线程.
You are trying to access objects in a cross threaded way.

Try returning to the main thread before trying to access this object.


这篇关于如何在特定的空闲时间关闭WPF窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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