English 中文(简体)
WaitHandle.WaitAny()和WaitHandle.Wait All()使用问题
原标题:WaitHandle.WaitAny() & WaitHandle.WaitAll() usage problem

我的申请并不恰当。 在等待所有上载业务完成后,我只是试图印刷连接的总数,然后quit。

以下是准则......

using System;
using System.Net;
using System.Threading;
using System.IO;
using System.Text;

namespace ServicePointDemo
{
    class Program
    {
        struct UploadState
        {
            public string Filename;
            public AutoResetEvent are;
        }

        static void Main(string[] args)
        {
            AutoResetEvent are = new AutoResetEvent(false);
            ServicePoint sp = ServicePointManager.FindServicePoint(new Uri("ftp://xxx.xxx.xxx.xxx/public"));

            UploadState us1 = new UploadState();
            us1.are = new AutoResetEvent(false);
            us1.Filename = @"C:inventory.xls";

            UploadState us2 = new UploadState();
            us2.are = new AutoResetEvent(false);
            us2.Filename = @"C:somefile.txt";

            Thread t1, t2;
            t1 = new Thread(new ParameterizedThreadStart(DoUpload));
            t2 = new Thread(new ParameterizedThreadStart(DoUpload));

            t1.Start(us1);            
            t2.Start(us2);

            Console.WriteLine("Waiting for something to trigger up");
            WaitHandle.WaitAny(new WaitHandle[] { us1.are, us2.are });            

            Console.WriteLine("CurrentConnections = {0}", sp.CurrentConnections);
            Console.WriteLine("Waiting for all operations to complete...");            
            WaitHandle.WaitAll(new WaitHandle[] { us1.are, us2.are });

            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }

        static void DoUpload(object state)
        {
            string filename = ((UploadState)state).Filename;
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://172.16.130.22/public/" + Path.GetFileName(filename));
            Console.WriteLine("Upload URI = {0}", ftpRequest.RequestUri.AbsoluteUri);
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
            ftpRequest.Credentials = new NetworkCredential("anonymous", "guest@");
            ftpRequest.Proxy = new WebProxy();
            Stream stream = null;
            FileStream file = new FileStream(filename, FileMode.Open);
            Console.WriteLine("Total file size of {0} = {1}", filename, file.Length);
            StreamReader rdr = new StreamReader(file);
            Console.WriteLine("Getting bytes of {0}", filename);
            byte[] fileBytes = Encoding.ASCII.GetBytes(rdr.ReadToEnd());
            rdr.Close();
            Console.WriteLine("Acquiring connection of {0} upload...", filename);
            try
            {
                stream = ftpRequest.GetRequestStream();
                Console.WriteLine("Upload of {0} has acquired a connection", filename);
                ((UploadState)state).are.Set();
                stream.Write(fileBytes, 0, fileBytes.Length);
                Console.WriteLine("Uploading {0} complete", filename);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception has occurred: {0}", ex.Message);                
            }
            finally
            {
                Console.WriteLine("Ending uploading {0}", filename);
                stream.Close();
                ((UploadState)state).are.Set();                
            }
            Console.WriteLine("Quit DoUpload() for {0}", filename);//...is not executed(?)
        }
    }
}
问题回答

你们似乎有一个种族条件。

如果胎面迅速运行,可以在主线到达第一个瓦伊特阿尼(WaitAny)之前,两次接上“Seta()”号汽车再造活动。 这样,它就可以通过第一个瓦伊特阿尼(WaitAny)(),但会阻碍瓦伊特(WaitAll())的实现,因为两面都不会再接过任何电话。 (在你开始两条背景线后,通过在主线上铺设一条长线,可以模拟)

我建议你使用两个单独的自动检索系统(Orwang EWventaitHandles,在接获时使用一套,在连接完成后安装,因此,你不依赖线的时机。

将WaitAll()电话改为请Join()接线。

顺便提一下,主(主)不需要第一部自动检索代码<>are,因为它没有使用。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签