Проблема с парсингом через многопоток System.Threading.Tasks.Parallel.For

Dmitriy Ka

Client
Регистрация
03.05.2016
Сообщения
716
Благодарностей
461
Баллы
63
Столкнулся с проблемой, собираю ID через цикл while в многопотоке System.Threading.Tasks.Parallel.For. Есть условие если Гет ответ 200, сохраняем ID и увеличиваем ID++, если ответ не 200, просто ID++. Но из-за того что работает многопоток, ID сохраняется не верный. Подскажите есть ли какое решение, чтобы работать в многопотоке и сохранять правильные ID?

C#:
System.Threading.Tasks.Parallel.For(0, intAppIdThread, p => {   
    while(true){
        
        //GET запрос
        string strUrlAppId = "https://site.com/client_id=" + intAppIdStart;
        string strGetAppId = ZennoPoster.HTTP.Request
            (
                ZennoLab.InterfacesLibrary.Enums.Http.HttpMethod.GET, strUrlAppId,
                "", "", strIP, "UTF-8", ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.HeaderOnly, 15000, "", project.Profile.UserAgent, true, 5,
                new String[]{
                    "Referer: ",
                }, "", false, false, project.Profile.CookieContainer
            );
        if (strGetAppId.Contains("401")){
            project.SendInfoToLog("Плохой ID", boolLog);
            intAppIdStart ++;
        }
        else if (strGetAppId.Contains("200")){
            intCountStop ++;
            project.SendInfoToLog(String.Format("Рабочий ID: " + intAppIdStart.ToString()), true);
            project.SendInfoToLog(String.Format("Собрано ID:{0} из {1}",intCountStop.ToString(), intAppIdCount.ToString()), true);
            File.AppendAllText(strAppIdSave,"\r\n" + intAppIdStart.ToString());
            // Удалить пустые строки в файле
            var lines = System.IO.File.ReadAllLines(strAppIdSave).Where(arg => !string.IsNullOrWhiteSpace(arg));
            System.IO.File.WriteAllLines(strAppIdSave, lines);
            intAppIdStart ++;
            
        }
        else{
            project.SendInfoToLog("Неизвестный ответ", boolLog);
            intAppIdStart ++;
        }

        if (intCountStop >= intAppIdCount) break;
        if (intAppIdStart > 7000000) break;

    }
});
 
Последнее редактирование:

Phoenix78

Client
Read only
Регистрация
06.11.2018
Сообщения
11 790
Благодарностей
5 710
Баллы
113

Dmitriy Ka

Client
Регистрация
03.05.2016
Сообщения
716
Благодарностей
461
Баллы
63

Dmitriy Ka

Client
Регистрация
03.05.2016
Сообщения
716
Благодарностей
461
Баллы
63
Разобрался сам, оказалось это легко делается добавлением одной переменной)) и работай с ней при ответе 200

C#:
        string strUrlAppId = "https://site.com/client_id=" + intAppIdStart;
        string _AppId = intAppIdStart.ToString();// Решение проблемы :)
        string strGetAppId = ZennoPoster.HTTP.Request
            (
                ZennoLab.InterfacesLibrary.Enums.Http.HttpMethod.GET, strUrlAppId,
                "", "", strIP, "UTF-8", ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.HeaderOnly, 15000, "", project.Profile.UserAgent, true, 5,
                new String[]{
                    "Referer: ",
                }, "", false, false, project.Profile.CookieContainer
            );
 

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)