【发布时间】:2017-11-29 23:25:48
【问题描述】:
所以我遇到的问题是我想在 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 中的方法设置文本的位置,但您也可以看到这些方法是静态的,这意味着它需要该方法的静态版本,这意味着使用 @987654323 @ 不再起作用,或者至少这是我对正在发生的事情的理解。任何帮助将不胜感激。
【问题讨论】:
-
“设置文本的方法必须是静态的”——不,当然不是。
-
我并没有说事实上这正是我认为正在发生的事情,毕竟我确实在这里发帖寻求帮助。
-
然后将
AsyncClient的方法设为实例方法,而不是static。然后你必须构造一个AsyncClient客户端的实例,然后你可以将Action<string> setText传递给构造函数。
标签: c# multithreading class sockets static-methods