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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:06:47+00:00 2026-05-18T10:06:47+00:00

In Qt, there is a nice idiom to have each object associated with a

  • 0

In Qt, there is a nice idiom to have each object associated with a thread, so that all its event handlers will only run in that thread (unless called directly, of course).

Is there anything even remotely like that in C#/.NET? If not, how would you start writing your own?

Example:

// threaded.h
#include <QThread>
#include <QDebug>
#include <QtGlobal>

class ThreadedObject : public QObject {
    Q_OBJECT
public:
    ThreadedObject(const QString &name){
        Name = name;
        // the default QThread implementation is an empty event loop
        Thread = new QThread(this);
        moveToThread(Thread);
        Thread->start();
    }

public slots:
    void tick() {
        qDebug() << Name << "in thread" << (int)QThread::currentThreadId();
    }

private:
    QThread *Thread;
    QString Name;
};

and

// main.cpp
#include <QtCore/QCoreApplication>
#include <QTimer>
#include "threaded.h"


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    ThreadedObject *foo = new ThreadedObject("Foo");
    QTimer footimer;
    QObject::connect(&footimer, SIGNAL(timeout()), foo, SLOT(tick()));

    ThreadedObject *bar = new ThreadedObject("Bar");
    QTimer bartimer;
    QObject::connect(&bartimer, SIGNAL(timeout()), bar, SLOT(tick()));

    qDebug() << "Main thread is" << (int)QThread::currentThreadId();

    footimer.start(1300);
    bartimer.start(3240);

    return a.exec();
}

will output:

Main thread is 3916 
"Foo" in thread 3824 
"Foo" in thread 3824 
"Bar" in thread 3920 
"Foo" in thread 3824 
...
  • 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-18T10:06:48+00:00Added an answer on May 18, 2026 at 10:06 am

    WPF Dispatcher!

    using System;
    using System.Windows.Threading;
    using System.Threading;
    
    namespace dispatchertest
    {
        public class Dispatched : DispatcherObject
        {
            readonly object Lock = new object();
            readonly string _name;
            public string Name { get { return _name; } }
    
            public Dispatched(string name) {
                this._name = name;
            }
    
            public void tick(object sender, EventArgs e) {
                lock ( Lock ) {
                    Console.WriteLine("{0} in thread {1}", Name, Thread.CurrentThread.ManagedThreadId);
                }
            }
        }
    
        class Program
        {
            static void Main(string[] args) {
    
                var timer = new DispatcherTimer(DispatcherPriority.Send, Dispatcher.CurrentDispatcher);
    
                Thread thread1 = new Thread(() => {
                    var d2 = Dispatcher.CurrentDispatcher;
                    var foo = new Dispatched("Foo");
    
    
                    var timer1 = new DispatcherTimer(DispatcherPriority.Send, Dispatcher.CurrentDispatcher);
                    timer1.Interval = new TimeSpan(0,0,0,0, milliseconds: 809);
                    timer1.Tick += foo.tick;
    
    
                    timer1.Start();
                    Dispatcher.Run();
                });
    
                var bar = new Dispatched("Bar");
                timer.Tick += bar.tick;
    
                thread1.Start();
    
                timer.Interval = new TimeSpan(0,0,0,0, milliseconds: 1234);
                timer.Start();
                Dispatcher.Run();
            }
        }
    }
    

    Output:

    Foo in thread 10
    Bar in thread 9
    Foo in thread 10
    Foo in thread 10
    Bar in thread 9
    Foo in thread 10
    Bar in thread 9
    Foo in thread 10
    Foo in thread 10
    Bar in thread 9
    Foo in thread 10
    Bar in thread 9
    Foo in thread 10
    Foo in thread 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got some binary files containing integers. Is there some nice Unix command, that
There's a nice plugin for Frog CMS that lets you just type in yourpicture.120x120.jpg
There is nice one for java - MINA . Once I've heard that there
Is there a nice and easy way to have a Colorbox act as a
Is there a nice way to do this in Objective-C, or do I have
Is there a nice and tested piece of code out there that I can
Is there a nice way to have Spring's @Controller classes to call a specific
I'm pretty sure ruby has an idiom for that. I just have too many
Is there a nice way to extract tokens that start with a pre-defined string
Is there any nice tool to check memory leaks in android? any help will

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.