0

I have an ActiveX control ("rumba" in my code) that connects to a mainframe. The code below works in WinForms, but I want to put the code in a class library project. With what do I replace Application.DoEvents()? I tried putting Thread.Sleep or just deleting the row but then rumba only starts to connect when the while cycle is done.

Here is my code:

    public bool Connect(int timeOutInSeconds = 5)
    {
        DateTime limit = DateTime.Now.AddSeconds(timeOutInSeconds);

        rumba.Connect();

        while (DateTime.Now < limit)
        {
            string s = GetCurrentScreenAsText();
            s = s.Replace("\0", null);

            if (!string.IsNullOrWhiteSpace(s))
                return true;

            Application.DoEvents();
        }

        return false;
    }
kovacs lorand
  • 741
  • 7
  • 23
  • 4
    You shouldn't be using `DoEvents` at all in an operation like this, even if it's not in a class library. You should be performing this operation asynchronously. – Servy Nov 30 '15 at 16:38
  • I would take a look at this [Threading Windows Forms](http://www.yoda.arachsys.com/csharp/threads/winforms.shtml) – MethodMan Nov 30 '15 at 16:42
  • Possible duplicate of [Use of Application.DoEvents()](http://stackoverflow.com/questions/5181777/use-of-application-doevents) – cassandrad Nov 30 '15 at 16:49

1 Answers1

0
  1. If you really want to use it, you can always include system.windows.forms.dll to your project.

2, the only reason you call Application.DoEvent is allowing the application to handle other events when your event is processing. Hence, you want to wrap your event in a separate thread such as using a BackgroundWorker or a Thread object to emulate an async call. In WPF, there is a Dispatch you can invoke or beginInvoke to execute a method asynchronously.

sowen
  • 1,090
  • 9
  • 28
  • You shouldn't be using a new thread at all. There's no reason for there to be additional threads here. It should be done asynchronously, but without creating any new threads. – Servy Nov 30 '15 at 16:52