将应用程序转换为在网络上工作 [英] Converting an application to work on a network

查看:51
本文介绍了将应用程序转换为在网络上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经转换了许多应用程序,使它们能够在网络上一起工作。我被引导相信我可以这样做:


FileStream fs = new FileStream(一些代码);

while(!fs.CanRead ){允许处理器处理另一个线程;}


Hoverer,做一些测试我发现解决方案不起作用。


有一个简单的出路吗?

I have converted a number of applications to enable them to work together on
a network. I have been led to believe that I can do this as follows:

FileStream fs = new FileStream(some code);
while (!fs.CanRead){allow the processor to handle another thread;}

Hoverer, doing some tests I find out that the solution isn''t working.

Is there a simple way out?

推荐答案




我不觉得CanRead属性是动态更新的。我相信

它在创建FileStream时获得它的价值。


请尝试以下代码:


public static FileStream OpenWhenReady(字符串路径,FileAccess访问)

{

FileStream stream = null;


do

{

试试

{

stream = new FileStream(path,FileMode.Open,access);

}

catch(IOException)

{

System.Threading.Thread.Sleep(500);

}

catch(UnauthorizedAccessException)

{

System.Threading.Thread.Sleep(500);

}

}

while(stream == null);


返回流;

}


使用这样的方法:


using(FileStream stream = OpenWhenReady(path,FileAccess.Read))

{

StreamReader reader = new StreamReader(stream);

Console.WriteLine(reader.ReadToEnd());

}


-

Dave Sexton


" Adrian<" < no*@all.accessiblewrote in message

news:45 ******************** @ dreader2.news.tiscali。 nl ...
Hi,

I don''t think that the CanRead property is dynamically updated. I believe
it acquires its value when the FileStream is created.

Try the following instead:

public static FileStream OpenWhenReady(string path, FileAccess access)
{
FileStream stream = null;

do
{
try
{
stream = new FileStream(path, FileMode.Open, access);
}
catch (IOException)
{
System.Threading.Thread.Sleep(500);
}
catch (UnauthorizedAccessException)
{
System.Threading.Thread.Sleep(500);
}
}
while (stream == null);

return stream;
}

Use the method like this:

using (FileStream stream = OpenWhenReady(path, FileAccess.Read))
{
StreamReader reader = new StreamReader(stream);
Console.WriteLine(reader.ReadToEnd());
}

--
Dave Sexton

"Adrian <" <no*@all.accessiblewrote in message
news:45********************@dreader2.news.tiscali. nl...

>我已经转换了许多应用程序以使它们能够在网络上一起工作

。我被引导相信我可以这样做:


FileStream fs = new FileStream(一些代码);

while(!fs.CanRead ){允许处理器处理另一个线程;}


Hoverer,做一些测试我发现解决方案不起作用。


有一个简单的出路吗?

>I have converted a number of applications to enable them to work together
on
a network. I have been led to believe that I can do this as follows:

FileStream fs = new FileStream(some code);
while (!fs.CanRead){allow the processor to handle another thread;}

Hoverer, doing some tests I find out that the solution isn''t working.

Is there a simple way out?



" Dave Sexton" < dave @jwa [remove.this] online.comwrote in message

news:um ************** @ TK2MSFTNGP02.phx.gbl ...
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:um**************@TK2MSFTNGP02.phx.gbl...




我不认为CanRead属性是动态更新的。我相信

它在创建FileStream时获得它的价值。


请尝试以下代码:


public static FileStream OpenWhenReady(字符串路径,FileAccess访问)

{

FileStream stream = null;


do

{

试试

{

stream = new FileStream(path,FileMode.Open,access);

}

catch(IOException)

{

System.Threading.Thread.Sleep(500);

}

catch(UnauthorizedAccessException)

{

System.Threading.Thread.Sleep(500);

}

}

while(stream == null);


返回流;

}


使用这样的方法:


using(FileStream stream = OpenWhenReady(path,FileAccess.Read))

{

St reamReader reader = new StreamReader(stream);

Console.WriteLine(reader.ReadToEnd());

}


- -

Dave Sexton
Hi,

I don''t think that the CanRead property is dynamically updated. I believe
it acquires its value when the FileStream is created.

Try the following instead:

public static FileStream OpenWhenReady(string path, FileAccess access)
{
FileStream stream = null;

do
{
try
{
stream = new FileStream(path, FileMode.Open, access);
}
catch (IOException)
{
System.Threading.Thread.Sleep(500);
}
catch (UnauthorizedAccessException)
{
System.Threading.Thread.Sleep(500);
}
}
while (stream == null);

return stream;
}

Use the method like this:

using (FileStream stream = OpenWhenReady(path, FileAccess.Read))
{
StreamReader reader = new StreamReader(stream);
Console.WriteLine(reader.ReadToEnd());
}

--
Dave Sexton



*********************** *********

戴夫,


你的建议看起来非常好。在

的平均时间内,我想到了以下内容:

即使用该目录作为

文件管理的手段,就像这样;你觉得怎么样?

我猜你的解决方案是专业的。


Adrian


void block( int arg)

{

string blocker_file =" block" + arg.ToString();

while(File.Exists(blocker_file) )

{

System.Threading.Thread.Sleep(200);

}

StreamWriter fs = File .CreateText(blocker_file);

}


void unblock(int arg)

{

string blocker_file =" block" + arg.ToString();

while(true)

{

try

{

File.Delete(blocker_file);

休息;

}

catch

{

System.Threading.Thread.Sleep(200);

继续;

}

}

}

