0 / 0 / 1
Регистрация: 27.05.2012
Сообщений: 39
1

сначала запускаю с низким, а затем с высоким. В итоге пока поток с низким приоритетом не завершит работу, приоритет с высоким даже не стартует.

27.02.2013, 21:45. Показов 695. Ответов 2
Метки нет (Все метки)

Студворк — интернет-сервис помощи студентам
Есть две кнопки на форме, каждая из которых запускает свой поток. Для первой ставлю приоритет самый высокий, для второй самый низкий. Затем сначала запускаю с низким, а затем с высоким. В итоге пока поток с низким приоритетом не завершит работу, приоритет с высоким даже не стартует.

Код, если нужно вдруг

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Threading;
namespace FileSearch
{
 
    public partial class SearchForm : Form
    {
 
        static EventWaitHandle wh;
        static bool Stop, Pause;
        int ThreadCount = 2;
        static DirectoryInfo[] Path;
        static List<FileInfo>[] Resultat;
        static string[] SearchStrings;
        static string[] pattern;
 
        public SearchForm()
        {
            InitializeComponent();
            Resultat = new List<FileInfo>[ThreadCount];
            Path = new DirectoryInfo[ThreadCount];
            pattern = new string[ThreadCount];
            SearchStrings = new string[ThreadCount];
 
            {
                for (int i = 0; i < ThreadCount; i++)
                {
                    Resultat[i] = new List<FileInfo>();
                }
            }
            wh = new AutoResetEvent(false);
 
        }
 
        private void GetDirectoryButton1_Click(object sender, EventArgs e)
        {
            GetFile(0);
        }
        private void GetFile(int i)
        {
            using (FolderBrowserDialog GetFileDialog = new FolderBrowserDialog())
            {
                if (GetFileDialog.ShowDialog() == DialogResult.OK)
                {
                    Path[i] = new DirectoryInfo(GetFileDialog.SelectedPath);
                }
            }
        }
 
        private void Search(object data)
        {
            if (data is ListBox)
            {
                ListBox l = (ListBox)data;
                FileInfo[] allFiles = null;
                try
                {
                    allFiles = Path[0].GetFiles(pattern[0], SearchOption.TopDirectoryOnly);
                }
                catch (Exception e)
                {
                    //    MessageBox.Show(e.Message, "Ошибочка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (allFiles != null)
                    foreach (FileInfo f in allFiles)
                    {
                        if (SearchStrings[0] != null && f.Extension == ".txt" && TextSearch(0, f) == true)
                            lock (Resultat)
                            {
                                Resultat[0].Add(f);
                            }
                        else if (SearchStrings[0] == null)
                            lock (Resultat)
                            {
                                Resultat[0].Add(f);
                            }
                    }
                DirectoryInfo[] allDirictories = null;
                try
                {
                    allDirictories = Path[0].GetDirectories("*.*", SearchOption.TopDirectoryOnly);
                }
                catch (Exception e)
                {
                    //  MessageBox.Show(e.Message, "Ошибочка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (allDirictories != null)
                    foreach (DirectoryInfo d in allDirictories)
                    {
                        HelpSearch(0, d);
                    }
 
 
                if (Resultat[0].Count > 0)
                    lock (Resultat)
                        foreach (FileInfo f in Resultat[0])
                            addItem(f.FullName);
 
            }
            Button1Ready();
        }
 
        private void Search2(object data)
        {
            if (data is ListBox)
            {
                ListBox l = (ListBox)data;
                FileInfo[] allFiles = null;
                try
                {
                    allFiles = Path[1].GetFiles(pattern[1], SearchOption.TopDirectoryOnly);
                }
                catch (Exception e)
                {
                    //    MessageBox.Show(e.Message, "Ошибочка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (allFiles != null)
                    foreach (FileInfo f in allFiles)
                    {
                        if (SearchStrings[1] != null && f.Extension == ".txt" && TextSearch(1, f) == true)
                            lock (Resultat)
                            {
                                Resultat[1].Add(f);
                            }
                        else if (SearchStrings[1] == null)
                            lock (Resultat)
                            {
                                Resultat[1].Add(f);
                            }
                    }
                DirectoryInfo[] allDirictories = null;
                try
                {
                    allDirictories = Path[1].GetDirectories("*.*", SearchOption.TopDirectoryOnly);
                }
                catch (Exception e)
                {
                    //  MessageBox.Show(e.Message, "Ошибочка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (allDirictories != null)
                    foreach (DirectoryInfo d in allDirictories)
                    {
                        HelpSearch(1, d);
                    }
 
 
                if (Resultat[1].Count > 0)
                    lock (Resultat)
                        foreach (FileInfo f in Resultat[1])
                            addItem2(f.FullName);
 
            }
            Button2Ready();
        }
 
        private static void HelpSearch(int i, DirectoryInfo dir)
        {
 
            FileInfo[] allFiles = null;
            try
            {
                allFiles = dir.GetFiles(pattern[i], SearchOption.TopDirectoryOnly);
            }
            catch (Exception e)
            {
                //   MessageBox.Show(e.Message, "Ошибочка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (allFiles != null)
                foreach (FileInfo f in allFiles)
                {
                    if (SearchStrings[i] != null && f.Extension == ".txt" && TextSearch(i, f) == true)
                        lock (Resultat)
                            Resultat[i].Add(f);
                    else if (SearchStrings[i] == null)
                        lock (Resultat)
                            Resultat[i].Add(f);
                }
            DirectoryInfo[] allDirictories = null;
            try
            {
                allDirictories = dir.GetDirectories("*.*", SearchOption.TopDirectoryOnly);
            }
            catch (Exception e)
            {
                // MessageBox.Show(e.Message, "Ошибочка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (allDirictories != null)
                foreach (DirectoryInfo d in allDirictories)
                    HelpSearch(i, d);
        }
 
        private static bool TextSearch(int i, FileInfo f)
        {
            bool ok;
            using (StreamReader sr = new StreamReader(f.FullName, System.Text.Encoding.GetEncoding(1251)))
            {
                string input = null;
                input = sr.ReadToEnd();
                if (input.Contains(SearchStrings[i]))
                    ok = true;
                else
                    ok = false;
            }
            return ok;
        }
 
        Thread thread1, thread2;
 
        private void StartButton1_Click(object sender, EventArgs e)
        {
            Resultat[0].Clear();
            ResultlistBox1.Items.Clear();
            if (SearchStringtextBox1.Text.Length > 0)
                SearchStrings[0] = SearchStringtextBox1.Text;
            else
                SearchStrings[0] = null;
            if (TemplateTextBox1.Text.Length > 0)
            {
                StartButton1.Enabled = false;
                thread1 = new Thread(Search);
                pattern[0] = TemplateTextBox1.Text;
                switch (PriorityComboBox1.Text)
                {
                    case "Lowest": thread1.Priority = ThreadPriority.Lowest;
                        break;
                    case "BelowNormal": thread1.Priority = ThreadPriority.BelowNormal;
                        break;
                    case "Normal": thread1.Priority = ThreadPriority.Normal;
                        break;
                    case "AboveNormal": thread1.Priority = ThreadPriority.AboveNormal;
                        break;
                    case "Highest": thread1.Priority = ThreadPriority.Highest;
                        break;
                }
                if (Path[0] != null)                
                    thread1.Start(ResultlistBox1);
                else
                    MessageBox.Show("Не выбран каталог", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
 
            else
                MessageBox.Show("Не выбран шаблон", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
 
        private void StartButton2_Click(object sender, EventArgs e)
        {
            Resultat[1].Clear();
            ResultlistBox2.Items.Clear();
            if (SearchStringtextBox2.Text.Length > 0)
                SearchStrings[1] = SearchStringtextBox2.Text;
            else
                SearchStrings[1] = null;
            if (TemplateTextBox2.Text.Length > 0)
            {
                StartButton2.Enabled = false;
                thread2 = new Thread(Search2);
                pattern[1] = TemplateTextBox2.Text;
                if (Path[1] != null)
                {
                    switch (PriorityComboBox2.Text)
                    {
                        case "Lowest": thread2.Priority = ThreadPriority.Lowest;
                            break;
                        case "BelowNormal": thread2.Priority = ThreadPriority.BelowNormal;
                            break;
                        case "Normal": thread2.Priority = ThreadPriority.Normal;
                            break;
                        case "AboveNormal": thread2.Priority = ThreadPriority.AboveNormal;
                            break;
                        case "Highest": thread2.Priority = ThreadPriority.Highest;
                            break;
                    }
                    thread2.Start(ResultlistBox2);
                }
                else
                    MessageBox.Show("Не выбран каталог", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
 
            else
                MessageBox.Show("Не выбран шаблон", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
 
        #region
        private delegate void SetListBoxItem(string name);//Создаём объект типа делигат
        private delegate void SetButtonReady();
 
        private void LoadItemsListCallBack(string item) //Создаём метод который будет вызываться делигатом
        {
            ResultlistBox1.Items.Add(item); // добовляем строки в ListLog
        }
        private void LoadItemsListCallBack2(string item) //Создаём метод который будет вызываться делигатом
        {
            ResultlistBox2.Items.Add(item); // добовляем строки в ListLog
        }
        private void Button1IsReady()
        {
            StartButton1.Enabled = true;
        }
        private void Button2IsReady()
        {
            StartButton2.Enabled = true;
        }
 
 
        public void addItem(string item)
        {
            if (ResultlistBox1.InvokeRequired)
            {
                SetListBoxItem lstBoxItem = new SetListBoxItem(this.LoadItemsListCallBack);
                this.Invoke(lstBoxItem, new object[] { item });
            }
            else
            {
                ResultlistBox1.Items.Add(item);
            }
        }
        public void addItem2(string item)
        {
            if (ResultlistBox2.InvokeRequired)
            {
                SetListBoxItem lstBoxItem = new SetListBoxItem(this.LoadItemsListCallBack2);
                this.Invoke(lstBoxItem, new object[] { item });
            }
            else
            {
                ResultlistBox2.Items.Add(item);
            }
        }
        public void Button1Ready()
        {
            if (StartButton1.InvokeRequired)
            {
                SetButtonReady Button = new SetButtonReady(this.Button1IsReady);
                this.Invoke(Button, null);
            }
            else
            {
                StartButton1.Enabled = true;
            }
        }
        public void Button2Ready()
        {
            if (StartButton1.InvokeRequired)
            {
                SetButtonReady Button = new SetButtonReady(this.Button2IsReady);
                this.Invoke(Button, null);
            }
            else
            {
                StartButton2.Enabled = true; ;
            }
        }
        #endregion
 
        private void StopButton1_Click(object sender, EventArgs e)
        {
 
        }
 
        private void WaitButton1_Click(object sender, EventArgs e)
        {
 
        }
 
        private void GetDirectoryButton2_Click(object sender, EventArgs e)
        {
            GetFile(1);
        }
 
        private void PriorityComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
 
        }
    }
}
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
27.02.2013, 21:45
Ответы с готовыми решениями:

Найти разницу в росте между высоким и низким футболистом команды
помогите пожалуйста решить задачу:cry: Найти разницу в росте между высоким и низким футболистом...

Вычислить средний рост студентов и разность в росте между самым высоким и самым низким студентом
Одномерный массив вещественных чисел R75 содержит данные о росте группы студентов. Массив заполнить...

Определить разницу между самым высоким и самым низким учеником в класе . Организовать ввод значений с клавиатуры.
Можно пожалуйста полный алгоритм.

Timer с высоким приоритетом
Хочу отображать ход выполнения моей процедуры(online :)). Для этого создал Таймер и при клике по...

2
Заблокирован
28.02.2013, 01:29 2
код дизайнера скинь. влом набирать.
0
0 / 0 / 1
Регистрация: 27.05.2012
Сообщений: 39
28.02.2013, 22:54  [ТС] 3
Спасибо,уже сделал, просто локов переставил лишних)
но есть пара новых вопросов:
1. При приостановке одного потока и запуска в этот момент другого, программа зависает
2. Когда ищут оба потока в текстовых файлах, вылетает программа с ошибкой, а как сделать асинхронность я не знаю( beginread пробовал, но не получилось)
вот сам код:
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Text;
namespace FileSearch
{
 
    public partial class SearchForm : Form
    {
 
        static ManualResetEvent mh;        
        int ThreadCount = 2;
        static DirectoryInfo[] Path;
        static List<FileInfo>[] Resultat;
        static string[] SearchStrings;
        static string[] pattern;
 
        public SearchForm()
        {
            InitializeComponent();
            Resultat = new List<FileInfo>[ThreadCount];
            Path = new DirectoryInfo[ThreadCount];
            pattern = new string[ThreadCount];
            SearchStrings = new string[ThreadCount];          
            Thread.CurrentThread.Priority = ThreadPriority.Highest;
            for (int i = 0; i < ThreadCount; i++)
            {
                Resultat[i] = new List<FileInfo>();
            }
 
            mh = new ManualResetEvent(true);
 
        }
 
        private void GetDirectoryButton1_Click(object sender, EventArgs e)
        {
            GetFile(0);
        }
        private void GetDirectoryButton2_Click(object sender, EventArgs e)
        {
            GetFile(1);
        }
        private void GetFile(int i)
        {
            using (FolderBrowserDialog GetFileDialog = new FolderBrowserDialog())
            {
                if (GetFileDialog.ShowDialog() == DialogResult.OK)
                {
                    Path[i] = new DirectoryInfo(GetFileDialog.SelectedPath);
                }
            }
        }
 
        private void Search(object data)
        {
            if (data is ListBox)
            {
                ListBox l = (ListBox)data;
                FileInfo[] allFiles = null;
                try
                {
                    allFiles = Path[0].GetFiles(pattern[0], SearchOption.TopDirectoryOnly);
                }
                catch (Exception e)
                {
                    //    MessageBox.Show(e.Message, "Ошибочка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (allFiles != null)
                    foreach (FileInfo f in allFiles)
                    {
                        if (SearchStrings[0] != null && f.Extension == ".txt" && TextSearch(0, f) == true)
                            lock (Resultat)
                            {
                                Resultat[0].Add(f);
                            }
                        else if (SearchStrings[0] == null)
                            lock (Resultat)
                            {
                                Resultat[0].Add(f);
                            }
                    }
                DirectoryInfo[] allDirictories = null;
                try
                {
                    allDirictories = Path[0].GetDirectories("*.*", SearchOption.TopDirectoryOnly);
                }
                catch (Exception e)
                {
                    //  MessageBox.Show(e.Message, "Ошибочка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (allDirictories != null)
                    foreach (DirectoryInfo d in allDirictories)
                    {
                        HelpSearch(0, d);
                    }
 
 
                if (Resultat[0].Count > 0)
                    lock (Resultat)
                        foreach (FileInfo f in Resultat[0])
                            addItem(f.FullName);
 
            }
            Button1Ready();
        }
 
        private void Search2(object data)
        {
            if (data is ListBox)
            {
                ListBox l = (ListBox)data;
                FileInfo[] allFiles = null;
                try
                {
                    allFiles = Path[1].GetFiles(pattern[1], SearchOption.TopDirectoryOnly);
                }
                catch (Exception e)
                {
                    //    MessageBox.Show(e.Message, "Ошибочка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (allFiles != null)
                    foreach (FileInfo f in allFiles)
                    {
                        if (SearchStrings[1] != null && f.Extension == ".txt" && TextSearch(1, f) == true)
                           // lock (Resultat)
                            {
                                Resultat[1].Add(f);
                            }
                        else if (SearchStrings[1] == null)
                          //  lock (Resultat)
                            {
                                Resultat[1].Add(f);
                            }
                    }
                DirectoryInfo[] allDirictories = null;
                try
                {
                    allDirictories = Path[1].GetDirectories("*.*", SearchOption.TopDirectoryOnly);
                }
                catch (Exception e)
                {
                    //  MessageBox.Show(e.Message, "Ошибочка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (allDirictories != null)
                    foreach (DirectoryInfo d in allDirictories)
                    {
                        HelpSearch(1, d);
                    }
 
 
                if (Resultat[1].Count > 0)
                    //lock (Resultat)
                        foreach (FileInfo f in Resultat[1])
                            addItem2(f.FullName);
 
            }
            Button2Ready();
        }
 
        private static void HelpSearch(int i, DirectoryInfo dir)
        {            
            FileInfo[] allFiles = null;
            try
            {
                allFiles = dir.GetFiles(pattern[i], SearchOption.TopDirectoryOnly);
            }
            catch (Exception e)
            {
                //   MessageBox.Show(e.Message, "Ошибочка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (allFiles != null)
                foreach (FileInfo f in allFiles)
                {
                    if (SearchStrings[i] != null && f.Extension == ".txt" && TextSearch(i, f) == true)
                       // lock (Resultat)
                            Resultat[i].Add(f);
                    else if (SearchStrings[i] == null)
                       // lock (Resultat)
                            Resultat[i].Add(f);
                }
            DirectoryInfo[] allDirictories = null;
            try
            {
                allDirictories = dir.GetDirectories("*.*", SearchOption.TopDirectoryOnly);
            }
            catch (Exception e)
            {
                // MessageBox.Show(e.Message, "Ошибочка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (allDirictories != null)
                foreach (DirectoryInfo d in allDirictories)
                    HelpSearch(i, d);
        }
        private static bool TextSearch(int i, FileInfo f)
        {
            bool ok;
            using (StreamReader sr = new StreamReader(f.FullName, System.Text.Encoding.GetEncoding(1251)))
            {
                string input = null;
                input = sr.ReadToEnd();
                if (input.Contains(SearchStrings[i]))
                    ok = true;
                else
                    ok = false;
            }
            return ok;
        }        
        
 
        Thread thread1, thread2;
 
        private void StartButton1_Click(object sender, EventArgs e)
        {
            Resultat[0].Clear();
            ResultlistBox1.Items.Clear();
            if (SearchStringtextBox1.Text.Length > 0)
                SearchStrings[0] = SearchStringtextBox1.Text;
            else
                SearchStrings[0] = null;
            if (TemplateTextBox1.Text.Length > 0)
            {
                
                thread1 = new Thread(Search);
                pattern[0] = TemplateTextBox1.Text;
                
                if (Path[0] != null)
                {
                    switch (PriorityComboBox1.Text)
                    {
 
                        case "Lowest": thread1.Priority = ThreadPriority.Lowest;
                            break;
                        case "BelowNormal": thread1.Priority = ThreadPriority.BelowNormal;
                            break;
                        case "AboveNormal": thread1.Priority = ThreadPriority.AboveNormal;
                            break;
                        case "Highest": thread1.Priority = ThreadPriority.Highest;
                            break;
                        default: thread1.Priority = ThreadPriority.Normal;
                            break;
                    }                   
                    thread1.Start(ResultlistBox1);          
                    StartButton1.Enabled = false;
                }
                else
                    MessageBox.Show("Не выбран каталог", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
 
            else
                MessageBox.Show("Не выбран шаблон", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
 
        private void StartButton2_Click(object sender, EventArgs e)
        {
            Resultat[1].Clear();
            ResultlistBox2.Items.Clear();
            if (SearchStringtextBox2.Text.Length > 0)
                SearchStrings[1] = SearchStringtextBox2.Text;
            else
                SearchStrings[1] = null;
            if (TemplateTextBox2.Text.Length > 0)
            {                
                thread2 = new Thread(Search2);
                pattern[1] = TemplateTextBox2.Text;
                if (Path[1] != null)
                {
                    StartButton2.Enabled = false;
                    switch (PriorityComboBox2.Text)
                    {
                        case "Lowest": thread2.Priority = ThreadPriority.Lowest;
                            break;
                        case "BelowNormal": thread2.Priority = ThreadPriority.BelowNormal;
                            break;                        
                        case "AboveNormal": thread2.Priority = ThreadPriority.AboveNormal;
                            break;
                        case "Highest": thread2.Priority = ThreadPriority.Highest;
                            break;
                        default: thread2.Priority = ThreadPriority.Normal;
                            break;
                    }
                    thread2.Start(ResultlistBox2);
                }
                else
                    MessageBox.Show("Не выбран каталог", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
 
            else
                MessageBox.Show("Не выбран шаблон", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
 
        #region
        private delegate void SetListBoxItem(string name);
        private delegate void SetButtonReady();
 
        private void LoadItemsListCallBack(string item)
        {    
                ResultlistBox1.Items.Add(item);
        }
        private void LoadItemsListCallBack2(string item) 
        {
            ResultlistBox2.Items.Add(item); 
        }
        private void Button1IsReady()
        {
            StartButton1.Enabled = true;
        }
        private void Button2IsReady()
        {
            StartButton2.Enabled = true;
        }
 
 
        public void addItem(string item)
        {
            if (ResultlistBox1.InvokeRequired)
            {
                SetListBoxItem lstBoxItem = new SetListBoxItem(this.LoadItemsListCallBack);
                this.Invoke(lstBoxItem, new object[] { item });
            }
            else
            {
                ResultlistBox1.Items.Add(item);
            }
        }
        public void addItem2(string item)
        {
            if (ResultlistBox2.InvokeRequired)
            {
                SetListBoxItem lstBoxItem = new SetListBoxItem(this.LoadItemsListCallBack2);
                this.Invoke(lstBoxItem, new object[] { item });
            }
            else
            {
                ResultlistBox2.Items.Add(item);
            }
        }
        public void Button1Ready()
        {
            if (StartButton1.InvokeRequired)
            {
                SetButtonReady Button = new SetButtonReady(this.Button1IsReady);
                this.Invoke(Button, null);
            }
            else
            {
                StartButton1.Enabled = true;
            }
        }
        public void Button2Ready()
        {
            if (StartButton1.InvokeRequired)
            {
                SetButtonReady Button = new SetButtonReady(this.Button2IsReady);
                this.Invoke(Button, null);
            }
            else
            {
                StartButton2.Enabled = true; ;
            }
        }
        #endregion
 
        private void StopButton1_Click(object sender, EventArgs e)
        {
            if ((thread1 != null))
                if ((thread1.ThreadState != ThreadState.Suspended))
                {
                    thread1.Abort();
                    thread1 = null;
                    StartButton1.Enabled = true;
                }
        }
 
        private void WaitButton1_Click(object sender, EventArgs e)
        {
            if (thread1 != null)
                if (WaitButton1.Text == "Приостановить")
                {
                    if ((thread1.ThreadState != ThreadState.Stopped))
                    {
                        thread1.Suspend();
                        WaitButton1.Text = "Продолжить";
                    }
                }
                else
                {
                    if ((thread1.ThreadState != ThreadState.Stopped))
                    {
                        thread1.Resume();
                        WaitButton1.Text = "Приостановить";
                    }
                }
        }
 
        private void StopButton2_Click(object sender, EventArgs e)
        {
            if ((thread2 != null))
                if ((thread1.ThreadState != ThreadState.Suspended))
                {
                    thread2.Abort();
                    thread2 = null;
                    StartButton2.Enabled = true;
                }
        }
 
        private void WaitButton2_Click(object sender, EventArgs e)
        {
            if (thread2 != null)
                if (WaitButton2.Text == "Приостановить")
                {
                    if ((thread2.ThreadState != ThreadState.Stopped))
                    {
                        thread2.Suspend();
                        WaitButton2.Text = "Продолжить";
                    }
                }
                else
                {
                    if ((thread2.ThreadState != ThreadState.Stopped))
                    {
                        thread2.Resume();
                        WaitButton2.Text = "Приостановить";
                    }
                }
        }
 
        private void SearchForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (thread1 != null)
                thread1.Abort();
            if (thread2 != null)
                thread2.Abort();
        }       
    }
}

вот код дизайнера:
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
namespace FileSearch
{
    partial class SearchForm
    {
        /// <summary>
        /// Требуется переменная конструктора.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Освободить все используемые ресурсы.
        /// </summary>
        /// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Код, автоматически созданный конструктором форм Windows
 
        /// <summary>
        /// Обязательный метод для поддержки конструктора - не изменяйте
        /// содержимое данного метода при помощи редактора кода.
        /// </summary>
        private void InitializeComponent()
        {
            this.ResultlistBox2 = new System.Windows.Forms.ListBox();
            this.SearchStringtextBox2 = new System.Windows.Forms.TextBox();
            this.SearchStringLabel2 = new System.Windows.Forms.Label();
            this.TemplateTextBox2 = new System.Windows.Forms.TextBox();
            this.Templatelabel2 = new System.Windows.Forms.Label();
            this.GetDirectoryButton2 = new System.Windows.Forms.Button();
            this.DirectorySearchLabel2 = new System.Windows.Forms.Label();
            this.ResultlistBox1 = new System.Windows.Forms.ListBox();
            this.SearchStringtextBox1 = new System.Windows.Forms.TextBox();
            this.TemplateTextBox1 = new System.Windows.Forms.TextBox();
            this.Templatelabel1 = new System.Windows.Forms.Label();
            this.SearchStringLabel1 = new System.Windows.Forms.Label();
            this.GetDirectoryButton1 = new System.Windows.Forms.Button();
            this.DirectorySearchLabel1 = new System.Windows.Forms.Label();
            this.StartButton1 = new System.Windows.Forms.Button();
            this.StartButton2 = new System.Windows.Forms.Button();
            this.StopButton1 = new System.Windows.Forms.Button();
            this.WaitButton1 = new System.Windows.Forms.Button();
            this.StopButton2 = new System.Windows.Forms.Button();
            this.WaitButton2 = new System.Windows.Forms.Button();
            this.PriorityLabel1 = new System.Windows.Forms.Label();
            this.PriorityComboBox1 = new System.Windows.Forms.ComboBox();
            this.PriorityComboBox2 = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // ResultlistBox2
            // 
            this.ResultlistBox2.FormattingEnabled = true;
            this.ResultlistBox2.HorizontalScrollbar = true;
            this.ResultlistBox2.Location = new System.Drawing.Point(638, 195);
            this.ResultlistBox2.Name = "ResultlistBox2";
            this.ResultlistBox2.Size = new System.Drawing.Size(500, 420);
            this.ResultlistBox2.TabIndex = 27;
            // 
            // SearchStringtextBox2
            // 
            this.SearchStringtextBox2.Location = new System.Drawing.Point(729, 109);
            this.SearchStringtextBox2.Name = "SearchStringtextBox2";
            this.SearchStringtextBox2.Size = new System.Drawing.Size(152, 20);
            this.SearchStringtextBox2.TabIndex = 26;
            // 
            // SearchStringLabel2
            // 
            this.SearchStringLabel2.AutoSize = true;
            this.SearchStringLabel2.Location = new System.Drawing.Point(632, 112);
            this.SearchStringLabel2.Name = "SearchStringLabel2";
            this.SearchStringLabel2.Size = new System.Drawing.Size(84, 13);
            this.SearchStringLabel2.TabIndex = 25;
            this.SearchStringLabel2.Text = "Искать строку:";
            // 
            // TemplateTextBox2
            // 
            this.TemplateTextBox2.Location = new System.Drawing.Point(729, 65);
            this.TemplateTextBox2.Name = "TemplateTextBox2";
            this.TemplateTextBox2.Size = new System.Drawing.Size(152, 20);
            this.TemplateTextBox2.TabIndex = 24;
            // 
            // Templatelabel2
            // 
            this.Templatelabel2.AutoSize = true;
            this.Templatelabel2.Location = new System.Drawing.Point(635, 68);
            this.Templatelabel2.Name = "Templatelabel2";
            this.Templatelabel2.Size = new System.Drawing.Size(49, 13);
            this.Templatelabel2.TabIndex = 23;
            this.Templatelabel2.Text = "Шаблон:";
            // 
            // GetDirectoryButton2
            // 
            this.GetDirectoryButton2.Location = new System.Drawing.Point(756, 23);
            this.GetDirectoryButton2.Name = "GetDirectoryButton2";
            this.GetDirectoryButton2.Size = new System.Drawing.Size(75, 23);
            this.GetDirectoryButton2.TabIndex = 22;
            this.GetDirectoryButton2.Text = "Обзор..";
            this.GetDirectoryButton2.UseVisualStyleBackColor = true;
            this.GetDirectoryButton2.Click += new System.EventHandler(this.GetDirectoryButton2_Click);
            // 
            // DirectorySearchLabel2
            // 
            this.DirectorySearchLabel2.AutoSize = true;
            this.DirectorySearchLabel2.Location = new System.Drawing.Point(632, 28);
            this.DirectorySearchLabel2.Name = "DirectorySearchLabel2";
            this.DirectorySearchLabel2.Size = new System.Drawing.Size(118, 13);
            this.DirectorySearchLabel2.TabIndex = 21;
            this.DirectorySearchLabel2.Text = "Искать в директории:";
            // 
            // ResultlistBox1
            // 
            this.ResultlistBox1.FormattingEnabled = true;
            this.ResultlistBox1.HorizontalScrollbar = true;
            this.ResultlistBox1.Location = new System.Drawing.Point(26, 195);
            this.ResultlistBox1.Name = "ResultlistBox1";
            this.ResultlistBox1.Size = new System.Drawing.Size(500, 420);
            this.ResultlistBox1.TabIndex = 20;
            // 
            // SearchStringtextBox1
            // 
            this.SearchStringtextBox1.Location = new System.Drawing.Point(113, 118);
            this.SearchStringtextBox1.Name = "SearchStringtextBox1";
            this.SearchStringtextBox1.Size = new System.Drawing.Size(152, 20);
            this.SearchStringtextBox1.TabIndex = 19;
            // 
            // TemplateTextBox1
            // 
            this.TemplateTextBox1.Location = new System.Drawing.Point(113, 70);
            this.TemplateTextBox1.Name = "TemplateTextBox1";
            this.TemplateTextBox1.Size = new System.Drawing.Size(152, 20);
            this.TemplateTextBox1.TabIndex = 18;
            // 
            // Templatelabel1
            // 
            this.Templatelabel1.AutoSize = true;
            this.Templatelabel1.Location = new System.Drawing.Point(23, 73);
            this.Templatelabel1.Name = "Templatelabel1";
            this.Templatelabel1.Size = new System.Drawing.Size(49, 13);
            this.Templatelabel1.TabIndex = 17;
            this.Templatelabel1.Text = "Шаблон:";
            // 
            // SearchStringLabel1
            // 
            this.SearchStringLabel1.AutoSize = true;
            this.SearchStringLabel1.Location = new System.Drawing.Point(23, 121);
            this.SearchStringLabel1.Name = "SearchStringLabel1";
            this.SearchStringLabel1.Size = new System.Drawing.Size(84, 13);
            this.SearchStringLabel1.TabIndex = 16;
            this.SearchStringLabel1.Text = "Искать строку:";
            // 
            // GetDirectoryButton1
            // 
            this.GetDirectoryButton1.Location = new System.Drawing.Point(147, 23);
            this.GetDirectoryButton1.Name = "GetDirectoryButton1";
            this.GetDirectoryButton1.Size = new System.Drawing.Size(75, 23);
            this.GetDirectoryButton1.TabIndex = 15;
            this.GetDirectoryButton1.Text = "Обзор..";
            this.GetDirectoryButton1.UseVisualStyleBackColor = true;
            this.GetDirectoryButton1.Click += new System.EventHandler(this.GetDirectoryButton1_Click);
            // 
            // DirectorySearchLabel1
            // 
            this.DirectorySearchLabel1.AutoSize = true;
            this.DirectorySearchLabel1.Location = new System.Drawing.Point(23, 28);
            this.DirectorySearchLabel1.Name = "DirectorySearchLabel1";
            this.DirectorySearchLabel1.Size = new System.Drawing.Size(118, 13);
            this.DirectorySearchLabel1.TabIndex = 14;
            this.DirectorySearchLabel1.Text = "Искать в директории:";
            // 
            // StartButton1
            // 
            this.StartButton1.Location = new System.Drawing.Point(26, 627);
            this.StartButton1.Name = "StartButton1";
            this.StartButton1.Size = new System.Drawing.Size(75, 23);
            this.StartButton1.TabIndex = 28;
            this.StartButton1.Text = "Пуск";
            this.StartButton1.UseVisualStyleBackColor = true;
            this.StartButton1.Click += new System.EventHandler(this.StartButton1_Click);
            // 
            // StartButton2
            // 
            this.StartButton2.Location = new System.Drawing.Point(638, 627);
            this.StartButton2.Name = "StartButton2";
            this.StartButton2.Size = new System.Drawing.Size(75, 23);
            this.StartButton2.TabIndex = 29;
            this.StartButton2.Text = "Пуск";
            this.StartButton2.UseVisualStyleBackColor = true;
            this.StartButton2.Click += new System.EventHandler(this.StartButton2_Click);
            // 
            // StopButton1
            // 
            this.StopButton1.Location = new System.Drawing.Point(122, 627);
            this.StopButton1.Name = "StopButton1";
            this.StopButton1.Size = new System.Drawing.Size(75, 23);
            this.StopButton1.TabIndex = 30;
            this.StopButton1.Text = "Стоп";
            this.StopButton1.UseVisualStyleBackColor = true;
            this.StopButton1.Click += new System.EventHandler(this.StopButton1_Click);
            // 
            // WaitButton1
            // 
            this.WaitButton1.Location = new System.Drawing.Point(220, 627);
            this.WaitButton1.Name = "WaitButton1";
            this.WaitButton1.Size = new System.Drawing.Size(94, 23);
            this.WaitButton1.TabIndex = 31;
            this.WaitButton1.Text = "Приостановить";
            this.WaitButton1.UseVisualStyleBackColor = true;
            this.WaitButton1.Click += new System.EventHandler(this.WaitButton1_Click);
            // 
            // StopButton2
            // 
            this.StopButton2.Location = new System.Drawing.Point(729, 627);
            this.StopButton2.Name = "StopButton2";
            this.StopButton2.Size = new System.Drawing.Size(75, 23);
            this.StopButton2.TabIndex = 32;
            this.StopButton2.Text = "Стоп";
            this.StopButton2.UseVisualStyleBackColor = true;
            this.StopButton2.Click += new System.EventHandler(this.StopButton2_Click);
            // 
            // WaitButton2
            // 
            this.WaitButton2.Location = new System.Drawing.Point(820, 627);
            this.WaitButton2.Name = "WaitButton2";
            this.WaitButton2.Size = new System.Drawing.Size(95, 23);
            this.WaitButton2.TabIndex = 33;
            this.WaitButton2.Text = "Приостановить";
            this.WaitButton2.UseVisualStyleBackColor = true;
            this.WaitButton2.Click += new System.EventHandler(this.WaitButton2_Click);
            // 
            // PriorityLabel1
            // 
            this.PriorityLabel1.AutoSize = true;
            this.PriorityLabel1.Location = new System.Drawing.Point(23, 163);
            this.PriorityLabel1.Name = "PriorityLabel1";
            this.PriorityLabel1.Size = new System.Drawing.Size(64, 13);
            this.PriorityLabel1.TabIndex = 34;
            this.PriorityLabel1.Text = "Приоритет:";
            // 
            // PriorityComboBox1
            // 
            this.PriorityComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.PriorityComboBox1.FormattingEnabled = true;
            this.PriorityComboBox1.Items.AddRange(new object[] {
            "Lowest",
            "BelowNormal",
            "Normal",
            "AboveNormal",
            "Highest"});
            this.PriorityComboBox1.Location = new System.Drawing.Point(113, 160);
            this.PriorityComboBox1.Name = "PriorityComboBox1";
            this.PriorityComboBox1.Size = new System.Drawing.Size(121, 21);
            this.PriorityComboBox1.TabIndex = 35;
            // 
            // PriorityComboBox2
            // 
            this.PriorityComboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.PriorityComboBox2.FormattingEnabled = true;
            this.PriorityComboBox2.Items.AddRange(new object[] {
            "Lowest",
            "BelowNormal",
            "Normal",
            "AboveNormal",
            "Highest"});
            this.PriorityComboBox2.Location = new System.Drawing.Point(720, 157);
            this.PriorityComboBox2.Name = "PriorityComboBox2";
            this.PriorityComboBox2.Size = new System.Drawing.Size(121, 21);
            this.PriorityComboBox2.TabIndex = 37;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(632, 160);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(64, 13);
            this.label1.TabIndex = 36;
            this.label1.Text = "Приоритет:";
            // 
            // SearchForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1184, 662);
            this.Controls.Add(this.PriorityComboBox2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.PriorityComboBox1);
            this.Controls.Add(this.PriorityLabel1);
            this.Controls.Add(this.WaitButton2);
            this.Controls.Add(this.StopButton2);
            this.Controls.Add(this.WaitButton1);
            this.Controls.Add(this.StopButton1);
            this.Controls.Add(this.StartButton2);
            this.Controls.Add(this.StartButton1);
            this.Controls.Add(this.ResultlistBox2);
            this.Controls.Add(this.SearchStringtextBox2);
            this.Controls.Add(this.SearchStringLabel2);
            this.Controls.Add(this.TemplateTextBox2);
            this.Controls.Add(this.Templatelabel2);
            this.Controls.Add(this.GetDirectoryButton2);
            this.Controls.Add(this.DirectorySearchLabel2);
            this.Controls.Add(this.ResultlistBox1);
            this.Controls.Add(this.SearchStringtextBox1);
            this.Controls.Add(this.TemplateTextBox1);
            this.Controls.Add(this.Templatelabel1);
            this.Controls.Add(this.SearchStringLabel1);
            this.Controls.Add(this.GetDirectoryButton1);
            this.Controls.Add(this.DirectorySearchLabel1);
            this.Name = "SearchForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Поиск файлов";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.SearchForm_FormClosing);
            this.ResumeLayout(false);
            this.PerformLayout();
 
        }
 
        #endregion
 
        private System.Windows.Forms.ListBox ResultlistBox2;
        private System.Windows.Forms.TextBox SearchStringtextBox2;
        private System.Windows.Forms.Label SearchStringLabel2;
        private System.Windows.Forms.TextBox TemplateTextBox2;
        private System.Windows.Forms.Label Templatelabel2;
        private System.Windows.Forms.Button GetDirectoryButton2;
        private System.Windows.Forms.Label DirectorySearchLabel2;
        private System.Windows.Forms.ListBox ResultlistBox1;
        private System.Windows.Forms.TextBox SearchStringtextBox1;
        private System.Windows.Forms.TextBox TemplateTextBox1;
        private System.Windows.Forms.Label Templatelabel1;
        private System.Windows.Forms.Label SearchStringLabel1;
        private System.Windows.Forms.Button GetDirectoryButton1;
        private System.Windows.Forms.Label DirectorySearchLabel1;
        private System.Windows.Forms.Button StartButton1;
        private System.Windows.Forms.Button StartButton2;
        private System.Windows.Forms.Button StopButton1;
        private System.Windows.Forms.Button WaitButton1;
        private System.Windows.Forms.Button StopButton2;
        private System.Windows.Forms.Button WaitButton2;
        private System.Windows.Forms.Label PriorityLabel1;
        private System.Windows.Forms.ComboBox PriorityComboBox1;
        private System.Windows.Forms.ComboBox PriorityComboBox2;
        private System.Windows.Forms.Label label1;
    }
}
Добавлено через 4 минуты
Функция, в которой текст исчется TextSearch называется
0
28.02.2013, 22:54
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
28.02.2013, 22:54
Помогаю со студенческими работами здесь

Запуск приложения с высоким приоритетом
Здравствуйте! У меня проблема... Нужно запустить приложение с высоким приоритетом В свойствах...

Запустить выполнение скрипта с "низким приоритетом" SQL Server
Добрый день! Что я имею ввиду: Хочу создать job внутри которого будет скрипт, который будет...

Исправить работу видео на мобильных экранах с низким разрешением
Так-с, народ, короче помощ нужна :) У меня вот такая проблема, мне нужно чтобы при клике на...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
3
Ответ Создать тему
Опции темы

КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2023, CyberForum.ru