Author Topic: Alternative to DoEvents  (Read 3667 times)

Offline Erel

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
Alternative to DoEvents
« on: March 08, 2012, 09:31:06 PM »
I'm looking into WCL in order to allow our application to connect to Android devices.
The first initial tests seem to work fine.

I want to handle the complete communication in a background thread. I saw the solution with DoEvents.
This solution is not perfect as it creates a "busy-wait" loop.
What do you think about this solution based on Application.Run (it only demonstrates the code that stops the message loop):
Code: [Select]
        private volatile SynchronizationContext sc;
        public Form1()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
        }

        void Form1_Load(object sender, EventArgs e)
        {
            Thread t2 = new Thread(new ThreadStart(TS));
            t2.Name = "t2";
            t2.Start();
        }
        void TS()
        {
            //connect here...

            new Button(); //creates the SynchronizationContext
            sc = SynchronizationContext.Current;
            Application.Run();
            Console.WriteLine("after");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SendOrPostCallback ld = delegate(object o)
            {
                Console.WriteLine("running: " + Thread.CurrentThread.Name);
                Application.ExitThread();
            };
            sc.Post(ld, null);
        }

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: Alternative to DoEvents
« Reply #1 on: March 08, 2012, 11:34:20 PM »
Hello,

WCL is asynchronouse library and usualy you do not any additional threads in your application as WCL doesn;t block UI. However if you need a threads your thread must have a message loop in your thread because WCL uses windows messages internaly (that's why you need a DoEvent - it process messages).

Offline Erel

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
Re: Alternative to DoEvents
« Reply #2 on: March 09, 2012, 12:08:21 PM »
I understand that it needs to process the message queue.
My code uses Application.Run instead of a loop with DoEvents. I didn't check it with WCL library however from my understanding it should work (and is a better solution than the DoEvents solution).

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: Alternative to DoEvents
« Reply #3 on: March 09, 2012, 12:45:54 PM »
Application.Run simple starts message loop (internally in .NET framework classes).

But in your thread, to prevent a thread from exiting (as you know thread ends when its function exists) you have to "lock" thread function somehow. Usualy its simple loop.
So if you create a WCL object in thread and then your thread loops then no one windows message will be processed in this locked thread. To process that messages you must call DoEvent method.

Offline Erel

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
Re: Alternative to DoEvents
« Reply #4 on: March 11, 2012, 06:47:16 PM »
Actually Application.Run starts handling the message loop. The thread will only exit after you call Application.ExitThread.

I've already implemented it and it works fine.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: Alternative to DoEvents
« Reply #5 on: March 11, 2012, 06:56:53 PM »
Not so good way as you placed all application message loop into separate thread. So the question: why do yo uneed thread if main message loop is in it?

Offline Erel

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
Re: Alternative to DoEvents
« Reply #6 on: March 12, 2012, 10:14:37 AM »
It is not the same message loop. It is a message loop specific to this thread.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: Alternative to DoEvents
« Reply #7 on: March 12, 2012, 10:30:50 AM »
Yes, of course. Any way if it works for you than it's great.

Offline Erel

  • Newbie
  • *
  • Posts: 7
  • Karma: 0
Re: Alternative to DoEvents
« Reply #8 on: March 12, 2012, 05:57:57 PM »
Just ordered the commercial version a few minutes ago :)


Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3055
  • Karma: 1000
    • Soft Service Company
Re: Alternative to DoEvents
« Reply #9 on: March 12, 2012, 05:59:44 PM »
Thank you for your order. Registration has been sent as our records show. :)

 

Sitemap 1 2 3 4 5 6 7 8 9