********************************
Dave,

What you suggest looks very good. In the
mean time, the following came to my mind,
namely using the directory as means for the
file management, like so; what do you think?
I guess your solution is the professional one.

Adrian

void block(int arg)
{
string blocker_file = "block"+arg.ToString();
while(File.Exists(blocker_file))
{
System.Threading.Thread.Sleep(200);
}
StreamWriter fs = File.CreateText(blocker_file);
}

void unblock(int arg)
{
string blocker_file = "block"+arg.ToString();
while(true)
{
try
{
File.Delete(blocker_file);
break;
}
catch
{
System.Threading.Thread.Sleep(200);
continue;
}
}
}


嗨阿德里安,


我的代码并不是要同步访问该文件,它只是意味着

等待可用性。但是,有一些方法可以同步访问,其中一些可以包含在我的例子中:


A.在构造FileStream时指定FileShare枚举参数

B.调用FileStream.Lock一旦打开(Win​​32 LockFile)

C.使用标准的跨进程同步机制,如Mutex

(这将只允许你的应用程序内的同步

进程)

D.创建一个同步控制器服务来同步访问

你的应用程序的共享资源,例如文件。使用Tcp或Ipc在

IIS

或远程控制框架上托管服务。


就个人而言,我不喜欢使用临时文件提供

同步的想法,如你的例子所示。


-

Dave Sexton


" Adrian<"" < no*@all.accessiblewrote in message

news:45 ******************** @ dreader2.news.tiscali。 nl ...
Hi Adrian,

My code isn''t meant to synchronize access to the file, it''s simply meant to
wait for availability. However, there are ways to synchronize access, some
of which could be included in my example:

A. Specify the FileShare enum parameter when constructing the FileStream
B. Call FileStream.Lock once it has been opened (Win32 LockFile)
C. Use standard, cross-process synchronization mechanisms such as Mutex
(This will only allow synchronization from within your application
processes)
D. Create a synchronization controller service to synchronize access to
your application''s shared resources, such as files. Host the service on
IIS
or the remoting framework using Tcp or Ipc.

Personally, I don''t like the idea of using a temporary file to provide
synchronization, as in your example.

--
Dave Sexton

"Adrian <" <no*@all.accessiblewrote in message
news:45********************@dreader2.news.tiscali. nl...

" Dave Sexton" < dave @jwa [remove.this] online.comwrote in message

news:um ************** @ TK2MSFTNGP02.phx.gbl ...
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:um**************@TK2MSFTNGP02.phx.gbl...

>

我不认为CanRead属性是动态更新的。我相信它会在创建FileStream时获得它的价值。

请尝试以下方法:

public static FileStream OpenWhenReady(string path,FileAccess访问)
{/> FileStream stream = null;


{
尝试
{/> stream = new FileStream(path ,FileMode.Open,access);
}
catch(IOException)
{/> System.Threading.Thread.Sleep(500);
}
catch(UnauthorizedAccessException)
{
System.Threading.Thread.Sleep(500);
}
}
while(stream == null);

返回流;


使用如下方法:

使用(FileStream stream = OpenWhenReady(path,FileAccess.Read))
{
StreamReader reader = new StreamReader(stream);
Console.WriteLine(reader.ReadToEnd());
}

-
Dave Sexton
>Hi,

I don''t think that the CanRead property is dynamically updated. I
believe
it acquires its value when the FileStream is created.

Try the following instead:

public static FileStream OpenWhenReady(string path, FileAccess access)
{
FileStream stream = null;

do
{
try
{
stream = new FileStream(path, FileMode.Open, access);
}
catch (IOException)
{
System.Threading.Thread.Sleep(500);
}
catch (UnauthorizedAccessException)
{
System.Threading.Thread.Sleep(500);
}
}
while (stream == null);

return stream;
}

Use the method like this:

using (FileStream stream = OpenWhenReady(path, FileAccess.Read))
{
StreamReader reader = new StreamReader(stream);
Console.WriteLine(reader.ReadToEnd());
}

--
Dave Sexton



********************************

戴夫,


你的建议看起来非常好。在

的平均时间内,我想到了以下内容:

即使用该目录作为

文件管理的手段,就像这样;你觉得怎么样?

我猜你的解决方案是专业的。


Adrian


void block( int arg)

{

string blocker_file =" block" + arg.ToString();

while(File.Exists(blocker_file) )

{

System.Threading.Thread.Sleep(200);

}

StreamWriter fs = File .CreateText(blocker_file);

}


void unblock(int arg)

{

string blocker_file =" block" + arg.ToString();

while(true)

{

try

{

File.Delete(blocker_file);

休息;

}

catch

{

System.Threading.Thread.Sleep(200);

继续;

}

}

}

********************************
Dave,

What you suggest looks very good. In the
mean time, the following came to my mind,
namely using the directory as means for the
file management, like so; what do you think?
I guess your solution is the professional one.

Adrian

void block(int arg)
{
string blocker_file = "block"+arg.ToString();
while(File.Exists(blocker_file))
{
System.Threading.Thread.Sleep(200);
}
StreamWriter fs = File.CreateText(blocker_file);
}

void unblock(int arg)
{
string blocker_file = "block"+arg.ToString();
while(true)
{
try
{
File.Delete(blocker_file);
break;
}
catch
{
System.Threading.Thread.Sleep(200);
continue;
}
}
}



这篇关于将应用程序转换为在网络上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