Results 1 to 9 of 9

Thread: dotnet / C# threading question - passing data?

  1. #1
    Theoretical Element Spud1's Avatar
    Join Date
    Jul 2003
    Location
    North West
    Posts
    7,508
    Thanks
    336
    Thanked
    320 times in 255 posts
    • Spud1's system
      • Motherboard:
      • Gigabyte Aorus Master
      • CPU:
      • 9900k
      • Memory:
      • 16GB GSkill Trident Z
      • Storage:
      • Lots.
      • Graphics card(s):
      • RTX3090
      • PSU:
      • 750w
      • Case:
      • BeQuiet Dark Base Pro rev.2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Asus PG35VQ
      • Internet:
      • 910/100mb Fibre

    dotnet / C# threading question - passing data?

    I'm starting to explore how to use threading in my applications now, and i can successfully create/execute basic threads in C#. eg this:

    Code:
    ThreadStart thrStartUpdate = new ThreadStart (threaded_run_check);
    Thread fThread = new Thread (thrStartUpdate);							
    fThread.Name = "thrDocbaseUpdate";								
    fThread.Start();
    That code works fine, the method 'threaded_run_check' executes in its own thread and then i have more thread handling code later on to find out when its finished etc.

    My question is - is it possible to call a method that is NOT void (ie has a return type) and/or can take parameters? Obviously not by 'standard' methods (ie not the same as what you would do with a normal method), but maybe through some threading methods? I have searched the net for an example or something saying one way or the other, but I can;t find anything

    Any help would be appreciated,

  2. #2
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS
    it's probably not the best way, but I've had my threads directly accessing private variables - getting & setting as needed - whch are part of the same class

  3. #3
    Theoretical Element Spud1's Avatar
    Join Date
    Jul 2003
    Location
    North West
    Posts
    7,508
    Thanks
    336
    Thanked
    320 times in 255 posts
    • Spud1's system
      • Motherboard:
      • Gigabyte Aorus Master
      • CPU:
      • 9900k
      • Memory:
      • 16GB GSkill Trident Z
      • Storage:
      • Lots.
      • Graphics card(s):
      • RTX3090
      • PSU:
      • 750w
      • Case:
      • BeQuiet Dark Base Pro rev.2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Asus PG35VQ
      • Internet:
      • 910/100mb Fibre
    Yeah that is what I might have to do, it won't really matter for a small program like im using to test it, but surely there must be a better way...I guess it's not *that* important and I could use properties to do it, its just not as clean as passing params and getting back a return value ;/

  4. #4
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts
    inter-thread memory might seam like a complex issue, but one well worth learning, and one sawly neglected on a lot of computer science sylbuses

    The idea is that you might have two threads wanting to write to the same data at the same time. You also have RWR errors, these happen when your writing to memory, and there are read requests either side, obviously the memory has to be locked to one of the request else it goes titsup.

    this is often called thread syncronisation, because its two (or more) threads interacting simultaniously.

    now whilst your probably just wanting to know about accessing memory, its worth while learning about timing, and "blocking functions".

    most inter-proccess/thread stuff relies on one waiting for hte other to finish (ie blocking on that instruction). Or when asyncronous stuff is wanted (ie you don't want ur code to hang, just get an event every time the memory can be used).

    http://msdn2.microsoft.com/en-us/library/ms173179.aspx should give a grounding

    learn by sample http://msdn2.microsoft.com/en-us/library/b6dc69b7.aspx

    some of these functions ARE PLATFORM SPECIFIC. This is because there are many schools of thought on how to handle memory.
    throw new ArgumentException (String, String, Exception)

  5. #5
    Theoretical Element Spud1's Avatar
    Join Date
    Jul 2003
    Location
    North West
    Posts
    7,508
    Thanks
    336
    Thanked
    320 times in 255 posts
    • Spud1's system
      • Motherboard:
      • Gigabyte Aorus Master
      • CPU:
      • 9900k
      • Memory:
      • 16GB GSkill Trident Z
      • Storage:
      • Lots.
      • Graphics card(s):
      • RTX3090
      • PSU:
      • 750w
      • Case:
      • BeQuiet Dark Base Pro rev.2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Asus PG35VQ
      • Internet:
      • 910/100mb Fibre
    thanks for the links, I will have a read through them It seems very useful, and getting a decent grounding now should help for when I go back to uni after I finish my placement.

  6. #6
    Senior Member Shad's Avatar
    Join Date
    Jul 2003
    Location
    In front
    Posts
    2,782
    Thanks
    23
    Thanked
    42 times in 25 posts
    What's the scenario?

    The last multi-threaded app I wrote was part of a web based system that imported data from a spread sheet (bane of my life). Anyway, ASP.NET's intrinsic objects made life easy for sharing data.

    The worker thread ran and iterated through the spread sheet. Each time around the loop it would write information to session items (though there are other places you could write too, such as the page cache) so that when the page was refreshed it was able to give details about how the thread was doing. If you use the page cache (which persists between different sessions until the item expires, is removed or the application is restarted) you can even block other users from performing the same task, or interact with completely different areas of the system.

    If the method your are running in your thread has parameters, then you must pass variables via properties of the class.

    I don't do a great deal of desktop based stuff at the moment, but in a .NET 2.0 app I wrote a few months ago there were a few similar places you can share data from, within the application while all threads are running. You can do clever things with pooling too but I haven't gone that far yet!

    Hope that helps
    Simon


  7. #7
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts
    ado.net will let you access a spreadsheet via active-x into a nice typed data set shad!

    sorry to take thread off topic.
    throw new ArgumentException (String, String, Exception)

  8. #8
    Senior Member Shad's Avatar
    Join Date
    Jul 2003
    Location
    In front
    Posts
    2,782
    Thanks
    23
    Thanked
    42 times in 25 posts
    Indeed. However, accessing the data isn't normally the issue; it's making sense of the mangled mess of information that the customer usually refers to as their 'database' but which is really no more than a few worksheets with some annoying layouts and stupid formatting
    Simon


  9. #9
    Theoretical Element Spud1's Avatar
    Join Date
    Jul 2003
    Location
    North West
    Posts
    7,508
    Thanks
    336
    Thanked
    320 times in 255 posts
    • Spud1's system
      • Motherboard:
      • Gigabyte Aorus Master
      • CPU:
      • 9900k
      • Memory:
      • 16GB GSkill Trident Z
      • Storage:
      • Lots.
      • Graphics card(s):
      • RTX3090
      • PSU:
      • 750w
      • Case:
      • BeQuiet Dark Base Pro rev.2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Asus PG35VQ
      • Internet:
      • 910/100mb Fibre
    Quote Originally Posted by Shad
    What's the scenario?

    If the method your are running in your thread has parameters, then you must pass variables via properties of the class.
    Thanks for the advice, this is kinda what I am doing now. Well almost lol, I have further abstracted my classes and separated them out - and with the smaller authentication class I can justify having a few class level variables which I can use as parameters and return values. In this particular case there is no danger of another thread overwriting these values, and with a bit of sycronisation I am not going to be reading from null values either. Works great Thanks for the suggestions and info ppl, really helps when learning a new language or programming technique

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Data Recovery
    By LayZeh in forum PC Hardware and Components
    Replies: 8
    Last Post: 04-08-2005, 11:00 PM
  2. Modem connecting to internet but not passing data.
    By domdom8754 in forum PC Hardware and Components
    Replies: 8
    Last Post: 01-04-2004, 01:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •