'由于应用程序正在调度输入同步呼叫,因此无法进行传出呼叫。 (RPC_E_CANTCALLOUT_ININPUTSYNCCALL))'

时间:2019-04-04 02:36:36

标签: c#

我刚接触C#,正在尝试为特定功能创建网络浏览器 我有Form1(一个不可见的表单),该表单调用Form2(浏览器)并进行监视,以确保Form2始终在运行,如果空闲,请关闭并重新打开Form2

我认为我遇到了线程问题,我将其设置为运行计时器(这是我解决问题的唯一方法)

我确定只有在尝试从线程内部调用函数时,它才能启动Form2

using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using Browselite;
using System.Diagnostics;
using System.Threading;

namespace BrowseLite
{

    public partial class Form1 : Form
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool SetProcessDPIAware();

        public static Boolean IdleTimeoutEnabled { get; private set; }
        public static int IdleTimeout { get; private set; }
        public static Boolean ClearRunning { get; private set; }
        public Form2 Browser { get; private set; }
        public static Boolean programmaticClose { get; set; }
        public static Boolean Form2Open { get; set; }

        public Form1()
        {
            InitializeComponent();

        }


        [DllImport("user32.dll")]
        public static extern Boolean GetLastInputInfo(ref tagLASTINPUTINFO plii);
        public struct tagLASTINPUTINFO
        {
            public uint cbSize;
            public Int32 dwTime;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                using (RegistryKey RootKey = Registry.CurrentUser.OpenSubKey("Software\\Policies\\BrowseLite"))
                {
                    try
                    {

                        Form1.IdleTimeout = Int32.Parse(RootKey.GetValue("IdleTimeout", -1, RegistryValueOptions.None).ToString());
                        if (Form1.IdleTimeout <= 0)
                        {
                            Form1.IdleTimeoutEnabled = false;
                        }
                        else
                        {
                            Form1.IdleTimeoutEnabled = true;
                        }
                    }
                    catch
                    {
                        Form1.IdleTimeout = 0;
                        Form1.IdleTimeoutEnabled = false;
                    }

                }
            }
            catch
            {
                Form1.IdleTimeout = 0;
                Form1.IdleTimeoutEnabled = false;
            }


            Thread Timer = new Thread(new ThreadStart(MyTimer));

            Browser = new Form2();
            OpenBrowser();

            Timer.Start();
        }

        private void MyTimer()
        {
            while (true)
            {
                FormCollection OpenForms = Application.OpenForms;
                foreach (Form OpenForm in OpenForms)
                {
                    if (OpenForm.Name.Contains("Form2"))
                    {
                        Form1.Form2Open = true;
                    }
                }
                if (!Form1.Form2Open)
                {
                    Browser.ShowDialog();
                    Form1.Form2Open = true;
                }


                tagLASTINPUTINFO LastInput = new tagLASTINPUTINFO();
                Int32 IdleTime;
                LastInput.cbSize = (uint)Marshal.SizeOf(LastInput);
                LastInput.dwTime = 0;

                if (GetLastInputInfo(ref LastInput))
                {
                    IdleTime = System.Environment.TickCount - LastInput.dwTime;
                    int IdleTimeSet = IdleTimeout * 60 * 1000;
                    if (Form1.IdleTimeoutEnabled)
                    {
                        if (IdleTime >= IdleTimeSet)
                        {
                            if (Form1.ClearRunning == false)
                            {
                                CloseBrowser();
                                OpenBrowser();
                            }
                        }
                        else
                        {
                            Form1.ClearRunning = false;
                        }
                    }
                }
                Thread.Sleep(1000 * 30); //Time in seconds (30)
            }
        }


        private void CloseBrowser()
        {
            Form1.programmaticClose = true;
            Browser.Close();
        }

        private void OpenBrowser()
        {
            Form1.programmaticClose = false;
            Form1.Form2Open = true;
            Browser.ShowDialog();
        }
    }
}

任何帮助将不胜感激,但是正如我所说的...我对此并不满意。

1 个答案:

答案 0 :(得分:1)

对于其他偶然发现的人,我自己找到了答案

如果在线程中设置变量。而不是使用

  

Form1.Running = true;

代替使用

  

BeginInvoke(new Action(()=> Form1.Running = true),null);

如果从线程内调用函数,则使用

  

BeginInvoke(new InvokeDelegate( FUNCTION ));

这似乎已经完全解决了我的问题