Is being used by another process, so the process cannot access the file.
In system. Io. _ error. winioerror (int32 errorcode, string maybefullpath)
In system. io. filestream. init (string path, filemode mode, fileaccess access, int32 rights, Boolean userights, fileshare share, int32 buffersize, fileoptions options, security_attributes secattrs, string msgpath, Boolean bfromproxy)
In system. Io. filestream... ctor (string path, filemode mode, fileaccess access, fileshare)
This is because of the mutex in multiple threads. An exception occurs when a file is not closed and data streams are written.
The lock failed to solve the problem. Later, I felt that the mutex volume was good. After the test, the problem was solved and the exception notification was lost.
View plaincopy to clipboardprint?
01. namespace filewrite
02 .{
03. Public partial class form1: Form
04 .{
05. mutex CTX = new mutex ();
06. Public form1 ()
07 .{
08. initializecomponent ();
09 .}
10.
11. Private void button#click (Object sender, eventargs E)
12 .{
13. For (INT I = 0; I <30; I ++)
14 .{
15. Thread checkclientonline = new thread (checkonline );
16. checkclientonline. Start ();
17 .}
18.
19 .}
20. // write logs
21. Public static void writetolog1 (String title)
22 .{
23.
24.
25. If (Title = "") return;
26. String filename = application. startuppath;
27. // object thislock = new object ();
28.
29 .{
30. filestream FS = new filestream (filename + "http://www.cnblogs.com/itecho/admin/file://tiplog.txt/", filemode. append, fileaccess. Write, fileshare. Write );
31. byte [] btitle = unicodetombcs (title );
32. fs. Write (btitle, 0, btitle. Length );
33. fs. Close ();
34 .}
35 .}
36.
37. Public static byte [] unicodetombcs (string SRC)
38 .{
39. Encoding ENC = encoding. getencoding (936); // dont use codePage 52936, but 54936 or 936
40. Int Len = SRC. length;
41.
42. byte [] tmpb = new byte [Len * 2];
43.
44. tmpb = enc. getbytes (SRC );
45.
46. // string tmphead = tmpb. length. tostring ();
47. // tmphead = tmphead. padleft (4, '0 ');
48.
49. // tmpb = enc. getbytes (tmphead + SRC );
50. Return tmpb;
51 .}
52. Void checkonline ()
53 .{
54. While (true)
55 .{
56. Try
57 .{
58.
59. CTX. waitone ();
60. // Write File here
61. writetolog1 ("hhh ");
62. CTX. releasemutex ();
63 .;
64.} catch (exception E)
65 .{
66. Console. writeline (E. tostring ());
67 .}
68 .}
69 .}
70 .}
71 .}