gpt4 book ai didi

c# - ActiveX 控件 '6bf52a52-394a-11d3-b153-00c04f79faa6' 无法实例化,因为当前线程不在单线程单元中

转载 作者:行者123 更新时间:2023-12-03 12:56:36 25 4
gpt4 key购买 nike

我有一个 C# 主窗口窗体,它监听 UDP 端口以获取消息。收到所需消息后,它将启动另一个表单(视频表单)。这个视频表单由一个 axWindowsMediaPlayer1 组成,它将播放视频。

但是每当它收到启动视频表单的消息时,它就会收到一个 UDP 错误“ActiveX 控件 '6bf52a52-394a-11d3-b153-00c04f79faa6' 无法实例化,因为当前线程不在单线程单元中。”

    private void initCommunication()
{
CheckForIllegalCrossThreadCalls = false;
try
{

// For receiving messages
//We are using UDP sockets
serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);

//Assign the any IP of the machine and listen on port number
IPEndPoint ipEndPoint2 = new IPEndPoint(IPAddress.Any, listeningPort);

//Bind this address to the server
serverSocket.Bind(ipEndPoint2);

IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
//The epSender identifies the incoming clients
EndPoint epSender = (EndPoint)ipeSender;

//Start receiving data
serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length,
SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender);

}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "UDP Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

#region UDP OnReceive
private void OnReceive(IAsyncResult ar)
{
try
{
IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
EndPoint epSender = (EndPoint)ipeSender;

serverSocket.EndReceiveFrom(ar, ref epSender);

//Transform the array of bytes received from the user into an
//intelligent form of object Data
Data msgReceived = new Data(byteData);

switch (msgReceived.strMessage)
{
case "1":
btnPlayVideo_Click(null,null);
break;
}

txtLog.Text += msgReceived.strName + " : " + msgReceived.strMessage + "\r\n";
txtLog.SelectionStart = txtLog.Text.Length;
txtLog.ScrollToCaret();
serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender,
new AsyncCallback(OnReceive), epSender);

}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "UDP OnReceive Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion

错误在这一行中抛出
axWindowsMediaPlayer1 = new AxWMPLib.AxWindowsMediaPlayer();

我在网上搜索过,如何创建 STA?

最佳答案

添加 [STAThread]归因于您的 Main方法。

关于c# - ActiveX 控件 '6bf52a52-394a-11d3-b153-00c04f79faa6' 无法实例化,因为当前线程不在单线程单元中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11063575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com