Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: dante93150 en 29 Mayo 2016, 04:09 am



Título: Alguien Me Puede Ayudar A Como Suspender Una Thread??
Publicado por: dante93150 en 29 Mayo 2016, 04:09 am
Hola bueno vengo aqui se alguien me puede ayudar a como suspender una thread desde el vb,con el process hacker como lo hago ??
Yo e intentado esto pero no funciona
thread.sleep(1000)
pero como agrego la id o addres del proceso???


Título: Re: Alguien Me Puede Ayudar A Como Suspender Una Thread??
Publicado por: fary en 29 Mayo 2016, 07:53 am
Hola,

Esto es vb.net, iría en otro subforo  :P

De todas formas aquí tienes tu solución.

https://msdn.microsoft.com/es-es/library/system.threading.thread.suspend(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1


Título: Re: Alguien Me Puede Ayudar A Como Suspender Una Thread??
Publicado por: Meta en 29 Mayo 2016, 09:43 am
Hola bueno vengo aqui se alguien me puede ayudar a como suspender una thread desde el vb,con el process hacker como lo hago ??
Yo e intentado esto pero no funciona
thread.sleep(1000)
pero como agrego la id o addres del proceso???

No se suspende, se queda esclavo. Mejor usar timer.

Timer1.Start(); para empezar y Timer1.Stop(); para parar cuando quieras.


Título: Re: Alguien Me Puede Ayudar A Como Suspender Una Thread??
Publicado por: Eleкtro en 29 Mayo 2016, 12:37 pm
De todas formas aquí tienes tu solución.
https://msdn.microsoft.com/es-es/library/system.threading.thread.suspend(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

Yo creo que él más bien se refiere a suspender la ejecución de un hilo por un tiempo definido como hace el método Thread.Sleep(), pero pudiendo especificar un identificador del hilo.



thread.sleep(1000)
como agrego la id o addres del proceso???

No puedes especificar un thread-id, el método Thread.Sleep() solamente afecta al hilo desde donde se invocó a dicha función, de hecho las funciones Win32 Sleep y SleepEx tienen el mismo comportamiento, simplemente NO es posible ...de forma "nativa".

Pero puedes recurrir a las funciones Win32 relacionadas (SuspendThread, ResumeThread), aplicando metodologías asincrónicas.

Te dejo esta solución que he elaborado (lo testeé con el thread-id del UI thread):

Modo de empleo:
Código
  1. Dim threadId As Integer = GetCurrentThreadId().
  2. SleepThread(threadId, TimeSpan.FromSeconds(5))

Código fuente:
Código
  1. Public Shared Sub SleepThread(ByVal threadId As Integer, ByVal timespan As TimeSpan)
  2.  
  3.    Dim hThread As IntPtr
  4.    Dim win32Err As Integer
  5.  
  6.    Dim suspendFunc As Func(Of Integer) =
  7.        Function() As Integer ' Returns the previous suspend count for the thread.
  8.            Dim suspendCount As Integer
  9.            Debug.WriteLine("Sleeping...")
  10.            Thread.Sleep(timespan)
  11.            Debug.WriteLine("Resuming thread...")
  12.            suspendCount = ResumeThread(hThread)
  13.            CloseHandle(hThread)
  14.            Return suspendCount
  15.        End Function
  16.  
  17.    hThread = OpenThread(ThreadAccessRights.SuspendResume Or ThreadAccessRights.Terminate, True, threadId)
  18.    win32Err = Marshal.GetLastWin32Error()
  19.  
  20.    If (hThread = IntPtr.Zero) Then
  21.        Throw New Win32Exception(win32Err)
  22.  
  23.    Else
  24.        Debug.WriteLine("Pausing thread...")
  25.        Dim suspendTask As Task(Of Integer) = Task.Factory.StartNew(Of Integer)(suspendFunc)
  26.        SuspendThread64(hThread)
  27.  
  28.    End If
  29.  
  30. End Sub

P/Invoking:
Código
  1. <SuppressUnmanagedCodeSecurity>
  2. <DllImport("kernel32.dll", SetLastError:=False)>
  3. Public Shared Function GetCurrentThreadId(
  4. ) As <MarshalAs(UnmanagedType.U4)> Integer
  5. End Function
  6.  
  7. <DllImport("kernel32.dll", SetLastError:=True)>
  8. Public Shared Function OpenThread(
  9.    <MarshalAs(UnmanagedType.I4)> ByVal dwDesiredAccess As ThreadAccessRights,
  10.  <MarshalAs(UnmanagedType.Bool)> ByVal bInheritHandle As Boolean,
  11.    <MarshalAs(UnmanagedType.U4)> ByVal dwThreadId As Integer
  12. ) As IntPtr
  13. End Function
  14.  
  15. <DllImport("kernel32.dll", EntryPoint:="Wow64SuspendThread", SetLastError:=True)>
  16. Public Shared Function SuspendThread32(
  17.     <MarshalAs(UnmanagedType.SysInt)> ByVal hThread As IntPtr
  18. ) As Integer
  19. End Function
  20.  
  21. <DllImport("kernel32.dll", EntryPoint:="SuspendThread", SetLastError:=True)>
  22. Public Shared Function SuspendThread64(
  23.     <MarshalAs(UnmanagedType.SysInt)> ByVal hThread As IntPtr
  24. ) As Integer
  25. End Function
  26.  
  27. <DllImport("kernel32.dll", SetLastError:=True)>
  28. Public Shared Function ResumeThread(
  29.     <MarshalAs(UnmanagedType.SysInt)> ByVal hThread As IntPtr
  30. ) As Integer
  31. End Function
  32.  
  33. <DllImport("kernel32.dll", SetLastError:=True)>
  34. Public Shared Function CloseHandle(
  35. <MarshalAs(UnmanagedType.SysInt)> ByVal hObject As IntPtr
  36. ) As <MarshalAs(UnmanagedType.Bool)> Boolean
  37. End Function
  38.  
  39. <Flags>
  40. Public Enum ThreadAccessRights As Integer
  41.    Terminate = &H1
  42.    SuspendResume = &H2
  43. End Enum

PD: Todo esto lo puedes encontrar en mi API ElektroKit (en mi firma, aquí abajo).

Saludos.