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

  • SEARCH
  • Home
  • 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 7512537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:53:41+00:00 2026-05-29T23:53:41+00:00

Note : Although in some similar cases BackgroundWorker is recommended I prefer to do

  • 0

Note :
Although in some similar cases BackgroundWorker is recommended
I prefer to do this job without it, cause this way I could make more generic code
that could be used across my applications, and it will use my naming conventions.

What I want :
Show my arraylist inside MyListBox via ShowResult(…) Method

What do you suggest as a best practice in this case

Also maybe I have to move the Focus Method right ater the thread.Start method, which I don’t know

Sample code

btnPress Method :

        SetStatus(" Openning & Initializing the File");
        ThreadStart threadStart = new ThreadStart(HeavyProcess);
        threadProcess = new Thread(threadStart);
        threadProcess.Start();

HeavyProcess Method

    public void HeavyProcess()
    {
        SetStatus("Thread Started!");


        // Filling an ArrayList arrHealth ....            

        ShowResult(...);
        // Filling the ListBox with the ArrayList
        // At this time I want to be sure that the extra thread be finished and it 
        // returned to the GUI thread

The Cross-Thread Solution of mine :

public delegate void SetStatusDelegate(string text);

public void SetStatus(string stStatus)
{
    if (statLLS.InvokeRequired)
        statLLS.Invoke(new SetStatusDelegate(SetStatus),stStatus);
    else
        statLLS.Text = stStatus;
}

— Edit —

Path1 :

The same as above

This will cause an error :
Invalid Operation …
Cross-thread operation not valid …
When I want to access the ListBox and put the Arraylist result in it
It shows that it’s still in the extra thread
how can I leave this “ShowResult(…); ” for the main thread

Path2 :

        threadProcess.Start();                    
        ShowResult(...); 
        // Filling the ListBox with the ArrayList
        // the Watch shows that ArrayList is null

this will cause nothing !

  • 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-29T23:53:43+00:00Added an answer on May 29, 2026 at 11:53 pm

    Your problem is in calling MyListBox.Focus() in the background thread. You can’t do that with a Windows Forms app — you must leave GUI handling to the main GUI thread. A more typical approach would be to poll or wait for the background thread to complete and then do the GUI stuff in the main thread. The point of a BackgroundWorker is that it makes this easy by providing things like the RunWorkerCompleted event to actively notify you when the thread is done.

    Windows Forms does provide a specific mechanism for this kind of thing, though, with the Windows.Forms.Timer (NOT System.Timers.Timer). GUI actions performed by the event handlers attached to the Tick event, even though originating from a separate thread, will work.

    Basically, the format is:

    public partial class Form1 : Form {
    
        private Thread threadProcess;
        private System.Windows.Forms.Timer heavyProcessTimer = new Timer();
    
        public Form1() {
            InitializeComponent();
            heavyProcessTimer.Interval = 50; // or whatever
            heavyProcessTimer.Tick += new EventHandler(heavyProcessTimer_Tick);
        }
    
        void heavyProcessTimer_Tick(object sender, EventArgs e) {
            if (!threadProcess.IsAlive) {
                heavyProcessTimer.Stop();
                MyListBox.Focus();    
                ShowResult(...);
            }
        }
    
        private void btnPress(object sender, EventArgs e) {
            SetStatus(" Openning & Initializing the File"); 
            ThreadStart threadStart = new ThreadStart(HeavyProcess); 
            threadProcess = new Thread(threadStart); 
            threadProcess.Start();
            heavyProcessTimer.Start();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: Although I raise this issue in the context of an iOS app, I
Note: Although my particular context is Objective-C, my question actually transcends programming language choice.
Note: This was posted when I was starting out C#. With 2014 knowledge, I
Note: Originally this question was asked for PostgreSQL, however, the answer applies to almost
Note : The code in this question is part of deSleeper if you want
Note The question below was asked in 2008 about some code from 2003. As
(Note: This is for MySQL's SQL, not SQL Server.) I have a database column
Please note this is a question with regard to Backbone.js. This is not a
Some Background: ASP.net MVC is primarly a paradigm shift in the way you structure
Note: There is a very similar question here . Bear with me, however; my

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.