SyncObjects.InputSyncer - - - - - - SyncObjects.ListSyncer

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 812
Благодарностей
1 187
Баллы
113
Please explain or guide where I can find all necessary information about those methods.

Or better tell when I should use them ?

Every time I'm doing something with tables or lists i should use this method ?

And what about SyncObjects.InputSyncer for what is this method ?
Thanks
 
  • Спасибо
Реакции: Pierre Paul Jacques

rostonix

Известная личность
Регистрация
23.12.2011
Сообщения
29 067
Благодарностей
5 715
Баллы
113

455919946

Client
Регистрация
31.12.2013
Сообщения
104
Благодарностей
4
Баллы
18
  • Спасибо
Реакции: rostonix

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 812
Благодарностей
1 187
Баллы
113
It is for System.Windows.Forms. If you use multithreading you should lock input fields otherwise it will not work correctly.

You need to use it in case you work with list or table with multithreading.
I'm back and with real example:



Like there in example everything is okay how this SyncObjects.ListSyncer should be used ?
That mean only one thread will have access to do statements ?
Like example I run this template with 10 threads and only one thread will will be able to write data in files because it will delete also data from file and when second thread will come in it will just finish with error that in list there is no data.

Am I understood correct this ?

Thanks
 

darkdiver

Administrator
Команда форума
Регистрация
13.01.2009
Сообщения
2 285
Благодарностей
2 730
Баллы
113
It is not possible to read code from the screenshot. Please attach the screenshot directly to the thread (just Ctrl+V with screenshot in the clipboard) .
Or post the code here.
 

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 812
Благодарностей
1 187
Баллы
113
It is not possible to read code from the screenshot. Please attach the screenshot directly to the thread (just Ctrl+V with screenshot in the clipboard) .
Or post the code here.
Hmm i have attached screenshot directly in thread and not with any link, not sure why you are not seeing it, It shows up for me just fine...
Anyway there is code:

C#:
string dataPath = project.Directory + @"\Data\data.csv";
string imagePath = project.Directory + @"\Data\images.csv";
var dataList = project.Lists["Data"];
var imageList = project.Lists["Image"];

lock(SyncObjects.ListSyncer)
{
    do
    {
        string dataString = dataList[0];
        using(var sw = new System.IO.StreamWriter(dataPath, true, Encoding.UTF8))
            {
                sw.WriteLine(dataString);
                dataList.RemoveAt(0);
            }
    } while (dataList.Count != 0);
 
    do
    {
        string imageString = imageList[0];
        using(var sw = new System.IO.StreamWriter(imagePath, true, Encoding.UTF8))
            {
                sw.WriteLine(imageString);
                imageList.RemoveAt(0);
            }
    } while (imageList.Count != 0);
}
// Upload data
ZennoPoster.FtpUploadFile("host", 21, "FTP", "login", "pass", "", path, dataPath);
// Upload images
ZennoPoster.FtpUploadFile("host", 21, "FTP", "login", "pass", "", path, imagePath);
My question point is: I have template that runs in multithread and after list is empty it goes to this c# snippet, as you see i want to clean files and upload data to FTP, but i do not want that this happens in multithread...
 

darkdiver

Administrator
Команда форума
Регистрация
13.01.2009
Сообщения
2 285
Благодарностей
2 730
Баллы
113
C#:
string dataPath = project.Directory + @"\Data\data.csv";
string imagePath = project.Directory + @"\Data\images.csv";
var dataList = project.Lists["Data"];
var imageList = project.Lists["Image"];

lock(SyncObjects.ListSyncer)
{
    // comment the next line if you do not need to upload once
    // if other threads were waiting on lock they will try to create files and upload them but you do not need this.
    if (dataList.Count==0 && imageList.Count==0) return "lists are empty";

    do
    {
        string dataString = dataList[0];
        using(var sw = new System.IO.StreamWriter(dataPath, true, Encoding.UTF8))
            {
                sw.WriteLine(dataString);
                dataList.RemoveAt(0);
            }
    } while (dataList.Count != 0);

    do
    {
        string imageString = imageList[0];
        using(var sw = new System.IO.StreamWriter(imagePath, true, Encoding.UTF8))
            {
                sw.WriteLine(imageString);
                imageList.RemoveAt(0);
            }
    } while (imageList.Count != 0);

    // moving upload under lock will guaranteed you that upload also will be single threaded. 
    // Upload data
    ZennoPoster.FtpUploadFile("host", 21, "FTP", "login", "pass", "", path, dataPath);
    // Upload images
    ZennoPoster.FtpUploadFile("host", 21, "FTP", "login", "pass", "", path, imagePath);
}
 
  • Спасибо
Реакции: lokiys

darkdiver

Administrator
Команда форума
Регистрация
13.01.2009
Сообщения
2 285
Благодарностей
2 730
Баллы
113
Also instead of this:
C#:
string dataPath = project.Directory + @"\Data\data.csv";
var dataList = project.Lists["Data"];

lock(SyncObjects.ListSyncer)
{
    do
    {
        string dataString = dataList[0];
        using(var sw = new System.IO.StreamWriter(dataPath, true, Encoding.UTF8))
            {
                sw.WriteLine(dataString);
                dataList.RemoveAt(0);
            }
    } while (dataList.Count != 0);
}

Use this (this is MUCH more faster variant if you have more than 2 lines in the list):
C#:
string dataPath = project.Directory + @"\Data\data.csv";
var dataList = project.Lists["Data"];

lock(SyncObjects.ListSyncer)
{
   var content = string.Join(Environment.NewLine, dataList);
   using(var sw = new System.IO.StreamWriter(dataPath, true, Encoding.UTF8))
       sw.Write(content);
   dataList.Clear();
}
 
  • Спасибо
Реакции: lokiys

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