Console application with signal pump

I am trying to write a console application (C # .NET) that uses unmanaged code in a third-party DLL.

External assembly makes extensive use of callbacks. In the sample application, I use Windows Forms.

The code sample (Windows Forms) uses ThreadPool.QueueUserWorkItem

ThreadPool.QueueUserWorkItem(new WaitCallback(ConnectToControlPanel));

It would seem that with window forms there is a message pump that handles callbacks. This does not apply to the console application, so I need to create my own.

I tried to add a reset event

private static ManualResetEvent resetEvent = new ManualResetEvent(false);

static void main()
{
      ThreadPool.QueueUserWorkItem(new WaitCallback(ConnectToControlPanel));
      resetEvent.WaitOne();
}

private void ConnectToControlPanel()
{
   //Call external function from unmanaged DLL
   resetEvent.Set();
}

This does not work.

My problem seems very like this , but there is no real solution other than using Win.Forms and calling Application.DoEvents ()

Edit

I changed my code this way:

Task task = Task.Factory.StartNew(() => ConnectToControlPanel());
            while (task.Status != TaskStatus.RanToCompletion)
            {
                Application.DoEvents();
                Thread.Sleep(500);
            }

, , , .

, , .

, Application.DoEvents(), , , - , .

+4

Source: https://habr.com/ru/post/1682557/


All Articles