Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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);
}


Exception -

System.InvaliadOperationException

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

help me!..
Posted

1 solution

You are trying to access objects in a cross threaded way.

Try returning to the main thread before trying to access this object.
 
Share this answer
 
Comments
Sagotharan Jagadeeswaran 4-Apr-11 3:42am    
Thanks for ur answer.

how i return to main thread?. i already try to dispose all the other thread, but it not help.

pls anyone tell me the way to access the main thread...
Sagotharan Jagadeeswaran 4-Apr-11 4:09am    
ya i got the Solution,. thank u for a great idea friend.

if (idleCounter == itime)
{
Application.Current.Dispatcher.Invoke((ThreadStart)delegate
{
this.Close();
});
}
Tarun.K.S 4-Apr-11 6:45am    
Good!
Abhinav S 4-Apr-11 8:26am    
Yes. That is a good use of the Dispatcher class.
Jack Niu 31-May-11 4:38am    
Great! it works, thank you very much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900