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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:42:25+00:00 2026-05-23T13:42:25+00:00

I have the main component of my program which runs in the main thread.

  • 0

I have the main component of my program which runs in the main thread. The program has the ability to save the user’s work to a file, however extra meta-information is saved along with the user’s work.

Example:

[Serializable]
public Class UserWork
{
  // Lots of things in here. Lots of work user has done, etc...
}

In the program, I store a couple things like settings, etc. Let’s assume these are bool flag1 and bool flag2. I have a structure like this:

[Serializable]
public class SavedFile
{
  UserWork userWork;
  bool flag1;
  bool flag2;
}

When I save:

saveData(new SavedFile(userWork, flag1, flag2));

When I load:

SavedFile savedFile = SavedFile.Load(path);
UserWork userWork = savedFile.userWork;
bool flag1 = savedFile.flag1;
bool flag2 = savedFile.flag2;

So basically, flag1 and flag2 need to be saved with the work, but they don’t necessarily belong to with the work. They actually represent how far through the workflow the user is at the time of save.

Now, I have built an AutoSave feature which monitors events which are fired whenever the user makes any modifications. The UserWork class fires events, so this was easy to do. The issue I’m having is that I’m not sure how to send and store the references to the flags in the AutoSave thread.

I have:

public class AutoSave // (This code runs in a separate thread)
{
  bool flag1;
  bool flag2;

  UserWork userWork;

  public AutoSave(UserWork userWork, ref bool flag1, ref bool flag2)
  {
    this.flag1 = flag1;
    this.flag2 = flag2;

    this.userWork = userWork;

    userWork.workUpdated += new WorkUpdatedHandler(save);
  }

  void save(object sender, WorkUpdateArgs e)
  {
    saveDate(new SavedFile(userWork, flag1, flag2));
  }
}

When I run my code (which is not the above code, but similar code), the flags are both always false regardless of what their values are in my main thread.

Edit for clarification: The issue is not that I cannot get this to work with other workarounds. For example, I can make the two flags public in the AutoSave class and update the main flags and the autosave flags simultaneously. i.e. flag1 = autoSave.Flag1 = true. Rather, the issue is that this is cumbersome. It means I must remember to do this each and every time any of the flags get updated. Also, it doesn’t make sense to be updating several variables with the same data all the time when I can simply have one copy in memory and reference it in several places.

  • 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-23T13:42:26+00:00Added an answer on May 23, 2026 at 1:42 pm

    You can’t use ref like that – the code you’ve shown won’t compile. You’d have to take the ref modifier off your instance variables. Whatever was passed into the AutoSave constructor is entirely independent from the instance variables as soon as they’ve been assigned.

    You could use a wrapper class:

    public class Wrapper<T>
    {
        public T Value { get; set; }
    }
    

    and pass in two Wrapper<bool> references – then the caller could do

    flag.Value = true;
    

    and that change would be visible in AutoSave. It’s pretty nasty though… I’d try to avoid that pattern if you can.

    Can whatever’s changing the flags not raise an event too, so you could capture that in AutoSave?

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

Sidebar

Related Questions

This is my code I have developed. This is the main program which holds
I have main table called 'Employee' and another slave table called 'EmployeeTypes' that has
i have a table called category in which i have main category ids and
I have a main asp.net app, which is written in asp.net 1.1. Runnning underneath
I have a main window (#1) on my webpage from which I open a
I have a main canvas 'blackboard' in a panel, this canvas has itself several
I have my main GUI thread, and a second thread running inside it's own
I have a Flex application which references a separate MXML file as a template
My Goal I would like to have a main processing thread (non GUI), and
My program have 3 classes. 1) main, 2) frame, 3) drawingBoard. The logic of

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.