コード例 #1
0
        private void WriteToJson(String fileName, byte mod, List <cNews> news, ref FileOperation fileOperation)
        {
            bool lockTaken = false;

            try
            {
                Monitor.TryEnter(fileOperation, -1, ref lockTaken);
                if (lockTaken)
                {
                    while (fileOperation.IsOpen())
                    {
                        Thread.Sleep(2000);
                    }
                    // fileIsOpen = true;
                    fileOperation.CreateLockMarker();

                    List <cNews> toBeWritten = UnitePosts(fileName, news);

                    Action breakThisShittyCode = () =>
                    {
                        FileStream forBreakingGoals = File.Open(fileName, FileMode.Append);
                        Thread.Sleep(3000);
                        forBreakingGoals.Close();
                    };
                    Action action;
                    switch (mod)
                    {
                    case 1:

                        //var aCText = new { id = "" , text = ""};
                        //var aCTextList = (new[] { aCText }).ToList();
                        //foreach (cNews item in news)
                        //{
                        //    aCTextList.Add(new { id = item.id, text = item.text });
                        //}
                        //aCTextList.Remove(new { id = "", text = "" });
                        //File.AppendAllText(fileName, JsonConvert.SerializeObject(aCTextList, Formatting.Indented));

                        breakThisShittyCode();
                        File.WriteAllText(fileName, JsonConvert.SerializeObject(toBeWritten.Select(cNews => new { cNews.Id, cNews.Text }), Formatting.Indented));

                        action = () => { textBox4.Text = Convert.ToString(toBeWritten.Count); progressBar1.Value += 30; };

                        if (textBox1.InvokeRequired || progressBar1.InvokeRequired)
                        {
                            Invoke(action);
                        }
                        else
                        {
                            action();
                        }

                        break;

                    case 2:
                        breakThisShittyCode();
                        File.WriteAllText(fileName, JsonConvert.SerializeObject(toBeWritten.Select(cNews => new { cNews.Id, cNews.Link }), Formatting.Indented));
                        //File.AppendAllText(fileName, new JavaScriptSerializer().Serialize(news.Select(cNews => new { cNews.id, cNews.link })));

                        action = () => { progressBar1.Value += 30; };

                        if (textBox1.InvokeRequired || progressBar1.InvokeRequired)
                        {
                            Invoke(action);
                        }
                        else
                        {
                            action();
                        }

                        break;

                    case 3:
                        breakThisShittyCode();
                        File.WriteAllText(fileName, JsonConvert.SerializeObject(toBeWritten.Select(cNews => new { cNews.Id, cNews.ImageLink }), Formatting.Indented));

                        action = () => { progressBar1.Value += 30; };

                        if (textBox1.InvokeRequired || progressBar1.InvokeRequired)
                        {
                            Invoke(action);
                        }
                        else
                        {
                            action();
                        }

                        break;

                    default:
                        break;
                    }

                    //fileIsOpen = false;

                    fileOperation.DeleteLockMarker();
                }
                else
                {
                    Action debug = () => MessageBox.Show("something went wrong");
                    if (InvokeRequired)
                    {
                        Invoke(debug);
                    }
                    else
                    {
                        debug();
                    }
                }
            }
            finally
            {
                if (lockTaken)
                {
                    Monitor.Exit(fileOperation);
                }
            }
        }
コード例 #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 0;
            List <cNews> news;

            news = new List <cNews>();
            foreach (var elem in ChDriver.FindElements(By.ClassName("wall_post_cont")))
            {
                cNews toBeAddedObject        = new cNews();
                List <IWebElement> postTexts = FindIWIWebElementByClassName(elem, "wall_post_text");
                List <IWebElement> imagLinks = FindIWIWebElementByClassName(elem, "page_post_sized_thumbs");

                toBeAddedObject.Id = elem.GetAttribute("id");

                if (postTexts.Any())
                {
                    toBeAddedObject.Text = postTexts[0].Text;

                    //foreach (IWebElement link in postTexts[0].FindElements(By.TagName("a")))
                    //{
                    //    if (string.IsNullOrEmpty(link.GetAttribute("href")))
                    //        continue;
                    //    toBeAddedObject.link.Add(link.GetAttribute("href"));//linq
                    //}
                    toBeAddedObject.Link.AddRange((from link in postTexts[0].FindElements(By.TagName("a"))
                                                   where !string.IsNullOrEmpty(link.GetAttribute("href"))
                                                   select link.GetAttribute("href")));
                }

                if (imagLinks.Any())
                {
                    foreach (IWebElement iml in imagLinks[0].FindElements(By.TagName("a")))
                    {
                        string temp = iml.GetCssValue("background-image");
                        if (!string.IsNullOrEmpty(temp) && temp.Length > 7)
                        {
                            temp = temp.Substring(5);
                            temp = temp.Remove(temp.Length - 2);
                            toBeAddedObject.ImageLink.Add(temp);
                        }
                    }
                }
                if (toBeAddedObject.ImageLink.Any() || toBeAddedObject.Link.Any() || !string.IsNullOrEmpty(toBeAddedObject.Text))
                {
                    news.Add(toBeAddedObject);
                }
            }
            progressBar1.Value = 5;

            // bool[] fileIsBusy = new bool[3] { false, false, false };
            string[] fileName = new string[3] {
                "JStext.txt", "JSlink.txt", "JSimageLink.txt"
            };
            for (int i = 0; i < 3; i++)
            {
                if (FileOperation.IsOpen(fileName[i]))
                {
                    FileOperation.DeleteLockMarker(fileName[i]);
                }
            }


            // writeToJson(fileName[0], 1, news, ref fileIsBusy[0]);
            FileOperation[] fileOperations = new FileOperation[3] {
                new FileOperation(fileName[0]),
                new FileOperation(fileName[1]),
                new FileOperation(fileName[2]),
            };


            Task[] tasks = new Task[4]
            {
                new Task(() => WriteToJson(fileName[0], 1, news, ref fileOperations[0])),
                new Task(() => WriteToJson(fileName[1], 2, news, ref fileOperations[1])),
                new Task(() => WriteToJson(fileName[2], 3, news, ref fileOperations[2])),
                new Task(() => ReadFromJson(fileName, ref fileOperations)),
            };

            //Task.Factory.StartNew(() => writeToJson("JStext.txt", 1, news,));
            //Task.Factory.StartNew(() => writeToJson("JSlink.txt", 2, news,));
            //Task.Factory.StartNew(() => writeToJson("JSimageLink.txt", 3, news,));

            for (int i = 0; i < 4; i++)
            {
                tasks[i].Start();
            }
            progressBar1.Value += 5;
        }