同步线程似乎一次只有一个起作用?

发布于 2024-10-22 03:05:18 字数 1394 浏览 8 评论 0原文

线程a + b(两者都试图删除文件)。 首先调用 a,然后在 a 仍在运行时调用 bb 成功删除文件,但 a 没有成功。

如果我单独运行a,则a的文件可以正常删除。

当我单步执行代码时,我可以看到 a 的 MultiAttemptFilename 被 b 的 MultiAttemptFilename 覆盖。

我不明白。

我有一个 ajax 调用,指向一个通用处理程序,该处理程序随文件名一起传递。 在我的处理程序中,我有以下代码:

    Dim Dc As New Document
    Dim MyThread As New Thread(AddressOf Dc.DeleteFileMulitAttempt)
    Dc.MulitAttemptFilename = Filename
    MyThread.Start()

从我的“文档”类中,我调用以下内容:

    #Region "Delete"

  Public MulitAttemptFilename As String = ""
  Public Sub DeleteFileMulitAttempt()
      Dim TimeBetweenAttempts As Integer = 2000
      Dim NumberOfAttempts As Integer = 60
      Dim AttemptNumber As Integer = 0
      Dim Success As Boolean = False
      While (AttemptNumber < NumberOfAttempts)
          Try
              Success = (DeleteFile(MulitAttemptFilename) = "Ok")
          Catch ex As Exception
              Success = False
          End Try
          If (Success) Then Exit While
          Thread.Sleep(TimeBetweenAttempts)
          AttemptNumber += 1
      End While
      End If
  End Sub

...

这是为了处理取消/失败的上传,因为它们并不总是立即删除(服务器锁定等),因此会出现循环。

我在这里错过了一些基本的东西吗?

Threads a + b, (both are trying to delete files).
a gets called first, then b while a is still running.
b deletes the file successfully but a doesn't.

If I run a on its own, a's file deletes fine.

When I step through the code I can see that a's MultiAttemptFilename gets overwritten with b's.

I don't understand.

I have an ajax call pointing to a generic handler which passes the filename along with it.
In my handler I have the following code:

    Dim Dc As New Document
    Dim MyThread As New Thread(AddressOf Dc.DeleteFileMulitAttempt)
    Dc.MulitAttemptFilename = Filename
    MyThread.Start()

From my 'Document' class I'm calling the following:

    #Region "Delete"

  Public MulitAttemptFilename As String = ""
  Public Sub DeleteFileMulitAttempt()
      Dim TimeBetweenAttempts As Integer = 2000
      Dim NumberOfAttempts As Integer = 60
      Dim AttemptNumber As Integer = 0
      Dim Success As Boolean = False
      While (AttemptNumber < NumberOfAttempts)
          Try
              Success = (DeleteFile(MulitAttemptFilename) = "Ok")
          Catch ex As Exception
              Success = False
          End Try
          If (Success) Then Exit While
          Thread.Sleep(TimeBetweenAttempts)
          AttemptNumber += 1
      End While
      End If
  End Sub

...

This is to handle cancelled/failed uploads as they don't always delete right away (server locks etc), hence the loop.

Am I missing something fundamental here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栩栩如生 2024-10-29 03:05:18

看来您可能缺少多线程并发的基本概念。有一些专门讨论此问题的书籍,并且 .NET 书籍中的某些部分通常会解决此问题。这里只是关于该主题的Microsoft 的一篇文章

一个简短的答案是您需要使用 VB 的“lock”关键字。您创建一个对象,然后大致执行类似

lock(yourLockObject)
{
   //any code that might access a shared resource like the variable 
   //MulitAttempFilename [sic] would go here. 
}

我不会说 VB 的操作,但看起来您正在制作真正需要保护的全局变量。任何形式的全局数据几乎都是一个坏主意,当涉及到多线程时,这真是一个非常非常糟糕的主意。您必须重写代码以保护对被删除文件名称的访问。当您阅读多线程时,您可能还想了解线程池。

It seems like you might be missing the fundamental concept of multi-threaded concurrency. There are books dedicated to this, and often sections of .NET books will address this issue. Here's just one article by Microsoft on the topic.

One short answer is you need to use VB's "lock" keyword. You create an object and you do roughly something like

lock(yourLockObject)
{
   //any code that might access a shared resource like the variable 
   //MulitAttempFilename [sic] would go here. 
}

I don't speak VB but it looks like you're making the one thing that really needs to be protected a global variable. Global data is pretty much a bad idea in any form and when it comes to multi-threading it's a really, really bad idea. You'll have to rewrite your code to protect access to the name of the file being deleted. While you're reading up on multi-threading you might also want to learn about thread pools.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文