עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

cross-threading.. בעיה

Featured Replies

פורסם

טוב אז ככה,

בניתי תוכנת צ'אטים, ובלקיינט השתמשתי ב TcpClient,

עכשי ובשביל לקבל response מהשרת השתמשתי בת'ראד חדש עם לולאה,

זה עובד מצוין והכל, אבל הבעיה שיש קטע שאני רוצה להוסיף פריט ל ListBox, אבל כשאני מנסה להוסיף הת'ראד פשוט נתקע..

הינה הקוד:


public static void GetReq()
{
while (true)
{
try
{
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();

// Receive the TcpServer.response.

// Buffer to store the response bytes.
Byte[] data = new Byte[256];

// String to store the response ASCII representation.
String responseData = String.Empty;

// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);

if (responseData == "-kill")
MessageBox.Show("Disconnected: Server shutting down");

if (responseData.Contains("-addnick"))
{
char[] delimiterChars = { ':' };
string[] nick = responseData.Split(delimiterChars);

lst_users.Items.Add(nick[1]);
}

MessageBox.Show("Client received: " + responseData);

}
catch { }
}
}

מה אני צריך לעשות כדי שזה יפעל?

פורסם

היי בומר, הקוד הבעייתי שלך הוא הקטע הבא.


if (responseData.Contains("-addnick"))
{
char[] delimiterChars = { ':' };
string[] nick = responseData.Split(delimiterChars);

lst_users.Items.Add(nick[1]);
}

במקום השורה של ההוספה כמו שעשית אתה צריך לעשות את הדבר הבא (בכל תוכנית שתבנה רק הת'רד המרכזי או זה שבחרת בו לטפל ב GUI הוא היחידי שיכול לטפל ב GUI)



this.Invoke(new MethodInvoker(delegate() { lst_users.Items.Add(nick[1]); }));

שים לב ששמתי את הכול בשורה אחת, ניתן לפצל זאת לכמה שורות ע"י

הקוד הבא(למקרה שיש יותר מסתם להוסיף משתמש 1)


if (responseData.Contains("-addnick"))
{
char[] delimiterChars = { ':' };
string[] nick = responseData.Split(delimiterChars);

this.Invoke(new MethodInvoker(add_user);
}

public void add_user()// the function has to return void and cannot accept parameters.
{
lst_users.Items.Add(nick[1]);

}

פורסם
  • מחבר

תודה רבה!

אני אנסה את זה מאוחר יותר :)

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.