C#如何在单独的类和线程中更改textbox.Text的静态方法
所以我遇到的问题是,我想在Form1上设置textbox.Text。我很快发现了线程安全的问题,我认为我的代码解决了这个问题。我面临的下一个问题是,我的客户机的编码方式——设置文本的方法——必须是静态的,我不知道该怎么做。当然,除非AsyncClient类中的客户机方法不必是静态的,如果是这种情况并且是由于我缺乏知识,那就太好了 正如您可能知道的那样,我正在为客户重新设计来自Microsoft的模板。这主要是因为我是一名大学生,我只是利用它来学习 如果这个问题已经存在,我也想提前道歉,但当我查看时,我找不到任何足够具体的问题 这是代码的顶部减去名称空间。我将把它稍微分开,只是为了集中在我所说的部分C#如何在单独的类和线程中更改textbox.Text的静态方法,c#,multithreading,class,sockets,static-methods,C#,Multithreading,Class,Sockets,Static Methods,所以我遇到的问题是,我想在Form1上设置textbox.Text。我很快发现了线程安全的问题,我认为我的代码解决了这个问题。我面临的下一个问题是,我的客户机的编码方式——设置文本的方法——必须是静态的,我不知道该怎么做。当然,除非AsyncClient类中的客户机方法不必是静态的,如果是这种情况并且是由于我缺乏知识,那就太好了 正如您可能知道的那样,我正在为客户重新设计来自Microsoft的模板。这主要是因为我是一名大学生,我只是利用它来学习 如果这个问题已经存在,我也想提前道歉,但当我查看
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Socket client;
public delegate void setTextCallback(string text);
public void setText(string text)
{
if (this.txtLog.InvokeRequired)
{
// Different thread, use Invoke.
setTextCallback d = new setTextCallback(FinishSetText);
this.Invoke(d, new object[] { text });
}
else
{
// Same thread, no Invoke.
this.txtLog.Text = this.txtLog.Text + text;
}
}
private void FinishSetText(string text)
{
this.txtLog.Text = this.txtLog.Text + text;
}
以上是代码,就错误而言,它修复了我遇到的交叉践踏问题。如果有更好的方法,我愿意尝试新事物
// State object for receiving data from remote device.
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 256;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
public class AsyncClient
{
// The port number for the remote device.
private const int port = 8080;
// ManualResetEvent instances signal completion.
private static ManualResetEvent connectDone =
new ManualResetEvent(false);
private static ManualResetEvent sendDone =
new ManualResetEvent(false);
private static ManualResetEvent receiveDone =
new ManualResetEvent(false);
public struct properties
{
public static String response { get; set; }
}
public static Socket StartClient()
{
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
// Create a TCP/IP socket.
Socket client = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();
return client;
}
catch (Exception e)
{
setText(e.ToString());
return client;
}
}
private static void ConnectCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete the connection.
client.EndConnect(ar);
setText(string.Format("Socket connected to {0}",
client.RemoteEndPoint.ToString()));
// Signal that the connection has been made.
connectDone.Set();
}
catch (Exception e)
{
setText(e.ToString());
}
//Other AsyncClient methods below.
}
在代码的上方底部,您可以看到我试图使用Form1中的方法设置文本的位置,但您也可以看到这些方法是静态的,这意味着它需要该方法的静态版本,这意味着使用
This.textbox
不再起作用,或者至少这是我对正在发生的事情的理解。任何帮助都将不胜感激 删除static
关键字,并将此代码添加到AsyncClient
类的开头:
public class AsyncClient
{
private Action<string> setText;
public AsyncClient(Action<string> setText)
{
this.setText = setText;
}
公共类异步客户端
{
私人行动;
公共异步客户端(Action setText)
{
this.setText=setText;
}
然后,当您从表单创建实例时,只需执行
var ac=new AsyncClient(setText)
并调用ac.StartClient()
“设置文本的方法必须是静态的”-不,当然不是。我并不是说这正是我假设的事实,我确实在这里发布了帮助。然后将AsyncClient
的方法设置为实例方法,而不是static
。然后你必须构建AsyncClient
客户端的实例,然后你可以传递操作setText代码>到构造函数。