我想在我的 Backgroundworker 中更改 TextBox 的文本,但它不起作用,我在我的代碼中找不到錯誤。
后臺作業者:
class Check_Server
{
WebSocket webSocket = new WebSocket("ws://localhost:8548");
private Form1 Form1;
public void Check_WebSocket(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
while (!worker.CancellationPending)
{
Console.WriteLine("checking connection");
if (webSocket.IsAlive == true)
{
Console.WriteLine("connected");
}
else
{
Console.WriteLine("not connected");
Form1 form = new Form1();
form.SetText("XYZ");
MakeNewConnection();
}
}
}
void MakeNewConnection()
{
webSocket.Connect();
Thread.Sleep(10000);
}
}
形式:
public void SetText(string text)
{
if (InvokeRequired)
{
this.Invoke((MethodInvoker)delegate () { SetText(text); });
return;
}
textBox1.Text = text;
}
uj5u.com熱心網友回復:
您不顯示您的表單。添加form.Show()或form.ShowDialog(),取決于您是否要等到用戶關閉表單。
此外,我發現此擴展方法對于對控制元件進行執行緒安全更改非常有用。像這樣使用它:textBox1.InvokeSave(() => textBox1.Text = text);。
public static partial class ControlExtensions
{
public static void InvokeSave(this Control control, Action action)
{
Delegate del = action;
try
{
if (control.InvokeRequired)
{
control.Invoke(del);
}
else action();
}
catch (ObjectDisposedException)
{
// guess do nothing is fine then
}
}
}
uj5u.com熱心網友回復:
使用 Mariusz Kotas 的一個庫,Winforms 中的 Websockets 變得更加容易,這也許并不奇怪,Websocket.Client ;如果你打開 VS 的 nuget 包管理器并搜索它,你會找到它(目前大約是 4.3 版)
設定可能如下所示:
private WebsocketClient _cws;
private async void ConnectButton_Click(..){
_cws = new WebsocketClient(new Uri("your uri here"));
_cws.MessageReceived.Subscribe(msg => MessageReceived(msg.Text));
_cws.DisconnectionHappened.Subscribe(info => DoSomething("Disconnection happened: " info));
}
發送訊息可能如下所示:
private async void ConnectButton_Click(..){
_cws.Send(messageTextBox.Text);
}
接收訊息訂閱上面:
private void MessageReceived(string msg)
//local function for logging
void addAndScrollAsync()
{
_messagesListBox.Items.Add( msg);
//this scrolls to show new messages while the listbox is at the bottom, but doesn't if it's been scrolled up
int visibleItems = _messagesListBox.ClientSize.Height / _messagesListBox.ItemHeight;
if (_messagesListBox.Items.Count > visibleItems && _messagesListBox.TopIndex > _messagesListBox.Items.Count - visibleItems - 2)
_messagesListBox.TopIndex = _messagesListBox.Items.Count - visibleItems 1;
}
_messagesListBox.InvokeEx(() => addAndScrollAsync());
}
我有一些輔助擴展來執行任何呼叫:
public static class ControlExtensions
{
public static TResult InvokeEx<TControl, TResult>(this TControl control,
Func<TControl, TResult> func)
where TControl : Control
{
return control.InvokeRequired
? (TResult)control.Invoke(func, control)
: func(control);
}
public static void InvokeEx<TControl>(this TControl control,
Action<TControl> func)
where TControl : Control
{
control.InvokeEx(c => { func(c); return c; });
}
public static void InvokeEx<TControl>(this TControl control, Action action)
where TControl : Control
{
control.InvokeEx(c => action());
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/332917.html
下一篇:在C#中使用EHLLAPI從IBM的PersonalCommunicationsiSeries獲取字串讓我得到字串后跟垃圾
