我在 ASP.net 网站上托管 SignalR 服务器,然后我想在客户端 WinForms 应用程序中打开 FolderBrowserDialog。服务器和客户端都正确连接,我什至在客户端收到消息,但是当我尝试在新线程中打开 FolderBrowserDialog 时,它只是没有打开它,之后什么也不做。
这是从 WebForms 发送消息的代码:
public void sendToSpecific(string name, string message, string to)
{
// Call the broadcastMessage method to update clients.
Clients.Caller.broadcastMessage(name, message);
Clients.Client(dic[to]).broadcastMessage(name, message);
}
Winforms 代码是:
private string _selectedFolder;
HubConnection hubConnection;
IHubProxy hubProxy;
public Form1()
{
InitializeComponent();
hubConnection = new HubConnection("http://localhost:49335");
hubProxy = hubConnection.CreateHubProxy("ChatHub");
hubProxy.On<string, string>("broadcastMessage", (name, message) => checkMessage(message));
hubConnection.Start().Wait();
hubProxy.Invoke("Notify", "Console app", hubConnection.ConnectionId);
}
private void checkMessage(string message)
{
if (message == "Open")
{
var t = new Thread((ThreadStart)(() =>
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.RootFolder = System.Environment.SpecialFolder.MyComputer;
fbd.ShowNewFolderButton = true;
if (fbd.ShowDialog() == DialogResult.Cancel)
_selectedFolder = "Cancelled";
else
{
_selectedFolder = fbd.SelectedPath;
}
}));
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
lstMessages.Items.Add(_selectedFolder);
hubProxy.Invoke("Send", "Console app", _selectedFolder).Wait();
}
}
谁能告诉我我做错了什么,我也已经在 static void Main() 之前设置了[STAThread] 。