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 4626356
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:24:29+00:00 2026-05-22T03:24:29+00:00

I have a multi threaded app and based on a propertychange i wanted to

  • 0

I have a multi threaded app and based on a propertychange i wanted to do some actions, so i created an event handler for that and changed the property from a thread, but still when the event fires, and i try to access form elements in the event handler method visual studio complains that cross thread access is done. I dont understand why it is being considered as cross thread. This is my code,

public delegate void AppStateChangedEventHandler(int st);
    public event AppStateChangedEventHandler AppStateChanged;
    private int appState;
    public int AppState
    {
        get { return this.appState; }
        set
        {
            this.appState = value;
            if (this.AppStateChanged != null)
            {
                this.AppStateChanged(value);
            }
        }
    }
    public void MainForm_AppStateChanged(int val)
    {
        if (val == 1)
        {                
            totDwn.Text = "00:00:00";
            totAct.Text = "00:00:00";                
        }
        else if (val == 0)
        {                
            tt.Reset();
            sw.Reset();
        }
    }

Inside initializecomponent method i added

this.AppStateChanged += new AppStateChangedEventHandler(this.MainForm_AppStateChanged);

From inside the thread i changed the appstate property as

this.AppState = 1;

Now there are two problems

1)The first and important is vs complains that cross thread operation is being performed even though i am accessing GUI elements from the event handler.(i know how to handle this using invoke required and a delegate but i just need to know why it is like this)

2)VS keeps warning that AppStateChanged event is not part of Windows.form.

The first problem is whats really irritating me? Any ideas on 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-22T03:24:30+00:00Added an answer on May 22, 2026 at 3:24 am

    Edit: D’oh, I misread your question. You want to know why VS complains? It was a design decision by the C# language team to avoid the multitude of problems that cross-thread access to form controls has.

    Technically there’s no reason to disallow this, but it’s better to break in a consistent and easily fixable way than to fail in weird ways seemingly at random due to two threads manipulating a non-threadsafe control.


    The event is executed on the thread that fires it (your other thread), not on the one that registers it (the main/GUI thread).

    To solve this problem, you have to make sure the event code is invoked on the correct thread to allow accessing GUI elements, which can be done like this:

    public void MainForm_AppStateChanged(int val)
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new Action<int>(MainForm_AppStateChanged), val);
        }
        else
        {
            if (val == 1)
            {                
                totDwn.Text = "00:00:00";
                totAct.Text = "00:00:00";                
            }
            else if (val == 0)
            {                
                tt.Reset();
                sw.Reset();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi-threaded C++ app which does 3D rendering with the OpenSceneGraph library.
I have a multi-threaded Windows C++ app written in Visual Studio 6. Within the
I have a multi-threaded Windows application that occasionally deadlocks. Inevitably this happens on a
I have some questions about multi-threaded programming and multi-core usage. In particular I'm wondering
I have an object in a multi-threaded environment that maintains a collection of information,
In a C# windows forms application. I have a splash screen with some multi-threaded
I have a multi-threaded .Net app in which thread-local storage is used to hold
I currently have heavily multi-threaded server application, and I'm shopping around for a good
I have a .NET (C#) multi-threaded application and I want to know if a
I have been trying to learn multi-threaded programming in C# and I am confused

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.