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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:18:58+00:00 2026-06-15T12:18:58+00:00

I have a custom Queue class inherited from Queue class. It has an event

  • 0

I have a custom Queue class inherited from Queue class. It has an event ItemAdded. In the event handler of this event i am executing a method. But it is running other than main thread, though i want it in main thread. I don’t know how to do it. Any suggestion ?

//My custom class


using System;
using System.Collections; //Required to inherit non-generic Queue class.

namespace QueueWithEvent
{
    public class SmartQueue:Queue
    {

        public delegate void ItemAddedEventHandler(object sender, EventArgs e);

        public event ItemAddedEventHandler ItemAdded;

        protected virtual void OnItemAdded(EventArgs e)
        {
           if (ItemAdded != null)
           {
              ItemAdded(this, e);
           }
    }

    public override void Enqueue(object Item)
    {
        base.Enqueue(Item);
        OnItemAdded(EventArgs.Empty);
    }        

   }
}



 //Winform application

 using System;
 using System.ComponentModel;
 using System.Windows.Forms;
 using QueueWithEvent;

 namespace TestApp
 {
    public partial class Form1 : Form
    {

    SmartQueue qTest = new SmartQueue();

    public Form1()
    {
        InitializeComponent();
        qTest.ItemAdded += new SmartQueue.ItemAddedEventHandler(this.QChanged);
    }

    private void btnStartBgw_Click(object sender, EventArgs e)
    {
        DisplayThreadName();
        bgwTest.RunWorkerAsync();
    }

    private void bgwTest_DoWork(object sender, DoWorkEventArgs e)
    {
        try
        {
            for (int i = 0; i < 11; i++)
            {
                string valueTExt = i.ToString();
                qTest.Enqueue(valueTExt);
                System.Threading.Thread.Sleep(5000);
            }
        }
        catch (Exception Ex)
        {
            MessageBox.Show(Ex.Message);

        }
    }


    private void DisplayThreadName()
    {
        string tName = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString();
        txtThreadName.Text = tName;           
    }

    private void QChanged(object sender, EventArgs e)
    {
        //#########I want this method to run on main thread #############
        DisplayThreadName();
    }
}
}

Thanks in advance.
SKPaul.

  • 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-06-15T12:18:59+00:00Added an answer on June 15, 2026 at 12:18 pm

    You are enqueueing items on background thread (DoWork event handler runs on background thread), thus your event raised also in background thread.

    Use InvokeRequired method to verify if you are on UI thread. And if not, then use Invoke to run code on UI thread:

     private void QChanged(object sender, EventArgs e)
     {
         if (InvokeRequired)
         {
             Invoke((MethodInvoker)delegate { QChanged(sender, e); });
             return;
         }
         // this code will run on main (UI) thread 
         DisplayThreadName();
     }
    

    Another option for you – use ProgressChanged event to enqueue items (don’t forget to set WorkerReportsProgress to true). This event handler runs on UI thread:

    private void bgwTest_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = (BackgroundWorker)sender;
    
        for (int i = 0; i < 11; i++)
        { 
            // use user state for passing data
            // which is not reflecting progress percentage
            worker.ReportProgress(0, i);
            System.Threading.Thread.Sleep(5000);
        }
    }
    
    private void bgwTest_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
         string valueTExt = e.UserState.ToString();
         qTest.Enqueue(valueTExt);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have custom event that has several different subscribers who will all use the
I want to have a priority queue with custom ordering, but lazy as I
If I where to have a Queue holding a collection of objects (Custom object,bool,bool,bool,bool)
I have custom UITableViewCell . It contains UITextLabel . When I press this cell
I have custom validation rule: public function customRule($check) { } Inside this rule I
I have built a variety of little scripts using Ruby's very simple Queue class,
I have a custom log4j appender that writes log messages into a JMS queue.
Possible Duplicate: Priority queue in .Net This question is similar, but i want to
I have created a custom queue where I am doing some processing and thereby
I have created a custom generic queue which implements a generic IQueue interface, which

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.