简体   繁体   English

在创建 window 句柄之前,无法在控件上调用 c# Invoke 或 BeginInvoke System.Threading.Thread

[英]c# Invoke or BeginInvoke cannot be called on a control until the window handle has been created System.Threading.Thread

not very expert of Threading under Windows.不是Windows下的线程专家。 I have a Main WinForm that opens a child form in it's ctor.我有一个 Main WinForm 在它的 ctor 中打开一个子表单。

public partial class Main : Form
{
    public Main()
    {
        InitializeComponent();

        ImgRxUI formStart = new ImgRxUI();
        formStart.MdiParent = this;
        formStart.WindowState = FormWindowState.Maximized;
        formStart.Show();
    }

etc.. ETC..

The ImgRxUI Form (child form) starts a Thread passing to 2 Actions (delegates in simple form). ImgRxUI 表单(子表单)启动一个传递给 2 个操作(简单形式的委托)的线程。

 public partial class ImgRxUI : Form
{

    private ImgReceiver oImgReceiver = null;

    public ImgRxUI()
    {
        InitializeComponent();

        this.WindowState = FormWindowState.Maximized;
        this.ShowIcon = false;

        oImgReceiver = new ImgReceiver(UpdateImage, Log);
        oImgReceiver.startService();

    }



    public void UpdateImage(byte[] ProtocolType)
    {
       ...do stuff...

    }

    public void Log(string Text)
    {
        this.Invoke((MethodInvoker)delegate
        {
            LogMethod(Text);
            tLog.ScrollToCaret();
        });

    }

    private void LogMethod(string Text)
    {
        tLog.AppendText(Text + Environment.NewLine);
    }

The ImgReceiver as I said starts a thread that listens on a socket...正如我所说的 ImgReceiver 启动了一个在套接字上侦听的线程......

 public class ImgReceiver
{

    private Action<byte[]> ImgReceived;
    private Action<string> Log;
    private System.Threading.Thread Thread_ImgReceiver = null;

    public ImgReceiver(Action<byte[]> ImageReceivedDelegate, Action<string> LogDelegate)
    {
        
        this.ImgReceived = ImageReceivedDelegate;
        this.Log = LogDelegate;
    }

    public void startService()
    {
        Thread_ImgReceiver = new System.Threading.Thread(startListening);
        Thread_ImgReceiver.IsBackground = true;
        Thread_ImgReceiver.Start();
    }

    [SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)]
    public void killService()
    {
        Thread_ImgReceiver.Abort();
        System.Threading.Thread.Sleep(1000);
    }

    public void startListening()
    { ...do stuff...}

When I close the ImgRxUI form the following event on the form itself gets called当我关闭 ImgRxUI 表单时,表单本身的以下事件被调用

 private void ImgRxUI_FormClosing(object sender, FormClosingEventArgs e)
    {
        oImgReceiver.killService();            
    }

Hear rises the error in the title.听起标题中的错误。

Wht?什么?

Thaks塔克斯

Change the kill Service method to将 kill Service 方法更改为

[SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)]
public void killService(Action action)
{
   action.Invoke();
   System.Threading.Thread.Sleep(1000);
}

Change the access level Thread_ImgReceiver to public将访问级别 Thread_ImgReceiver 更改为 public

public System.Threading.Thread Thread_ImgReceiver = null;

and call killService to并调用 killService 到

private void ImgRxUI_FormClosing(object sender, FormClosingEventArgs e)
{  
    oImgReceiver.killService(new Action(delegate { oImgReceiver.Thread_ImgReceiver.Abort(); }));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#编译错误:“在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke。” - C# compile error: “Invoke or BeginInvoke cannot be called on a control until the window handle has been created.” VS 2022 C# Forms:System.InvalidOperationException:“在创建 window 句柄之前,无法对控件调用 Invoke 或 BeginInvoke。” - VS 2022 C# Forms: System.InvalidOperationException: 'Invoke or BeginInvoke cannot be called on a control until the window handle has been created.' $exception {在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke。} System.InvalidOperationException - $exception {Invoke or BeginInvoke cannot be called on a control until the window handle has been created.} System.InvalidOperationException 在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke - Invoke or BeginInvoke cannot be called on a control until the window handle has been created 错误:在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke - Error: Invoke or BeginInvoke cannot be called on a control until the window handle has been created InvokeEvent:在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke - InvokeEvent: Invoke or BeginInvoke cannot be called on a control until the window handle has been created 在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke - Invoke or BeginInvoke cannot be called on a control until the window handle has been created 我只能关闭一次表单,在创建窗口句柄之前,无法在控件上调用InvalidOperation Exception Invoke或BeginInvoke - I can only close a form once, InvalidOperation Exception Invoke or BeginInvoke cannot be called on a control until the window handle has been created “在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke”仅在第二次打开表单时出现 - “Invoke or BeginInvoke cannot be called on a control until the window handle has been created” only occurs the second time form opens 我只能关闭一次表单,在创建窗口句柄之前,无法在控件上调用InvalidOperation Exception Invoke或BeginInvoke - I can only close a form once, InvalidOperation Exception Invoke or BeginInvoke cannot be called on a control until the window handle has been created
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM