我有一个 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?