Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6776129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:59:27+00:00 2026-05-26T15:59:27+00:00

I’ve been trying to learn delegates.I just created a button,label and checkbox. If I

  • 0

I’ve been trying to learn delegates.I just created a button,label and checkbox. If I click checkbox, the time format changes. If i click the button , i print the date accordingly. However when trying to use asynchromous delegate i.e., to use another thread, i am stuck with an error

   public delegate void AsyncDelegate(bool seconds);

public partial class Form1 : Form
{
    AsyncDelegate ad;
    TimeZ t = new TimeZ();
    public Form1()
    {
        InitializeComponent();
    }

 private void btn_async_Click(object sender, EventArgs e)
    {

        ad = new AsyncDelegate(t.GetTime);
        AsyncCallback acb = new AsyncCallback(CB);
        if (chk_sec.Checked)
        {
            ad.BeginInvoke(true, acb, null);
        }
        else
            ad.BeginInvoke(false, acb, null);


    }
    public void CB(IAsyncResult ar)
    {
        t.Tim = ar.ToString();
        ad.EndInvoke(ar);
        lbl_time.Text = t.Tim;
    }

and in another class library i get Timez used above. I add a reference of it in the project

 public class TimeZ
{
  private string tim;
    public string Tim
    {
        get
        {
            return tim;
        }
        set
        {
            tim = value;
        }
    }
    public string GetTime(bool seconds)
    {
        if (seconds)
        {
           return DateTime.Now.ToLongTimeString();
        }
        else
            return DateTime.Now.ToShortTimeString();
    }
}

However i get this error when i run the program:

 Cross-thread operation not valid: Control 'lbl_time' accessed from a thread other than   
    the thread it was created on.

Can u help me out on how to solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:59:27+00:00Added an answer on May 26, 2026 at 3:59 pm

    You cannot access forms and controls properties and methods from a thread that is not the form thread.

    In windows, each window is bound to the thread that created it.

    You can do that only with Control.BeginInvoke or the more useful System.Threading.SynchronizationContext class.

    See http://msdn.microsoft.com/it-it/library/system.threading.synchronizationcontext(v=vs.95).aspx

    See http://msdn.microsoft.com/it-it/library/0b1bf3y3(v=vs.80).aspx

    It means, you have to post through synchronization context for example another async delegate in form thread.

    public partial class Form1 : Form
    {
        AsyncDelegate ad;
        TimeZ t = new TimeZ();
    
        // Our synchronization context
        SynchronizationContext syncContext;
    
        public Form1()
        {
            InitializeComponent();
    
            // Initialize the synchronization context field
            syncContext = SynchronizationContext.Current;
        }
    
        private void btn_async_Click(object sender, EventArgs e)
        {
    
            ad = new AsyncDelegate(t.GetTime);
            AsyncCallback acb = new AsyncCallback(CB);
            if (chk_sec.Checked)
            {
                ad.BeginInvoke(true, acb, null);
            }
            else
            {
                ad.BeginInvoke(false, acb, null);
            }
        }
    
        public void CB(IAsyncResult ar)
        {
            // this will be executed in another thread
            t.Tim = ar.ToString(); // ar.ToString()???? this will not give you the time for sure! why?
            ad.EndInvoke(ar);
    
            syncContext.Post(delegate(object state)
            {
                // This will be executed again in form thread
                lbl_time.Text = t.Tim;
            }, null);
        }
    

    I don’t know why you need an asynchronous callback to print time however 🙂 really don’t know why, thinking it is just some test code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.