עבור לתוכן
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.

הפעלת thread עם למבדה ב-C#

Featured Replies

פורסם

יש לי Class שמקבל string ואחד מהמתודות שלו מחזירה

Dictionary<string, string>

אני רוצה לקבל את התוצאה של המתודה הנ"ל כאשר אני עושה את זה ב-Thread נפרד ולכן רשמתי:

כאשר Statistics הוא ה-CLASS שלי ו-getStatistics זו המתודה שמחזירה

<Dictionary<string, string>


Thread<Dictionary<string, string>> myThread = new Thread<Dictionary<string, string>>(
() =>
{
Statistics myClass = new Statistics(path);
return myClass.getStatistics();
});

ואני מקבל שגיאה שאומרת The non-generic type 'System.Threading.Thread' cannot be used with type argument ואני לא מבין למה ומה זה אומר.

עריכה:

בסוף הסתדרתי אבל זה לא נראה כאילו הפעולה באמת נפתחת ב-Thread חדש כי כשהיא מחשבת את מה שהיא צריכה (לפעמים זה לוקח כמה שניות) אז בנתיים כל ה-GUI לא מגיב


private void showStatistics(string path)
{
Statistics myClass = new Statistics(path);


Thread myThread = new Thread(() => myClass.getIpStatistics());
{
foreach (KeyValuePair<string, string> item in myClass.getIpStatistics())
{
listBoxIps.Items.Add(item.Key + item.Value + "\n");
}


myThread.Start();
}
}

פורסם

לא ברור מה עשית כאן... הת'רד שלך אמור לבצע את myClass.getIpStatistics, אבל אתה גם קורא לפונקציה הזו מחוץ לת'רד, ב-foreach.

פורסם
  • מחבר

איך הצורה הנכונה לרשום את זה ?

פורסם
  • מחבר

ליצור את ה-CLASS הזה ולקלוט את ה-LIST שהפונקציה שלו מחזירה ב-THREAD נפרד

וכמובן שאשמח להסבר קצרצר של למה זו לא הסתדר לי ואיך עושים את זה נכון

פורסם

אז למה לא שמת את הקוד של ה-foreach גם כן בתוך הפונקציה שאתה מעביר לת'רד?

פורסם
  • מחבר

חשבתי שאם ה-foreach נמצא בתוך ה-{ אז הוא כן בתוך מה שה-thread יבצע.

ארכיון

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

דיונים חדשים

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.